2 * Copyright (c) 2007 Apple Inc. All Rights Reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1988 University of Utah.
30 * Copyright (c) 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * the Systems Programming Group of the University of Utah Computer
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
65 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
67 * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95
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,
77 * Mapped file (mmap) interface to VM
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/filedesc.h>
83 #include <sys/proc_internal.h>
84 #include <sys/kauth.h>
85 #include <sys/resourcevar.h>
86 #include <sys/vnode_internal.h>
89 #include <sys/file_internal.h>
90 #include <sys/vadvise.h>
91 #include <sys/trace.h>
96 #include <sys/ubc_internal.h>
97 #include <sys/sysproto.h>
99 #include <sys/cprotect.h>
102 #include <sys/syscall.h>
103 #include <sys/kdebug.h>
104 #include <sys/bsdtask_info.h>
106 #include <security/audit/audit.h>
107 #include <bsm/audit_kevents.h>
109 #include <mach/mach_types.h>
110 #include <mach/mach_traps.h>
111 #include <mach/vm_sync.h>
112 #include <mach/vm_behavior.h>
113 #include <mach/vm_inherit.h>
114 #include <mach/vm_statistics.h>
115 #include <mach/mach_vm.h>
116 #include <mach/vm_map.h>
117 #include <mach/host_priv.h>
118 #include <mach/sdt.h>
120 #include <machine/machine_routines.h>
122 #include <kern/cpu_number.h>
123 #include <kern/host.h>
124 #include <kern/task.h>
125 #include <kern/page_decrypt.h>
127 #include <IOKit/IOReturn.h>
129 #include <vm/vm_map.h>
130 #include <vm/vm_kern.h>
131 #include <vm/vm_pager.h>
132 #include <vm/vm_protos.h>
135 * XXX Internally, we use VM_PROT_* somewhat interchangeably, but the correct
136 * XXX usage is PROT_* from an interface perspective. Thus the values of
137 * XXX VM_PROT_* and PROT_* need to correspond.
140 mmap(proc_t p
, struct mmap_args
*uap
, user_addr_t
*retval
)
143 * Map in special device (must be SHARED) or file
146 register struct vnode
*vp
;
151 kern_return_t result
;
152 vm_map_offset_t user_addr
;
153 vm_map_size_t user_size
;
154 vm_object_offset_t pageoff
;
155 vm_object_offset_t file_pos
;
160 memory_object_t pager
= MEMORY_OBJECT_NULL
;
161 memory_object_control_t control
;
169 * Note that for UNIX03 conformance, there is additional parameter checking for
170 * mmap() system call in libsyscall prior to entering the kernel. The sanity
171 * checks and argument validation done in this function are not the only places
172 * one can get returned errnos.
175 user_map
= current_map();
176 user_addr
= (vm_map_offset_t
)uap
->addr
;
177 user_size
= (vm_map_size_t
) uap
->len
;
179 AUDIT_ARG(addr
, user_addr
);
180 AUDIT_ARG(len
, user_size
);
181 AUDIT_ARG(fd
, uap
->fd
);
183 prot
= (uap
->prot
& VM_PROT_ALL
);
186 * Since the hardware currently does not support writing without
187 * read-before-write, or execution-without-read, if the request is
188 * for write or execute access, we must imply read access as well;
189 * otherwise programs expecting this to work will fail to operate.
191 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
192 prot
|= VM_PROT_READ
;
193 #endif /* radar 3777787 */
199 * The vm code does not have prototypes & compiler doesn't do the'
200 * the right thing when you cast 64bit value and pass it in function
201 * call. So here it is.
203 file_pos
= (vm_object_offset_t
)uap
->pos
;
206 /* make sure mapping fits into numeric range etc */
207 if (file_pos
+ user_size
> (vm_object_offset_t
)-PAGE_SIZE_64
)
211 * Align the file position to a page boundary,
212 * and save its page offset component.
214 pageoff
= (file_pos
& vm_map_page_mask(user_map
));
215 file_pos
-= (vm_object_offset_t
)pageoff
;
218 /* Adjust size for rounding (on both ends). */
219 user_size
+= pageoff
; /* low end... */
220 user_size
= vm_map_round_page(user_size
,
221 vm_map_page_mask(user_map
)); /* hi end */
223 if (flags
& MAP_JIT
) {
224 if ((flags
& MAP_FIXED
) ||
225 (flags
& MAP_SHARED
) ||
226 !(flags
& MAP_ANON
) ||
227 (flags
& MAP_RESILIENT_CODESIGN
)) {
232 if ((flags
& MAP_RESILIENT_CODESIGN
) ||
233 (flags
& MAP_RESILIENT_MEDIA
)) {
234 assert(!(flags
& MAP_JIT
));
235 if (flags
& MAP_ANON
) {
238 if (prot
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) {
244 * Check for illegal addresses. Watch out for address wrap... Note
245 * that VM_*_ADDRESS are not constants due to casts (argh).
247 if (flags
& MAP_FIXED
) {
249 * The specified address must have the same remainder
250 * as the file offset taken modulo PAGE_SIZE, so it
251 * should be aligned after adjustment by pageoff.
253 user_addr
-= pageoff
;
254 if (user_addr
& vm_map_page_mask(user_map
))
258 /* DO not have apis to get this info, need to wait till then*/
260 * XXX for non-fixed mappings where no hint is provided or
261 * the hint would fall in the potential heap space,
262 * place it after the end of the largest possible heap.
264 * There should really be a pmap call to determine a reasonable
267 else if (addr
< vm_map_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
,
268 vm_map_page_mask(user_map
)))
269 addr
= vm_map_round_page(p
->p_vmspace
->vm_daddr
+ MAXDSIZ
,
270 vm_map_page_mask(user_map
));
276 if (flags
& MAP_ANON
) {
278 maxprot
= VM_PROT_ALL
;
283 error
= mac_proc_check_map_anon(p
, user_addr
, user_size
, prot
, flags
, &maxprot
);
290 * Mapping blank space is trivial. Use positive fds as the alias
291 * value for memory tracking.
295 * Use "fd" to pass (some) Mach VM allocation flags,
296 * (see the VM_FLAGS_* definitions).
298 alloc_flags
= fd
& (VM_FLAGS_ALIAS_MASK
| VM_FLAGS_SUPERPAGE_MASK
|
300 if (alloc_flags
!= fd
) {
301 /* reject if there are any extra flags */
310 struct vnode_attr va
;
311 vfs_context_t ctx
= vfs_context_current();
317 * Mapping file, get fp for validation. Obtain vnode and make
318 * sure it is of appropriate type.
320 err
= fp_lookup(p
, fd
, &fp
, 0);
324 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
326 uap
->addr
= (user_addr_t
)user_addr
;
327 uap
->len
= (user_size_t
)user_size
;
331 error
= pshm_mmap(p
, uap
, retval
, fp
, (off_t
)pageoff
);
339 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
340 error
= vnode_getwithref(vp
);
344 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
) {
350 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
353 * POSIX: mmap needs to update access time for mapped files
355 if ((vnode_vfsvisflags(vp
) & MNT_NOATIME
) == 0) {
357 nanotime(&va
.va_access_time
);
358 VATTR_SET_ACTIVE(&va
, va_access_time
);
359 vnode_setattr(vp
, &va
, ctx
);
363 * XXX hack to handle use of /dev/zero to map anon memory (ala
366 if (vp
->v_type
== VCHR
|| vp
->v_type
== VSTR
) {
372 * Ensure that file and memory protections are
373 * compatible. Note that we only worry about
374 * writability if mapping is shared; in this case,
375 * current and max prot are dictated by the open file.
376 * XXX use the vnode instead? Problem is: what
377 * credentials do we use for determination? What if
378 * proc does a setuid?
380 maxprot
= VM_PROT_EXECUTE
; /* ??? */
381 if (fp
->f_fglob
->fg_flag
& FREAD
)
382 maxprot
|= VM_PROT_READ
;
383 else if (prot
& PROT_READ
) {
389 * If we are sharing potential changes (either via
390 * MAP_SHARED or via the implicit sharing of character
391 * device mappings), and we are trying to get write
392 * permission although we opened it without asking
396 if ((flags
& MAP_SHARED
) != 0) {
397 if ((fp
->f_fglob
->fg_flag
& FWRITE
) != 0 &&
399 * Do not allow writable mappings of
400 * swap files (see vm_swapfile_pager.c).
404 * check for write access
406 * Note that we already made this check when granting FWRITE
407 * against the file, so it seems redundant here.
409 error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_CHECKIMMUTABLE
, ctx
);
411 /* if not granted for any reason, but we wanted it, bad */
412 if ((prot
& PROT_WRITE
) && (error
!= 0)) {
417 /* if writable, remember */
419 maxprot
|= VM_PROT_WRITE
;
421 } else if ((prot
& PROT_WRITE
) != 0) {
427 maxprot
|= VM_PROT_WRITE
;
431 error
= mac_file_check_mmap(vfs_context_ucred(ctx
),
432 fp
->f_fglob
, prot
, flags
, file_pos
, &maxprot
);
441 error
= cp_handle_vnop(vp
, CP_READ_ACCESS
| CP_WRITE_ACCESS
, 0);
443 (void) vnode_put(vp
);
447 #endif /* CONFIG_PROTECT */
451 if (user_size
== 0) {
459 * We bend a little - round the start and end addresses
460 * to the nearest page boundary.
462 user_size
= vm_map_round_page(user_size
,
463 vm_map_page_mask(user_map
));
465 if (file_pos
& vm_map_page_mask(user_map
)) {
472 if ((flags
& MAP_FIXED
) == 0) {
473 alloc_flags
|= VM_FLAGS_ANYWHERE
;
474 user_addr
= vm_map_round_page(user_addr
,
475 vm_map_page_mask(user_map
));
477 if (user_addr
!= vm_map_trunc_page(user_addr
,
478 vm_map_page_mask(user_map
))) {
485 * mmap(MAP_FIXED) will replace any existing mappings in the
486 * specified range, if the new mapping is successful.
487 * If we just deallocate the specified address range here,
488 * another thread might jump in and allocate memory in that
489 * range before we get a chance to establish the new mapping,
490 * and we won't have a chance to restore the old mappings.
491 * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it
492 * has to deallocate the existing mappings and establish the
493 * new ones atomically.
495 alloc_flags
|= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
498 if (flags
& MAP_NOCACHE
)
499 alloc_flags
|= VM_FLAGS_NO_CACHE
;
501 if (flags
& MAP_JIT
) {
502 alloc_flags
|= VM_FLAGS_MAP_JIT
;
505 if (flags
& MAP_RESILIENT_CODESIGN
) {
506 alloc_flags
|= VM_FLAGS_RESILIENT_CODESIGN
;
510 * Lookup/allocate object.
512 if (handle
== NULL
) {
516 #if defined(VM_PROT_READ_IS_EXEC)
517 if (prot
& VM_PROT_READ
)
518 prot
|= VM_PROT_EXECUTE
;
519 if (maxprot
& VM_PROT_READ
)
520 maxprot
|= VM_PROT_EXECUTE
;
525 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
526 prot
|= VM_PROT_READ
;
527 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
528 maxprot
|= VM_PROT_READ
;
529 #endif /* radar 3777787 */
531 result
= vm_map_enter_mem_object(user_map
,
532 &user_addr
, user_size
,
534 IPC_PORT_NULL
, 0, FALSE
,
536 (flags
& MAP_SHARED
) ?
540 /* If a non-binding address was specified for this anonymous
541 * mapping, retry the mapping with a zero base
542 * in the event the mapping operation failed due to
543 * lack of space between the address and the map's maximum.
545 if ((result
== KERN_NO_SPACE
) && ((flags
& MAP_FIXED
) == 0) && user_addr
&& (num_retries
++ == 0)) {
546 user_addr
= vm_map_page_size(user_map
);
550 if (vnode_isswap(vp
)) {
552 * Map swap files with a special pager
553 * that returns obfuscated contents.
556 pager
= swapfile_pager_setup(vp
);
557 if (pager
!= MEMORY_OBJECT_NULL
) {
558 control
= swapfile_pager_control(pager
);
561 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
564 if (control
== NULL
) {
572 * FIXME: if we're writing the file we need a way to
573 * ensure that someone doesn't replace our R/W creds
574 * with ones that only work for read.
577 ubc_setthreadcred(vp
, p
, current_thread());
579 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0) {
585 #if defined(VM_PROT_READ_IS_EXEC)
586 if (prot
& VM_PROT_READ
)
587 prot
|= VM_PROT_EXECUTE
;
588 if (maxprot
& VM_PROT_READ
)
589 maxprot
|= VM_PROT_EXECUTE
;
594 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
595 prot
|= VM_PROT_READ
;
596 if (maxprot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
597 maxprot
|= VM_PROT_READ
;
598 #endif /* radar 3777787 */
601 if ((flags
& MAP_RESILIENT_CODESIGN
) ||
602 (flags
& MAP_RESILIENT_MEDIA
)) {
603 if (prot
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) {
609 /* strictly limit access to "prot" */
612 result
= vm_map_enter_mem_object_control(user_map
,
613 &user_addr
, user_size
,
616 docow
, prot
, maxprot
,
617 (flags
& MAP_SHARED
) ?
621 /* If a non-binding address was specified for this file backed
622 * mapping, retry the mapping with a zero base
623 * in the event the mapping operation failed due to
624 * lack of space between the address and the map's maximum.
626 if ((result
== KERN_NO_SPACE
) && ((flags
& MAP_FIXED
) == 0) && user_addr
&& (num_retries
++ == 0)) {
627 user_addr
= vm_map_page_size(user_map
);
638 *retval
= user_addr
+ pageoff
;
641 case KERN_INVALID_ADDRESS
:
645 case KERN_PROTECTION_FAILURE
:
653 if (pager
!= MEMORY_OBJECT_NULL
) {
655 * Release the reference on the pager.
656 * If the mapping was successful, it now holds
657 * an extra reference.
659 memory_object_deallocate(pager
);
662 fp_drop(p
, fd
, fp
, 0);
664 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);
665 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO2
, SYS_mmap
) | DBG_FUNC_NONE
), (uint32_t)(*retval
>> 32), (uint32_t)(user_size
>> 32),
666 (uint32_t)(file_pos
>> 32), (uint32_t)file_pos
, 0);
671 msync(__unused proc_t p
, struct msync_args
*uap
, int32_t *retval
)
673 __pthread_testcancel(1);
674 return(msync_nocancel(p
, (struct msync_nocancel_args
*)uap
, retval
));
678 msync_nocancel(__unused proc_t p
, struct msync_nocancel_args
*uap
, __unused
int32_t *retval
)
680 mach_vm_offset_t addr
;
685 vm_sync_t sync_flags
=0;
687 user_map
= current_map();
688 addr
= (mach_vm_offset_t
) uap
->addr
;
689 size
= (mach_vm_size_t
)uap
->len
;
690 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);
691 if (addr
& vm_map_page_mask(user_map
)) {
692 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
697 * We cannot support this properly without maintaining
698 * list all mmaps done. Cannot use vm_map_entry as they could be
699 * split or coalesced by indepenedant actions. So instead of
700 * inaccurate results, lets just return error as invalid size
703 return (EINVAL
); /* XXX breaks posix apps */
707 /* disallow contradictory flags */
708 if ((flags
& (MS_SYNC
|MS_ASYNC
)) == (MS_SYNC
|MS_ASYNC
))
711 if (flags
& MS_KILLPAGES
)
712 sync_flags
|= VM_SYNC_KILLPAGES
;
713 if (flags
& MS_DEACTIVATE
)
714 sync_flags
|= VM_SYNC_DEACTIVATE
;
715 if (flags
& MS_INVALIDATE
)
716 sync_flags
|= VM_SYNC_INVALIDATE
;
718 if ( !(flags
& (MS_KILLPAGES
| MS_DEACTIVATE
))) {
719 if (flags
& MS_ASYNC
)
720 sync_flags
|= VM_SYNC_ASYNCHRONOUS
;
722 sync_flags
|= VM_SYNC_SYNCHRONOUS
;
725 sync_flags
|= VM_SYNC_CONTIGUOUS
; /* complain if holes */
727 rv
= mach_vm_msync(user_map
, addr
, size
, sync_flags
);
732 case KERN_INVALID_ADDRESS
: /* hole in region being sync'ed */
744 munmap(__unused proc_t p
, struct munmap_args
*uap
, __unused
int32_t *retval
)
746 mach_vm_offset_t user_addr
;
747 mach_vm_size_t user_size
;
748 kern_return_t result
;
751 user_map
= current_map();
752 user_addr
= (mach_vm_offset_t
) uap
->addr
;
753 user_size
= (mach_vm_size_t
) uap
->len
;
755 AUDIT_ARG(addr
, user_addr
);
756 AUDIT_ARG(len
, user_size
);
758 if (user_addr
& vm_map_page_mask(user_map
)) {
759 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
763 if (user_addr
+ user_size
< user_addr
)
766 if (user_size
== 0) {
767 /* UNIX SPEC: size is 0, return EINVAL */
771 result
= mach_vm_deallocate(user_map
, user_addr
, user_size
);
772 if (result
!= KERN_SUCCESS
) {
779 mprotect(__unused proc_t p
, struct mprotect_args
*uap
, __unused
int32_t *retval
)
781 register vm_prot_t prot
;
782 mach_vm_offset_t user_addr
;
783 mach_vm_size_t user_size
;
784 kern_return_t result
;
790 AUDIT_ARG(addr
, uap
->addr
);
791 AUDIT_ARG(len
, uap
->len
);
792 AUDIT_ARG(value32
, uap
->prot
);
794 user_map
= current_map();
795 user_addr
= (mach_vm_offset_t
) uap
->addr
;
796 user_size
= (mach_vm_size_t
) uap
->len
;
797 prot
= (vm_prot_t
)(uap
->prot
& (VM_PROT_ALL
| VM_PROT_TRUSTED
));
799 if (user_addr
& vm_map_page_mask(user_map
)) {
800 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
806 #if defined(VM_PROT_READ_IS_EXEC)
807 if (prot
& VM_PROT_READ
)
808 prot
|= VM_PROT_EXECUTE
;
813 if (prot
& (VM_PROT_EXECUTE
| VM_PROT_WRITE
))
814 prot
|= VM_PROT_READ
;
819 * The MAC check for mprotect is of limited use for 2 reasons:
820 * Without mmap revocation, the caller could have asked for the max
821 * protections initially instead of a reduced set, so a mprotect
822 * check would offer no new security.
823 * It is not possible to extract the vnode from the pager object(s)
824 * of the target memory range.
825 * However, the MAC check may be used to prevent a process from,
826 * e.g., making the stack executable.
828 error
= mac_proc_check_mprotect(p
, user_addr
,
834 if(prot
& VM_PROT_TRUSTED
) {
835 #if CONFIG_DYNAMIC_CODE_SIGNING
836 /* CODE SIGNING ENFORCEMENT - JIT support */
837 /* The special protection value VM_PROT_TRUSTED requests that we treat
838 * this page as if it had a valid code signature.
839 * If this is enabled, there MUST be a MAC policy implementing the
840 * mac_proc_check_mprotect() hook above. Otherwise, Codesigning will be
841 * compromised because the check would always succeed and thusly any
842 * process could sign dynamically. */
843 result
= vm_map_sign(
845 vm_map_trunc_page(user_addr
,
846 vm_map_page_mask(user_map
)),
847 vm_map_round_page(user_addr
+user_size
,
848 vm_map_page_mask(user_map
)));
852 case KERN_INVALID_ADDRESS
:
853 /* UNIX SPEC: for an invalid address range, return ENOMEM */
862 prot
&= ~VM_PROT_TRUSTED
;
864 result
= mach_vm_protect(user_map
, user_addr
, user_size
,
869 case KERN_PROTECTION_FAILURE
:
871 case KERN_INVALID_ADDRESS
:
872 /* UNIX SPEC: for an invalid address range, return ENOMEM */
880 minherit(__unused proc_t p
, struct minherit_args
*uap
, __unused
int32_t *retval
)
882 mach_vm_offset_t addr
;
884 register vm_inherit_t inherit
;
886 kern_return_t result
;
888 AUDIT_ARG(addr
, uap
->addr
);
889 AUDIT_ARG(len
, uap
->len
);
890 AUDIT_ARG(value32
, uap
->inherit
);
892 addr
= (mach_vm_offset_t
)uap
->addr
;
893 size
= (mach_vm_size_t
)uap
->len
;
894 inherit
= uap
->inherit
;
896 user_map
= current_map();
897 result
= mach_vm_inherit(user_map
, addr
, size
,
902 case KERN_PROTECTION_FAILURE
:
909 madvise(__unused proc_t p
, struct madvise_args
*uap
, __unused
int32_t *retval
)
912 mach_vm_offset_t start
;
914 vm_behavior_t new_behavior
;
915 kern_return_t result
;
918 * Since this routine is only advisory, we default to conservative
921 switch (uap
->behav
) {
923 new_behavior
= VM_BEHAVIOR_RANDOM
;
925 case MADV_SEQUENTIAL
:
926 new_behavior
= VM_BEHAVIOR_SEQUENTIAL
;
929 new_behavior
= VM_BEHAVIOR_DEFAULT
;
932 new_behavior
= VM_BEHAVIOR_WILLNEED
;
935 new_behavior
= VM_BEHAVIOR_DONTNEED
;
938 new_behavior
= VM_BEHAVIOR_FREE
;
940 case MADV_ZERO_WIRED_PAGES
:
941 new_behavior
= VM_BEHAVIOR_ZERO_WIRED_PAGES
;
943 case MADV_FREE_REUSABLE
:
944 new_behavior
= VM_BEHAVIOR_REUSABLE
;
946 case MADV_FREE_REUSE
:
947 new_behavior
= VM_BEHAVIOR_REUSE
;
950 new_behavior
= VM_BEHAVIOR_CAN_REUSE
;
954 new_behavior
= VM_BEHAVIOR_PAGEOUT
;
956 #else /* MACH_ASSERT */
958 #endif /* MACH_ASSERT */
963 start
= (mach_vm_offset_t
) uap
->addr
;
964 size
= (mach_vm_size_t
) uap
->len
;
967 user_map
= current_map();
969 result
= mach_vm_behavior_set(user_map
, start
, size
, new_behavior
);
973 case KERN_INVALID_ADDRESS
:
983 mincore(__unused proc_t p
, struct mincore_args
*uap
, __unused
int32_t *retval
)
985 mach_vm_offset_t addr
, first_addr
, end
;
989 int vecindex
, lastvecindex
;
1000 * Make sure that the addresses presented are valid for user
1003 first_addr
= addr
= vm_map_trunc_page(uap
->addr
,
1004 vm_map_page_mask(map
));
1005 end
= addr
+ vm_map_round_page(uap
->len
,
1006 vm_map_page_mask(map
));
1012 * Address of byte vector
1016 map
= current_map();
1019 * Do this on a map entry basis so that if the pages are not
1020 * in the current processes address space, we can easily look
1021 * up the pages elsewhere.
1024 for( ; addr
< end
; addr
+= PAGE_SIZE
) {
1026 ret
= mach_vm_page_query(map
, addr
, &pqueryinfo
, &numref
);
1027 if (ret
!= KERN_SUCCESS
)
1030 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_PRESENT
)
1031 mincoreinfo
|= MINCORE_INCORE
;
1032 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_REF
)
1033 mincoreinfo
|= MINCORE_REFERENCED
;
1034 if (pqueryinfo
& VM_PAGE_QUERY_PAGE_DIRTY
)
1035 mincoreinfo
|= MINCORE_MODIFIED
;
1039 * calculate index into user supplied byte vector
1041 vecindex
= (addr
- first_addr
)>> PAGE_SHIFT
;
1044 * If we have skipped map entries, we need to make sure that
1045 * the byte vector is zeroed for those skipped entries.
1047 while((lastvecindex
+ 1) < vecindex
) {
1049 error
= copyout(&c
, vec
+ lastvecindex
, 1);
1057 * Pass the page information to the user
1059 c
= (char)mincoreinfo
;
1060 error
= copyout(&c
, vec
+ vecindex
, 1);
1064 lastvecindex
= vecindex
;
1069 * Zero the last entries in the byte vector.
1071 vecindex
= (end
- first_addr
) >> PAGE_SHIFT
;
1072 while((lastvecindex
+ 1) < vecindex
) {
1074 error
= copyout(&c
, vec
+ lastvecindex
, 1);
1085 mlock(__unused proc_t p
, struct mlock_args
*uap
, __unused
int32_t *retvalval
)
1088 vm_map_offset_t addr
;
1089 vm_map_size_t size
, pageoff
;
1090 kern_return_t result
;
1092 AUDIT_ARG(addr
, uap
->addr
);
1093 AUDIT_ARG(len
, uap
->len
);
1095 addr
= (vm_map_offset_t
) uap
->addr
;
1096 size
= (vm_map_size_t
)uap
->len
;
1098 /* disable wrap around */
1099 if (addr
+ size
< addr
)
1105 user_map
= current_map();
1106 pageoff
= (addr
& vm_map_page_mask(user_map
));
1108 size
= vm_map_round_page(size
+pageoff
, vm_map_page_mask(user_map
));
1110 /* have to call vm_map_wire directly to pass "I don't know" protections */
1111 result
= vm_map_wire(user_map
, addr
, addr
+size
, VM_PROT_NONE
| VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_MLOCK
), TRUE
);
1113 if (result
== KERN_RESOURCE_SHORTAGE
)
1115 else if (result
!= KERN_SUCCESS
)
1118 return 0; /* KERN_SUCCESS */
1122 munlock(__unused proc_t p
, struct munlock_args
*uap
, __unused
int32_t *retval
)
1124 mach_vm_offset_t addr
;
1125 mach_vm_size_t size
;
1127 kern_return_t result
;
1129 AUDIT_ARG(addr
, uap
->addr
);
1130 AUDIT_ARG(addr
, uap
->len
);
1132 addr
= (mach_vm_offset_t
) uap
->addr
;
1133 size
= (mach_vm_size_t
)uap
->len
;
1134 user_map
= current_map();
1136 /* JMM - need to remove all wirings by spec - this just removes one */
1137 result
= mach_vm_wire(host_priv_self(), user_map
, addr
, size
, VM_PROT_NONE
);
1138 return (result
== KERN_SUCCESS
? 0 : ENOMEM
);
1143 mlockall(__unused proc_t p
, __unused
struct mlockall_args
*uap
, __unused
int32_t *retval
)
1149 munlockall(__unused proc_t p
, __unused
struct munlockall_args
*uap
, __unused
int32_t *retval
)
1154 #if CONFIG_CODE_DECRYPTION
1156 mremap_encrypted(__unused
struct proc
*p
, struct mremap_encrypted_args
*uap
, __unused
int32_t *retval
)
1158 mach_vm_offset_t user_addr
;
1159 mach_vm_size_t user_size
;
1160 kern_return_t result
;
1164 cpu_subtype_t cpusubtype
;
1165 pager_crypt_info_t crypt_info
;
1166 const char * cryptname
= 0;
1169 struct proc_regioninfo_internal pinfo
;
1171 uintptr_t vnodeaddr
;
1174 AUDIT_ARG(addr
, uap
->addr
);
1175 AUDIT_ARG(len
, uap
->len
);
1177 user_map
= current_map();
1178 user_addr
= (mach_vm_offset_t
) uap
->addr
;
1179 user_size
= (mach_vm_size_t
) uap
->len
;
1181 cryptid
= uap
->cryptid
;
1182 cputype
= uap
->cputype
;
1183 cpusubtype
= uap
->cpusubtype
;
1185 if (user_addr
& vm_map_page_mask(user_map
)) {
1186 /* UNIX SPEC: user address is not page-aligned, return EINVAL */
1192 /* not encrypted, just an empty load command */
1195 cryptname
="com.apple.unfree";
1198 /* some random cryptid that you could manually put into
1199 * your binary if you want NULL */
1200 cryptname
="com.apple.null";
1206 if (NULL
== text_crypter_create
) return ENOTSUP
;
1208 ret
= fill_procregioninfo_onlymappedvnodes( proc_task(p
), user_addr
, &pinfo
, &vnodeaddr
, &vid
);
1209 if (ret
== 0 || !vnodeaddr
) {
1210 /* No really, this returns 0 if the memory address is not backed by a file */
1214 vp
= (vnode_t
)vnodeaddr
;
1215 if ((vnode_getwithvid(vp
, vid
)) == 0) {
1216 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1223 ret
= vn_getpath(vp
, vpath
, &len
);
1225 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1236 kprintf("%s vpath %s cryptid 0x%08x cputype 0x%08x cpusubtype 0x%08x range 0x%016llx size 0x%016llx\n",
1237 __FUNCTION__
, vpath
, cryptid
, cputype
, cpusubtype
, (uint64_t)user_addr
, (uint64_t)user_size
);
1240 /* set up decrypter first */
1241 crypt_file_data_t crypt_data
= {
1244 .cpusubtype
= cpusubtype
};
1245 result
= text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
1246 #if DEVELOPMENT || DEBUG
1247 printf("APPLE_PROTECT: %d[%s] map %p [0x%llx:0x%llx] %s(%s) -> 0x%x\n",
1248 p
->p_pid
, p
->p_comm
,
1249 user_map
, (uint64_t) user_addr
, (uint64_t) (user_addr
+ user_size
),
1250 __FUNCTION__
, vpath
, result
);
1251 #endif /* DEVELOPMENT || DEBUG */
1252 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1255 printf("%s: unable to create decrypter %s, kr=%d\n",
1256 __FUNCTION__
, cryptname
, result
);
1257 if (result
== kIOReturnNotPrivileged
) {
1258 /* text encryption returned decryption failure */
1265 /* now remap using the decrypter */
1266 vm_object_offset_t crypto_backing_offset
;
1267 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
1268 result
= vm_map_apple_protected(user_map
,
1270 user_addr
+user_size
,
1271 crypto_backing_offset
,
1274 printf("%s: mapping failed with %d\n", __FUNCTION__
, result
);
1282 #endif /* CONFIG_CODE_DECRYPTION */