2 * Copyright (c) 1999-2008 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@
30 * Author: Umesh Vaishampayan [umeshv@apple.com]
31 * 05-Aug-1999 umeshv Created.
33 * Functions related to Unified Buffer cache.
35 * Caller of UBC functions MUST have a valid reference on the vnode.
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/mount_internal.h>
45 #include <sys/vnode_internal.h>
46 #include <sys/ubc_internal.h>
47 #include <sys/ucred.h>
48 #include <sys/proc_internal.h>
49 #include <sys/kauth.h>
52 #include <sys/codesign.h>
54 #include <mach/mach_types.h>
55 #include <mach/memory_object_types.h>
56 #include <mach/memory_object_control.h>
57 #include <mach/vm_map.h>
58 #include <mach/mach_vm.h>
61 #include <kern/kern_types.h>
62 #include <kern/kalloc.h>
63 #include <kern/zalloc.h>
64 #include <kern/thread.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_protos.h> /* last */
68 #include <libkern/crypto/sha1.h>
70 #include <security/mac_framework.h>
72 /* XXX These should be in a BSD accessible Mach header, but aren't. */
73 extern kern_return_t
memory_object_pages_resident(memory_object_control_t
,
75 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
77 extern void Debugger(const char *message
);
80 /* XXX no one uses this interface! */
81 kern_return_t
ubc_page_op_with_control(
82 memory_object_control_t control
,
93 #define assert(cond) \
94 ((void) ((cond) ? 0 : panic("Assert failed: %s", # cond)))
96 #include <kern/assert.h>
97 #endif /* DIAGNOSTIC */
99 static int ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
);
100 static int ubc_umcallback(vnode_t
, void *);
101 static int ubc_msync_internal(vnode_t
, off_t
, off_t
, off_t
*, int, int *);
102 static void ubc_cs_free(struct ubc_info
*uip
);
104 struct zone
*ubc_info_zone
;
109 * Routines to navigate code signing data structures in the kernel...
118 const void *lower_bound
,
119 const void *upper_bound
)
121 if (upper_bound
< lower_bound
||
126 if (start
< lower_bound
||
135 * Magic numbers used by Code Signing
138 CSMAGIC_REQUIREMENT
= 0xfade0c00, /* single Requirement blob */
139 CSMAGIC_REQUIREMENTS
= 0xfade0c01, /* Requirements vector (internal requirements) */
140 CSMAGIC_CODEDIRECTORY
= 0xfade0c02, /* CodeDirectory blob */
141 CSMAGIC_EMBEDDED_SIGNATURE
= 0xfade0cc0, /* embedded form of signature data */
142 CSMAGIC_EMBEDDED_SIGNATURE_OLD
= 0xfade0b02, /* XXX */
143 CSMAGIC_DETACHED_SIGNATURE
= 0xfade0cc1, /* multi-arch collection of embedded signatures */
145 CSSLOT_CODEDIRECTORY
= 0, /* slot index for CodeDirectory */
148 static const uint32_t supportsScatter
= 0x20100; // first version to support scatter option
151 * Structure of an embedded-signature SuperBlob
153 typedef struct __BlobIndex
{
154 uint32_t type
; /* type of entry */
155 uint32_t offset
; /* offset of entry */
158 typedef struct __SuperBlob
{
159 uint32_t magic
; /* magic number */
160 uint32_t length
; /* total length of SuperBlob */
161 uint32_t count
; /* number of index entries following */
162 CS_BlobIndex index
[]; /* (count) entries */
163 /* followed by Blobs in no particular order as indicated by offsets in index */
167 uint32_t count
; // number of pages; zero for sentinel (only)
168 uint32_t base
; // first page number
169 uint64_t targetOffset
; // offset in target
170 uint64_t spare
; // reserved
174 * C form of a CodeDirectory.
176 typedef struct __CodeDirectory
{
177 uint32_t magic
; /* magic number (CSMAGIC_CODEDIRECTORY) */
178 uint32_t length
; /* total length of CodeDirectory blob */
179 uint32_t version
; /* compatibility version */
180 uint32_t flags
; /* setup and mode flags */
181 uint32_t hashOffset
; /* offset of hash slot element at index zero */
182 uint32_t identOffset
; /* offset of identifier string */
183 uint32_t nSpecialSlots
; /* number of special hash slots */
184 uint32_t nCodeSlots
; /* number of ordinary (code) hash slots */
185 uint32_t codeLimit
; /* limit to main image signature range */
186 uint8_t hashSize
; /* size of each hash in bytes */
187 uint8_t hashType
; /* type of hash (cdHashType* constants) */
188 uint8_t spare1
; /* unused (must be zero) */
189 uint8_t pageSize
; /* log2(page size in bytes); 0 => infinite */
190 uint32_t spare2
; /* unused (must be zero) */
191 /* Version 0x20100 */
192 uint32_t scatterOffset
; /* offset of optional scatter vector */
193 /* followed by dynamic content as located by offset fields above */
198 * Locate the CodeDirectory from an embedded signature blob
201 CS_CodeDirectory
*findCodeDirectory(
202 const CS_SuperBlob
*embedded
,
206 const CS_CodeDirectory
*cd
= NULL
;
209 cs_valid_range(embedded
, embedded
+ 1, lower_bound
, upper_bound
) &&
210 ntohl(embedded
->magic
) == CSMAGIC_EMBEDDED_SIGNATURE
) {
211 const CS_BlobIndex
*limit
;
212 const CS_BlobIndex
*p
;
214 limit
= &embedded
->index
[ntohl(embedded
->count
)];
215 if (!cs_valid_range(&embedded
->index
[0], limit
,
216 lower_bound
, upper_bound
)) {
219 for (p
= embedded
->index
; p
< limit
; ++p
) {
220 if (ntohl(p
->type
) == CSSLOT_CODEDIRECTORY
) {
221 const unsigned char *base
;
223 base
= (const unsigned char *)embedded
;
224 cd
= (const CS_CodeDirectory
*)(base
+ ntohl(p
->offset
));
230 * Detached signatures come as a bare CS_CodeDirectory,
233 cd
= (const CS_CodeDirectory
*) embedded
;
237 cs_valid_range(cd
, cd
+ 1, lower_bound
, upper_bound
) &&
238 cs_valid_range(cd
, (const char *) cd
+ ntohl(cd
->length
),
239 lower_bound
, upper_bound
) &&
240 cs_valid_range(cd
, (const char *) cd
+ ntohl(cd
->hashOffset
),
241 lower_bound
, upper_bound
) &&
242 cs_valid_range(cd
, (const char *) cd
+
243 ntohl(cd
->hashOffset
) +
244 (ntohl(cd
->nCodeSlots
) * SHA1_RESULTLEN
),
245 lower_bound
, upper_bound
) &&
247 ntohl(cd
->magic
) == CSMAGIC_CODEDIRECTORY
) {
251 // not found or not a valid code directory
257 * Locating a page hash
259 static const unsigned char *
261 const CS_CodeDirectory
*cd
,
266 const unsigned char *base
, *top
, *hash
;
267 uint32_t nCodeSlots
= ntohl(cd
->nCodeSlots
);
269 assert(cs_valid_range(cd
, cd
+ 1, lower_bound
, upper_bound
));
271 if((ntohl(cd
->version
) >= supportsScatter
) && (ntohl(cd
->scatterOffset
))) {
272 /* Get first scatter struct */
273 const struct Scatter
*scatter
= (const struct Scatter
*)
274 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
275 uint32_t hashindex
=0, scount
, sbase
=0;
276 /* iterate all scatter structs */
278 if((const char*)scatter
> (const char*)cd
+ ntohl(cd
->length
)) {
280 printf("CODE SIGNING: Scatter extends past Code Directory\n");
285 scount
= ntohl(scatter
->count
);
286 uint32_t new_base
= ntohl(scatter
->base
);
293 if((hashindex
> 0) && (new_base
<= sbase
)) {
295 printf("CODE SIGNING: unordered Scatter, prev base %d, cur base %d\n",
298 return NULL
; /* unordered scatter array */
302 /* this scatter beyond page we're looking for? */
307 if (sbase
+scount
>= page
) {
308 /* Found the scatter struct that is
309 * referencing our page */
311 /* base = address of first hash covered by scatter */
312 base
= (const unsigned char *)cd
+ ntohl(cd
->hashOffset
) +
313 hashindex
* SHA1_RESULTLEN
;
314 /* top = address of first hash after this scatter */
315 top
= base
+ scount
* SHA1_RESULTLEN
;
316 if (!cs_valid_range(base
, top
, lower_bound
,
318 hashindex
> nCodeSlots
) {
325 /* this scatter struct is before the page we're looking
331 hash
= base
+ (page
- sbase
) * SHA1_RESULTLEN
;
333 base
= (const unsigned char *)cd
+ ntohl(cd
->hashOffset
);
334 top
= base
+ nCodeSlots
* SHA1_RESULTLEN
;
335 if (!cs_valid_range(base
, top
, lower_bound
, upper_bound
) ||
339 assert(page
< nCodeSlots
);
341 hash
= base
+ page
* SHA1_RESULTLEN
;
344 if (!cs_valid_range(hash
, hash
+ SHA1_RESULTLEN
,
345 lower_bound
, upper_bound
)) {
353 * End of routines to navigate code signing data structures in the kernel.
360 * Initialization of the zone for Unified Buffer Cache.
367 * ubc_info_zone(global) initialized for subsequent allocations
369 __private_extern__
void
374 i
= (vm_size_t
) sizeof (struct ubc_info
);
376 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
383 * Allocate and attach an empty ubc_info structure to a vnode
385 * Parameters: vp Pointer to the vnode
388 * vnode_size:ENOMEM Not enough space
389 * vnode_size:??? Other error from vnode_getattr
393 ubc_info_init(struct vnode
*vp
)
395 return(ubc_info_init_internal(vp
, 0, 0));
400 * ubc_info_init_withsize
402 * Allocate and attach a sized ubc_info structure to a vnode
404 * Parameters: vp Pointer to the vnode
405 * filesize The size of the file
408 * vnode_size:ENOMEM Not enough space
409 * vnode_size:??? Other error from vnode_getattr
412 ubc_info_init_withsize(struct vnode
*vp
, off_t filesize
)
414 return(ubc_info_init_internal(vp
, 1, filesize
));
419 * ubc_info_init_internal
421 * Allocate and attach a ubc_info structure to a vnode
423 * Parameters: vp Pointer to the vnode
424 * withfsize{0,1} Zero if the size should be obtained
425 * from the vnode; otherwise, use filesize
426 * filesize The size of the file, if withfsize == 1
429 * vnode_size:ENOMEM Not enough space
430 * vnode_size:??? Other error from vnode_getattr
432 * Notes: We call a blocking zalloc(), and the zone was created as an
433 * expandable and collectable zone, so if no memory is available,
434 * it is possible for zalloc() to block indefinitely. zalloc()
435 * may also panic if the zone of zones is exhausted, since it's
438 * We unconditionally call vnode_pager_setup(), even if this is
439 * a reuse of a ubc_info; in that case, we should probably assert
440 * that it does not already have a pager association, but do not.
442 * Since memory_object_create_named() can only fail from receiving
443 * an invalid pager argument, the explicit check and panic is
444 * merely precautionary.
447 ubc_info_init_internal(vnode_t vp
, int withfsize
, off_t filesize
)
449 register struct ubc_info
*uip
;
453 memory_object_control_t control
;
458 * If there is not already a ubc_info attached to the vnode, we
459 * attach one; otherwise, we will reuse the one that's there.
461 if (uip
== UBC_INFO_NULL
) {
463 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
464 bzero((char *)uip
, sizeof(struct ubc_info
));
467 uip
->ui_flags
= UI_INITED
;
468 uip
->ui_ucred
= NOCRED
;
470 assert(uip
->ui_flags
!= UI_NONE
);
471 assert(uip
->ui_vnode
== vp
);
473 /* now set this ubc_info in the vnode */
477 * Allocate a pager object for this vnode
479 * XXX The value of the pager parameter is currently ignored.
480 * XXX Presumably, this API changed to avoid the race between
481 * XXX setting the pager and the UI_HASPAGER flag.
483 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
487 * Explicitly set the pager into the ubc_info, after setting the
490 SET(uip
->ui_flags
, UI_HASPAGER
);
491 uip
->ui_pager
= pager
;
494 * Note: We can not use VNOP_GETATTR() to get accurate
495 * value of ui_size because this may be an NFS vnode, and
496 * nfs_getattr() can call vinvalbuf(); if this happens,
497 * ubc_info is not set up to deal with that event.
502 * create a vnode - vm_object association
503 * memory_object_create_named() creates a "named" reference on the
504 * memory object we hold this reference as long as the vnode is
505 * "alive." Since memory_object_create_named() took its own reference
506 * on the vnode pager we passed it, we can drop the reference
507 * vnode_pager_setup() returned here.
509 kret
= memory_object_create_named(pager
,
510 (memory_object_size_t
)uip
->ui_size
, &control
);
511 vnode_pager_deallocate(pager
);
512 if (kret
!= KERN_SUCCESS
)
513 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
516 uip
->ui_control
= control
; /* cache the value of the mo control */
517 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
519 if (withfsize
== 0) {
520 /* initialize the size */
521 error
= vnode_size(vp
, &uip
->ui_size
, vfs_context_current());
525 uip
->ui_size
= filesize
;
527 vp
->v_lflag
|= VNAMED_UBC
; /* vnode has a named ubc reference */
536 * Free a ubc_info structure
538 * Parameters: uip A pointer to the ubc_info to free
542 * Notes: If there is a credential that has subsequently been associated
543 * with the ubc_info via a call to ubc_setcred(), the reference
544 * to the credential is dropped.
546 * It's actually impossible for a ubc_info.ui_control to take the
547 * value MEMORY_OBJECT_CONTROL_NULL.
550 ubc_info_free(struct ubc_info
*uip
)
552 if (IS_VALID_CRED(uip
->ui_ucred
)) {
553 kauth_cred_unref(&uip
->ui_ucred
);
556 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
557 memory_object_control_deallocate(uip
->ui_control
);
559 cluster_release(uip
);
562 zfree(ubc_info_zone
, uip
);
568 ubc_info_deallocate(struct ubc_info
*uip
)
577 * Tell the VM that the the size of the file represented by the vnode has
580 * Parameters: vp The vp whose backing file size is
582 * nsize The new size of the backing file
587 * Notes: This function will indicate failure if the new size that's
588 * being attempted to be set is negative.
590 * This function will fail if there is no ubc_info currently
591 * associated with the vnode.
593 * This function will indicate success it the new size is the
594 * same or larger than the old size (in this case, the remainder
595 * of the file will require modification or use of an existing upl
596 * to access successfully).
598 * This function will fail if the new file size is smaller, and
599 * the memory region being invalidated was unable to actually be
600 * invalidated and/or the last page could not be flushed, if the
601 * new size is not aligned to a page boundary. This is usually
602 * indicative of an I/O error.
605 ubc_setsize(struct vnode
*vp
, off_t nsize
)
607 off_t osize
; /* ui_size before change */
608 off_t lastpg
, olastpgend
, lastoff
;
609 struct ubc_info
*uip
;
610 memory_object_control_t control
;
611 kern_return_t kret
= KERN_SUCCESS
;
613 if (nsize
< (off_t
)0)
616 if (!UBCINFOEXISTS(vp
))
620 osize
= uip
->ui_size
;
622 * Update the size before flushing the VM
624 uip
->ui_size
= nsize
;
626 if (nsize
>= osize
) { /* Nothing more to do */
627 lock_vnode_and_post(vp
, NOTE_EXTEND
);
628 return (1); /* return success */
632 * When the file shrinks, invalidate the pages beyond the
633 * new size. Also get rid of garbage beyond nsize on the
634 * last page. The ui_size already has the nsize, so any
635 * subsequent page-in will zero-fill the tail properly
637 lastpg
= trunc_page_64(nsize
);
638 olastpgend
= round_page_64(osize
);
639 control
= uip
->ui_control
;
641 lastoff
= (nsize
& PAGE_MASK_64
);
649 * new EOF ends up in the middle of a page
650 * zero the tail of this page if its currently
651 * present in the cache
653 kret
= ubc_create_upl(vp
, lastpg
, PAGE_SIZE
, &upl
, &pl
, UPL_SET_LITE
);
655 if (kret
!= KERN_SUCCESS
)
656 panic("ubc_setsize: ubc_create_upl (error = %d)\n", kret
);
658 if (upl_valid_page(pl
, 0))
659 cluster_zero(upl
, (uint32_t)lastoff
, PAGE_SIZE
- (uint32_t)lastoff
, NULL
);
661 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
663 lastpg
+= PAGE_SIZE_64
;
665 if (olastpgend
> lastpg
) {
669 flags
= MEMORY_OBJECT_DATA_FLUSH_ALL
;
671 flags
= MEMORY_OBJECT_DATA_FLUSH
;
673 * invalidate the pages beyond the new EOF page
676 kret
= memory_object_lock_request(control
,
677 (memory_object_offset_t
)lastpg
,
678 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
679 MEMORY_OBJECT_RETURN_NONE
, flags
, VM_PROT_NO_CHANGE
);
680 if (kret
!= KERN_SUCCESS
)
681 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
683 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
690 * Get the size of the file assocated with the specified vnode
692 * Parameters: vp The vnode whose size is of interest
694 * Returns: 0 There is no ubc_info associated with
695 * this vnode, or the size is zero
696 * !0 The size of the file
698 * Notes: Using this routine, it is not possible for a caller to
699 * successfully distinguish between a vnode associate with a zero
700 * length file, and a vnode with no associated ubc_info. The
701 * caller therefore needs to not care, or needs to ensure that
702 * they have previously successfully called ubc_info_init() or
703 * ubc_info_init_withsize().
706 ubc_getsize(struct vnode
*vp
)
708 /* people depend on the side effect of this working this way
709 * as they call this for directory
711 if (!UBCINFOEXISTS(vp
))
713 return (vp
->v_ubcinfo
->ui_size
);
720 * Call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes for this
723 * Parameters: mp The mount point
727 * Notes: There is no failure indication for this function.
729 * This function is used in the unmount path; since it may block
730 * I/O indefinitely, it should not be used in the forced unmount
731 * path, since a device unavailability could also block that
734 * Because there is no device ejection interlock on USB, FireWire,
735 * or similar devices, it's possible that an ejection that begins
736 * subsequent to the vnode_iterate() completing, either on one of
737 * those devices, or a network mount for which the server quits
738 * responding, etc., may cause the caller to block indefinitely.
740 __private_extern__
int
741 ubc_umount(struct mount
*mp
)
743 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
751 * Used by ubc_umount() as an internal implementation detail; see ubc_umount()
752 * and vnode_iterate() for details of implementation.
755 ubc_umcallback(vnode_t vp
, __unused
void * args
)
758 if (UBCINFOEXISTS(vp
)) {
760 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
762 return (VNODE_RETURNED
);
769 * Get the credentials currently active for the ubc_info associated with the
772 * Parameters: vp The vnode whose ubc_info credentials
773 * are to be retrieved
775 * Returns: !NOCRED The credentials
776 * NOCRED If there is no ubc_info for the vnode,
777 * or if there is one, but it has not had
778 * any credentials associated with it via
779 * a call to ubc_setcred()
782 ubc_getcred(struct vnode
*vp
)
784 if (UBCINFOEXISTS(vp
))
785 return (vp
->v_ubcinfo
->ui_ucred
);
794 * If they are not already set, set the credentials of the ubc_info structure
795 * associated with the vnode to those of the supplied thread; otherwise leave
798 * Parameters: vp The vnode whose ubc_info creds are to
800 * p The process whose credentials are to
801 * be used, if not running on an assumed
803 * thread The thread whose credentials are to
806 * Returns: 1 This vnode has no associated ubc_info
809 * Notes: This function takes a proc parameter to account for bootstrap
810 * issues where a task or thread may call this routine, either
811 * before credentials have been initialized by bsd_init(), or if
812 * there is no BSD info asscoiate with a mach thread yet. This
813 * is known to happen in both the initial swap and memory mapping
816 * This function is generally used only in the following cases:
818 * o a memory mapped file via the mmap() system call
819 * o a memory mapped file via the deprecated map_fd() call
820 * o a swap store backing file
821 * o subsequent to a successful write via vn_write()
823 * The information is then used by the NFS client in order to
824 * cons up a wire message in either the page-in or page-out path.
826 * There are two potential problems with the use of this API:
828 * o Because the write path only set it on a successful
829 * write, there is a race window between setting the
830 * credential and its use to evict the pages to the
833 * o Because a page-in may occur prior to a write, the
834 * credential may not be set at this time, if the page-in
835 * is not the result of a mapping established via mmap()
838 * In both these cases, this will be triggered from the paging
839 * path, which will instead use the credential of the current
840 * process, which in this case is either the dynamic_pager or
841 * the kernel task, both of which utilize "root" credentials.
843 * This may potentially permit operations to occur which should
844 * be denied, or it may cause to be denied operations which
845 * should be permitted, depending on the configuration of the NFS
849 ubc_setthreadcred(struct vnode
*vp
, proc_t p
, thread_t thread
)
851 struct ubc_info
*uip
;
853 struct uthread
*uthread
= get_bsdthread_info(thread
);
855 if (!UBCINFOEXISTS(vp
))
861 credp
= uip
->ui_ucred
;
863 if (!IS_VALID_CRED(credp
)) {
864 /* use per-thread cred, if assumed identity, else proc cred */
865 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
866 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
868 uip
->ui_ucred
= uthread
->uu_ucred
;
869 kauth_cred_ref(uip
->ui_ucred
);
881 * If they are not already set, set the credentials of the ubc_info structure
882 * associated with the vnode to those of the process; otherwise leave them
885 * Parameters: vp The vnode whose ubc_info creds are to
887 * p The process whose credentials are to
890 * Returns: 0 This vnode has no associated ubc_info
893 * Notes: The return values for this function are inverted from nearly
894 * all other uses in the kernel.
896 * See also ubc_setthreadcred(), above.
898 * This function is considered deprecated, and generally should
899 * not be used, as it is incompatible with per-thread credentials;
900 * it exists for legacy KPI reasons.
902 * DEPRECATION: ubc_setcred() is being deprecated. Please use
903 * ubc_setthreadcred() instead.
906 ubc_setcred(struct vnode
*vp
, proc_t p
)
908 struct ubc_info
*uip
;
911 /* If there is no ubc_info, deny the operation */
912 if ( !UBCINFOEXISTS(vp
))
916 * Check to see if there is already a credential reference in the
917 * ubc_info; if there is not, take one on the supplied credential.
921 credp
= uip
->ui_ucred
;
922 if (!IS_VALID_CRED(credp
)) {
923 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
933 * Get the pager associated with the ubc_info associated with the vnode.
935 * Parameters: vp The vnode to obtain the pager from
937 * Returns: !VNODE_PAGER_NULL The memory_object_t for the pager
938 * VNODE_PAGER_NULL There is no ubc_info for this vnode
940 * Notes: For each vnode that has a ubc_info associated with it, that
941 * ubc_info SHALL have a pager associated with it, so in the
942 * normal case, it's impossible to return VNODE_PAGER_NULL for
943 * a vnode with an associated ubc_info.
945 __private_extern__ memory_object_t
946 ubc_getpager(struct vnode
*vp
)
948 if (UBCINFOEXISTS(vp
))
949 return (vp
->v_ubcinfo
->ui_pager
);
958 * Get the memory object control associated with the ubc_info associated with
961 * Parameters: vp The vnode to obtain the memory object
965 * Returns: !MEMORY_OBJECT_CONTROL_NULL
966 * MEMORY_OBJECT_CONTROL_NULL
968 * Notes: Historically, if the flags were not "do not reactivate", this
969 * function would look up the memory object using the pager if
970 * it did not exist (this could be the case if the vnode had
971 * been previously reactivated). The flags would also permit a
972 * hold to be requested, which would have created an object
973 * reference, if one had not already existed. This usage is
974 * deprecated, as it would permit a race between finding and
975 * taking the reference vs. a single reference being dropped in
978 memory_object_control_t
979 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
981 if (UBCINFOEXISTS(vp
))
982 return((vp
->v_ubcinfo
->ui_control
));
984 return (MEMORY_OBJECT_CONTROL_NULL
);
991 * Convert a given block number to a memory backing object (file) offset for a
994 * Parameters: vp The vnode in which the block is located
995 * blkno The block number to convert
997 * Returns: !-1 The offset into the backing object
998 * -1 There is no ubc_info associated with
1000 * -1 An error occurred in the underlying VFS
1001 * while translating the block to an
1002 * offset; the most likely cause is that
1003 * the caller specified a block past the
1004 * end of the file, but this could also be
1005 * any other error from VNOP_BLKTOOFF().
1007 * Note: Representing the error in band loses some information, but does
1008 * not occlude a valid offset, since an off_t of -1 is normally
1009 * used to represent EOF. If we had a more reliable constant in
1010 * our header files for it (i.e. explicitly cast to an off_t), we
1011 * would use it here instead.
1014 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
1016 off_t file_offset
= -1;
1019 if (UBCINFOEXISTS(vp
)) {
1020 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
1025 return (file_offset
);
1032 * Convert a given offset in a memory backing object into a block number for a
1035 * Parameters: vp The vnode in which the offset is
1037 * offset The offset into the backing object
1039 * Returns: !-1 The returned block number
1040 * -1 There is no ubc_info associated with
1042 * -1 An error occurred in the underlying VFS
1043 * while translating the block to an
1044 * offset; the most likely cause is that
1045 * the caller specified a block past the
1046 * end of the file, but this could also be
1047 * any other error from VNOP_OFFTOBLK().
1049 * Note: Representing the error in band loses some information, but does
1050 * not occlude a valid block number, since block numbers exceed
1051 * the valid range for offsets, due to their relative sizes. If
1052 * we had a more reliable constant than -1 in our header files
1053 * for it (i.e. explicitly cast to an daddr64_t), we would use it
1057 ubc_offtoblk(vnode_t vp
, off_t offset
)
1059 daddr64_t blkno
= -1;
1062 if (UBCINFOEXISTS(vp
)) {
1063 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
1073 * ubc_pages_resident
1075 * Determine whether or not a given vnode has pages resident via the memory
1076 * object control associated with the ubc_info associated with the vnode
1078 * Parameters: vp The vnode we want to know about
1084 ubc_pages_resident(vnode_t vp
)
1087 boolean_t has_pages_resident
;
1089 if (!UBCINFOEXISTS(vp
))
1093 * The following call may fail if an invalid ui_control is specified,
1094 * or if there is no VM object associated with the control object. In
1095 * either case, reacting to it as if there were no pages resident will
1096 * result in correct behavior.
1098 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
1100 if (kret
!= KERN_SUCCESS
)
1103 if (has_pages_resident
== TRUE
)
1113 * Clean and/or invalidate a range in the memory object that backs this vnode
1115 * Parameters: vp The vnode whose associated ubc_info's
1116 * associated memory object is to have a
1117 * range invalidated within it
1118 * beg_off The start of the range, as an offset
1119 * end_off The end of the range, as an offset
1120 * flags See ubc_msync_internal()
1122 * Returns: 1 Success
1125 * Notes: see ubc_msync_internal() for more detailed information.
1127 * DEPRECATED: This interface is obsolete due to a failure to return error
1128 * information needed in order to correct failures. The currently
1129 * recommended interface is ubc_msync().
1132 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
1134 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
1141 * Clean and/or invalidate a range in the memory object that backs this vnode
1143 * Parameters: vp The vnode whose associated ubc_info's
1144 * associated memory object is to have a
1145 * range invalidated within it
1146 * beg_off The start of the range, as an offset
1147 * end_off The end of the range, as an offset
1148 * resid_off The address of an off_t supplied by the
1149 * caller; may be set to NULL to ignore
1150 * flags See ubc_msync_internal()
1152 * Returns: 0 Success
1153 * !0 Failure; an errno is returned
1156 * *resid_off, modified If non-NULL, the contents are ALWAYS
1157 * modified; they are initialized to the
1158 * beg_off, and in case of an I/O error,
1159 * the difference between beg_off and the
1160 * current value will reflect what was
1161 * able to be written before the error
1162 * occurred. If no error is returned, the
1163 * value of the resid_off is undefined; do
1164 * NOT use it in place of end_off if you
1165 * intend to increment from the end of the
1166 * last call and call iteratively.
1168 * Notes: see ubc_msync_internal() for more detailed information.
1172 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
1178 *resid_off
= beg_off
;
1180 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
1182 if (retval
== 0 && io_errno
== 0)
1189 * Clean and/or invalidate a range in the memory object that backs this vnode
1191 * Parameters: vp The vnode whose associated ubc_info's
1192 * associated memory object is to have a
1193 * range invalidated within it
1194 * beg_off The start of the range, as an offset
1195 * end_off The end of the range, as an offset
1196 * resid_off The address of an off_t supplied by the
1197 * caller; may be set to NULL to ignore
1198 * flags MUST contain at least one of the flags
1199 * UBC_INVALIDATE, UBC_PUSHDIRTY, or
1200 * UBC_PUSHALL; if UBC_PUSHDIRTY is used,
1201 * UBC_SYNC may also be specified to cause
1202 * this function to block until the
1203 * operation is complete. The behavior
1204 * of UBC_SYNC is otherwise undefined.
1205 * io_errno The address of an int to contain the
1206 * errno from a failed I/O operation, if
1207 * one occurs; may be set to NULL to
1210 * Returns: 1 Success
1214 * *resid_off, modified The contents of this offset MAY be
1215 * modified; in case of an I/O error, the
1216 * difference between beg_off and the
1217 * current value will reflect what was
1218 * able to be written before the error
1220 * *io_errno, modified The contents of this offset are set to
1221 * an errno, if an error occurs; if the
1222 * caller supplies an io_errno parameter,
1223 * they should be careful to initialize it
1224 * to 0 before calling this function to
1225 * enable them to distinguish an error
1226 * with a valid *resid_off from an invalid
1227 * one, and to avoid potentially falsely
1228 * reporting an error, depending on use.
1230 * Notes: If there is no ubc_info associated with the vnode supplied,
1231 * this function immediately returns success.
1233 * If the value of end_off is less than or equal to beg_off, this
1234 * function immediately returns success; that is, end_off is NOT
1237 * IMPORTANT: one of the flags UBC_INVALIDATE, UBC_PUSHDIRTY, or
1238 * UBC_PUSHALL MUST be specified; that is, it is NOT possible to
1239 * attempt to block on in-progress I/O by calling this function
1240 * with UBC_PUSHDIRTY, and then later call it with just UBC_SYNC
1241 * in order to block pending on the I/O already in progress.
1243 * The start offset is truncated to the page boundary and the
1244 * size is adjusted to include the last page in the range; that
1245 * is, end_off on exactly a page boundary will not change if it
1246 * is rounded, and the range of bytes written will be from the
1247 * truncate beg_off to the rounded (end_off - 1).
1250 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
1252 memory_object_size_t tsize
;
1254 int request_flags
= 0;
1255 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
1257 if ( !UBCINFOEXISTS(vp
))
1259 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
1261 if (end_off
<= beg_off
)
1264 if (flags
& UBC_INVALIDATE
)
1266 * discard the resident pages
1268 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
1270 if (flags
& UBC_SYNC
)
1272 * wait for all the I/O to complete before returning
1274 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
1276 if (flags
& UBC_PUSHDIRTY
)
1278 * we only return the dirty pages in the range
1280 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
1282 if (flags
& UBC_PUSHALL
)
1284 * then return all the interesting pages in the range (both
1285 * dirty and precious) to the pager
1287 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
1289 beg_off
= trunc_page_64(beg_off
);
1290 end_off
= round_page_64(end_off
);
1291 tsize
= (memory_object_size_t
)end_off
- beg_off
;
1293 /* flush and/or invalidate pages in the range requested */
1294 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
1296 (memory_object_offset_t
*)resid_off
,
1297 io_errno
, flush_flags
, request_flags
,
1300 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
1305 * ubc_msync_internal
1307 * Explicitly map a vnode that has an associate ubc_info, and add a reference
1308 * to it for the ubc system, if there isn't one already, so it will not be
1309 * recycled while it's in use, and set flags on the ubc_info to indicate that
1312 * Parameters: vp The vnode to map
1313 * flags The mapping flags for the vnode; this
1314 * will be a combination of one or more of
1315 * PROT_READ, PROT_WRITE, and PROT_EXEC
1317 * Returns: 0 Success
1318 * EPERM Permission was denied
1320 * Notes: An I/O reference on the vnode must already be held on entry
1322 * If there is no ubc_info associated with the vnode, this function
1323 * will return success.
1325 * If a permission error occurs, this function will return
1326 * failure; all other failures will cause this function to return
1329 * IMPORTANT: This is an internal use function, and its symbols
1330 * are not exported, hence its error checking is not very robust.
1331 * It is primarily used by:
1333 * o mmap(), when mapping a file
1334 * o The deprecated map_fd() interface, when mapping a file
1335 * o When mapping a shared file (a shared library in the
1336 * shared segment region)
1337 * o When loading a program image during the exec process
1339 * ...all of these uses ignore the return code, and any fault that
1340 * results later because of a failure is handled in the fix-up path
1341 * of the fault handler. The interface exists primarily as a
1344 * Given that third party implementation of the type of interfaces
1345 * that would use this function, such as alternative executable
1346 * formats, etc., are unsupported, this function is not exported
1349 * The extra reference is held until the VM system unmaps the
1350 * vnode from its own context to maintain a vnode reference in
1351 * cases like open()/mmap()/close(), which leave the backing
1352 * object referenced by a mapped memory region in a process
1355 __private_extern__
int
1356 ubc_map(vnode_t vp
, int flags
)
1358 struct ubc_info
*uip
;
1361 int need_wakeup
= 0;
1363 if (UBCINFOEXISTS(vp
)) {
1366 uip
= vp
->v_ubcinfo
;
1368 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1369 SET(uip
->ui_flags
, UI_MAPWAITING
);
1370 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1371 PRIBIO
, "ubc_map", NULL
);
1373 SET(uip
->ui_flags
, UI_MAPBUSY
);
1376 error
= VNOP_MMAP(vp
, flags
, vfs_context_current());
1381 vnode_lock_spin(vp
);
1384 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
1386 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
1388 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1390 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1391 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1397 wakeup(&uip
->ui_flags
);
1409 * Destroy the named memory object associated with the ubc_info control object
1410 * associated with the designated vnode, if there is a ubc_info associated
1411 * with the vnode, and a control object is associated with it
1413 * Parameters: vp The designated vnode
1417 * Notes: This function is called on vnode termination for all vnodes,
1418 * and must therefore not assume that there is a ubc_info that is
1419 * associated with the vnode, nor that there is a control object
1420 * associated with the ubc_info.
1422 * If all the conditions necessary are present, this function
1423 * calls memory_object_destory(), which will in turn end up
1424 * calling ubc_unmap() to release any vnode references that were
1425 * established via ubc_map().
1427 * IMPORTANT: This is an internal use function that is used
1428 * exclusively by the internal use function vclean().
1430 __private_extern__
void
1431 ubc_destroy_named(vnode_t vp
)
1433 memory_object_control_t control
;
1434 struct ubc_info
*uip
;
1437 if (UBCINFOEXISTS(vp
)) {
1438 uip
= vp
->v_ubcinfo
;
1440 /* Terminate the memory object */
1441 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
1442 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1443 kret
= memory_object_destroy(control
, 0);
1444 if (kret
!= KERN_SUCCESS
)
1445 panic("ubc_destroy_named: memory_object_destroy failed");
1454 * Determine whether or not a vnode is currently in use by ubc at a level in
1455 * excess of the requested busycount
1457 * Parameters: vp The vnode to check
1458 * busycount The threshold busy count, used to bias
1459 * the count usually already held by the
1460 * caller to avoid races
1462 * Returns: 1 The vnode is in use over the threshold
1463 * 0 The vnode is not in use over the
1466 * Notes: Because the vnode is only held locked while actually asking
1467 * the use count, this function only represents a snapshot of the
1468 * current state of the vnode. If more accurate information is
1469 * required, an additional busycount should be held by the caller
1470 * and a non-zero busycount used.
1472 * If there is no ubc_info associated with the vnode, this
1473 * function will report that the vnode is not in use by ubc.
1476 ubc_isinuse(struct vnode
*vp
, int busycount
)
1478 if ( !UBCINFOEXISTS(vp
))
1480 return(ubc_isinuse_locked(vp
, busycount
, 0));
1485 * ubc_isinuse_locked
1487 * Determine whether or not a vnode is currently in use by ubc at a level in
1488 * excess of the requested busycount
1490 * Parameters: vp The vnode to check
1491 * busycount The threshold busy count, used to bias
1492 * the count usually already held by the
1493 * caller to avoid races
1494 * locked True if the vnode is already locked by
1497 * Returns: 1 The vnode is in use over the threshold
1498 * 0 The vnode is not in use over the
1501 * Notes: If the vnode is not locked on entry, it is locked while
1502 * actually asking the use count. If this is the case, this
1503 * function only represents a snapshot of the current state of
1504 * the vnode. If more accurate information is required, the
1505 * vnode lock should be held by the caller, otherwise an
1506 * additional busycount should be held by the caller and a
1507 * non-zero busycount used.
1509 * If there is no ubc_info associated with the vnode, this
1510 * function will report that the vnode is not in use by ubc.
1513 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
1519 vnode_lock_spin(vp
);
1521 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
1533 * Reverse the effects of a ubc_map() call for a given vnode
1535 * Parameters: vp vnode to unmap from ubc
1539 * Notes: This is an internal use function used by vnode_pager_unmap().
1540 * It will attempt to obtain a reference on the supplied vnode,
1541 * and if it can do so, and there is an associated ubc_info, and
1542 * the flags indicate that it was mapped via ubc_map(), then the
1543 * flag is cleared, the mapping removed, and the reference taken
1544 * by ubc_map() is released.
1546 * IMPORTANT: This MUST only be called by the VM
1547 * to prevent race conditions.
1549 __private_extern__
void
1550 ubc_unmap(struct vnode
*vp
)
1552 struct ubc_info
*uip
;
1554 int need_wakeup
= 0;
1556 if (vnode_getwithref(vp
))
1559 if (UBCINFOEXISTS(vp
)) {
1561 uip
= vp
->v_ubcinfo
;
1563 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1564 SET(uip
->ui_flags
, UI_MAPWAITING
);
1565 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1566 PRIBIO
, "ubc_unmap", NULL
);
1568 SET(uip
->ui_flags
, UI_MAPBUSY
);
1570 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
1571 CLR(uip
->ui_flags
, UI_ISMAPPED
);
1577 (void)VNOP_MNOMAP(vp
, vfs_context_current());
1581 vnode_lock_spin(vp
);
1583 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1584 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1585 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1591 wakeup(&uip
->ui_flags
);
1595 * the drop of the vnode ref will cleanup
1604 * Manipulate individual page state for a vnode with an associated ubc_info
1605 * with an associated memory object control.
1607 * Parameters: vp The vnode backing the page
1608 * f_offset A file offset interior to the page
1609 * ops The operations to perform, as a bitmap
1610 * (see below for more information)
1611 * phys_entryp The address of a ppnum_t; may be NULL
1613 * flagsp A pointer to an int to contain flags;
1614 * may be NULL to ignore
1616 * Returns: KERN_SUCCESS Success
1617 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1619 * KERN_INVALID_OBJECT If UPL_POP_PHYSICAL and the object is
1620 * not physically contiguous
1621 * KERN_INVALID_OBJECT If !UPL_POP_PHYSICAL and the object is
1622 * physically contiguous
1623 * KERN_FAILURE If the page cannot be looked up
1626 * *phys_entryp (modified) If phys_entryp is non-NULL and
1628 * *flagsp (modified) If flagsp is non-NULL and there was
1629 * !UPL_POP_PHYSICAL and a KERN_SUCCESS
1631 * Notes: For object boundaries, it is considerably more efficient to
1632 * ensure that f_offset is in fact on a page boundary, as this
1633 * will avoid internal use of the hash table to identify the
1634 * page, and would therefore skip a number of early optimizations.
1635 * Since this is a page operation anyway, the caller should try
1636 * to pass only a page aligned offset because of this.
1638 * *flagsp may be modified even if this function fails. If it is
1639 * modified, it will contain the condition of the page before the
1640 * requested operation was attempted; these will only include the
1641 * bitmap flags, and not the PL_POP_PHYSICAL, UPL_POP_DUMP,
1642 * UPL_POP_SET, or UPL_POP_CLR bits.
1644 * The flags field may contain a specific operation, such as
1645 * UPL_POP_PHYSICAL or UPL_POP_DUMP:
1647 * o UPL_POP_PHYSICAL Fail if not contiguous; if
1648 * *phys_entryp and successful, set
1650 * o UPL_POP_DUMP Dump the specified page
1652 * Otherwise, it is treated as a bitmap of one or more page
1653 * operations to perform on the final memory object; allowable
1656 * o UPL_POP_DIRTY The page is dirty
1657 * o UPL_POP_PAGEOUT The page is paged out
1658 * o UPL_POP_PRECIOUS The page is precious
1659 * o UPL_POP_ABSENT The page is absent
1660 * o UPL_POP_BUSY The page is busy
1662 * If the page status is only being queried and not modified, then
1663 * not other bits should be specified. However, if it is being
1664 * modified, exactly ONE of the following bits should be set:
1666 * o UPL_POP_SET Set the current bitmap bits
1667 * o UPL_POP_CLR Clear the current bitmap bits
1669 * Thus to effect a combination of setting an clearing, it may be
1670 * necessary to call this function twice. If this is done, the
1671 * set should be used before the clear, since clearing may trigger
1672 * a wakeup on the destination page, and if the page is backed by
1673 * an encrypted swap file, setting will trigger the decryption
1674 * needed before the wakeup occurs.
1681 ppnum_t
*phys_entryp
,
1684 memory_object_control_t control
;
1686 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1687 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1688 return KERN_INVALID_ARGUMENT
;
1690 return (memory_object_page_op(control
,
1691 (memory_object_offset_t
)f_offset
,
1701 * Manipulate page state for a range of memory for a vnode with an associated
1702 * ubc_info with an associated memory object control, when page level state is
1703 * not required to be returned from the call (i.e. there are no phys_entryp or
1704 * flagsp parameters to this call, and it takes a range which may contain
1705 * multiple pages, rather than an offset interior to a single page).
1707 * Parameters: vp The vnode backing the page
1708 * f_offset_beg A file offset interior to the start page
1709 * f_offset_end A file offset interior to the end page
1710 * ops The operations to perform, as a bitmap
1711 * (see below for more information)
1712 * range The address of an int; may be NULL to
1715 * Returns: KERN_SUCCESS Success
1716 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1718 * KERN_INVALID_OBJECT If the object is physically contiguous
1721 * *range (modified) If range is non-NULL, its contents will
1722 * be modified to contain the number of
1723 * bytes successfully operated upon.
1725 * Notes: IMPORTANT: This function cannot be used on a range that
1726 * consists of physically contiguous pages.
1728 * For object boundaries, it is considerably more efficient to
1729 * ensure that f_offset_beg and f_offset_end are in fact on page
1730 * boundaries, as this will avoid internal use of the hash table
1731 * to identify the page, and would therefore skip a number of
1732 * early optimizations. Since this is an operation on a set of
1733 * pages anyway, the caller should try to pass only a page aligned
1734 * offsets because of this.
1736 * *range will be modified only if this function succeeds.
1738 * The flags field MUST contain a specific operation; allowable
1741 * o UPL_ROP_ABSENT Returns the extent of the range
1742 * presented which is absent, starting
1743 * with the start address presented
1745 * o UPL_ROP_PRESENT Returns the extent of the range
1746 * presented which is present (resident),
1747 * starting with the start address
1749 * o UPL_ROP_DUMP Dump the pages which are found in the
1750 * target object for the target range.
1752 * IMPORTANT: For UPL_ROP_ABSENT and UPL_ROP_PRESENT; if there are
1753 * multiple regions in the range, only the first matching region
1764 memory_object_control_t control
;
1766 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1767 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1768 return KERN_INVALID_ARGUMENT
;
1770 return (memory_object_range_op(control
,
1771 (memory_object_offset_t
)f_offset_beg
,
1772 (memory_object_offset_t
)f_offset_end
,
1781 * Given a vnode, cause the population of a portion of the vm_object; based on
1782 * the nature of the request, the pages returned may contain valid data, or
1783 * they may be uninitialized.
1785 * Parameters: vp The vnode from which to create the upl
1786 * f_offset The start offset into the backing store
1787 * represented by the vnode
1788 * bufsize The size of the upl to create
1789 * uplp Pointer to the upl_t to receive the
1790 * created upl; MUST NOT be NULL
1791 * plp Pointer to receive the internal page
1792 * list for the created upl; MAY be NULL
1795 * Returns: KERN_SUCCESS The requested upl has been created
1796 * KERN_INVALID_ARGUMENT The bufsize argument is not an even
1797 * multiple of the page size
1798 * KERN_INVALID_ARGUMENT There is no ubc_info associated with
1799 * the vnode, or there is no memory object
1800 * control associated with the ubc_info
1801 * memory_object_upl_request:KERN_INVALID_VALUE
1802 * The supplied upl_flags argument is
1806 * *plp (modified) If non-NULL, the value of *plp will be
1807 * modified to point to the internal page
1808 * list; this modification may occur even
1809 * if this function is unsuccessful, in
1810 * which case the contents may be invalid
1812 * Note: If successful, the returned *uplp MUST subsequently be freed
1813 * via a call to ubc_upl_commit(), ubc_upl_commit_range(),
1814 * ubc_upl_abort(), or ubc_upl_abort_range().
1822 upl_page_info_t
**plp
,
1825 memory_object_control_t control
;
1832 if (bufsize
& 0xfff)
1833 return KERN_INVALID_ARGUMENT
;
1835 if (uplflags
& (UPL_UBC_MSYNC
| UPL_UBC_PAGEOUT
| UPL_UBC_PAGEIN
)) {
1837 if (uplflags
& UPL_UBC_MSYNC
) {
1838 uplflags
&= UPL_RET_ONLY_DIRTY
;
1840 uplflags
|= UPL_COPYOUT_FROM
| UPL_CLEAN_IN_PLACE
|
1841 UPL_SET_INTERNAL
| UPL_SET_LITE
;
1843 } else if (uplflags
& UPL_UBC_PAGEOUT
) {
1844 uplflags
&= UPL_RET_ONLY_DIRTY
;
1846 if (uplflags
& UPL_RET_ONLY_DIRTY
)
1847 uplflags
|= UPL_NOBLOCK
;
1849 uplflags
|= UPL_FOR_PAGEOUT
| UPL_CLEAN_IN_PLACE
|
1850 UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
| UPL_SET_LITE
;
1852 uplflags
|= UPL_RET_ONLY_ABSENT
| UPL_NOBLOCK
|
1853 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
1854 UPL_SET_INTERNAL
| UPL_SET_LITE
;
1857 uplflags
&= ~UPL_FOR_PAGEOUT
;
1859 if (uplflags
& UPL_WILL_BE_DUMPED
) {
1860 uplflags
&= ~UPL_WILL_BE_DUMPED
;
1861 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
1863 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
1865 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1866 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1867 return KERN_INVALID_ARGUMENT
;
1869 kr
= memory_object_upl_request(control
, f_offset
, bufsize
, uplp
, NULL
, NULL
, uplflags
);
1870 if (kr
== KERN_SUCCESS
&& plp
!= NULL
)
1871 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
1877 * ubc_upl_maxbufsize
1879 * Return the maximum bufsize ubc_create_upl( ) will take.
1883 * Returns: maximum size buffer (in bytes) ubc_create_upl( ) will take.
1889 return(MAX_UPL_SIZE
* PAGE_SIZE
);
1895 * Map the page list assocated with the supplied upl into the kernel virtual
1896 * address space at the virtual address indicated by the dst_addr argument;
1897 * the entire upl is mapped
1899 * Parameters: upl The upl to map
1900 * dst_addr The address at which to map the upl
1902 * Returns: KERN_SUCCESS The upl has been mapped
1903 * KERN_INVALID_ARGUMENT The upl is UPL_NULL
1904 * KERN_FAILURE The upl is already mapped
1905 * vm_map_enter:KERN_INVALID_ARGUMENT
1906 * A failure code from vm_map_enter() due
1907 * to an invalid argument
1912 vm_offset_t
*dst_addr
)
1914 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
1921 * Unmap the page list assocated with the supplied upl from the kernel virtual
1922 * address space; the entire upl is unmapped.
1924 * Parameters: upl The upl to unmap
1926 * Returns: KERN_SUCCESS The upl has been unmapped
1927 * KERN_FAILURE The upl is not currently mapped
1928 * KERN_INVALID_ARGUMENT If the upl is UPL_NULL
1934 return(vm_upl_unmap(kernel_map
, upl
));
1941 * Commit the contents of the upl to the backing store
1943 * Parameters: upl The upl to commit
1945 * Returns: KERN_SUCCESS The upl has been committed
1946 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
1947 * KERN_FAILURE The supplied upl does not represent
1948 * device memory, and the offset plus the
1949 * size would exceed the actual size of
1952 * Notes: In practice, the only return value for this function should be
1953 * KERN_SUCCESS, unless there has been data structure corruption;
1954 * since the upl is deallocated regardless of success or failure,
1955 * there's really nothing to do about this other than panic.
1957 * IMPORTANT: Use of this function should not be mixed with use of
1958 * ubc_upl_commit_range(), due to the unconditional deallocation
1965 upl_page_info_t
*pl
;
1968 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1969 kr
= upl_commit(upl
, pl
, MAX_UPL_SIZE
);
1970 upl_deallocate(upl
);
1978 * Commit the contents of the specified range of the upl to the backing store
1980 * Parameters: upl The upl to commit
1981 * offset The offset into the upl
1982 * size The size of the region to be committed,
1983 * starting at the specified offset
1984 * flags commit type (see below)
1986 * Returns: KERN_SUCCESS The range has been committed
1987 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
1988 * KERN_FAILURE The supplied upl does not represent
1989 * device memory, and the offset plus the
1990 * size would exceed the actual size of
1993 * Notes: IMPORTANT: If the commit is successful, and the object is now
1994 * empty, the upl will be deallocated. Since the caller cannot
1995 * check that this is the case, the UPL_COMMIT_FREE_ON_EMPTY flag
1996 * should generally only be used when the offset is 0 and the size
1997 * is equal to the upl size.
1999 * The flags argument is a bitmap of flags on the rage of pages in
2000 * the upl to be committed; allowable flags are:
2002 * o UPL_COMMIT_FREE_ON_EMPTY Free the upl when it is
2003 * both empty and has been
2004 * successfully committed
2005 * o UPL_COMMIT_CLEAR_DIRTY Clear each pages dirty
2006 * bit; will prevent a
2008 * o UPL_COMMIT_SET_DIRTY Set each pages dirty
2009 * bit; will cause a later
2011 * o UPL_COMMIT_INACTIVATE Clear each pages
2012 * reference bit; the page
2013 * will not be accessed
2014 * o UPL_COMMIT_ALLOW_ACCESS Unbusy each page; pages
2015 * become busy when an
2016 * IOMemoryDescriptor is
2017 * mapped or redirected,
2018 * and we have to wait for
2021 * The flag UPL_COMMIT_NOTIFY_EMPTY is used internally, and should
2022 * not be specified by the caller.
2024 * The UPL_COMMIT_CLEAR_DIRTY and UPL_COMMIT_SET_DIRTY flags are
2025 * mutually exclusive, and should not be combined.
2028 ubc_upl_commit_range(
2030 upl_offset_t offset
,
2034 upl_page_info_t
*pl
;
2038 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
2039 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
2041 if (flags
& UPL_COMMIT_KERNEL_ONLY_FLAGS
) {
2042 return KERN_INVALID_ARGUMENT
;
2045 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2047 kr
= upl_commit_range(upl
, offset
, size
, flags
,
2048 pl
, MAX_UPL_SIZE
, &empty
);
2050 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
2051 upl_deallocate(upl
);
2058 * ubc_upl_abort_range
2060 * Abort the contents of the specified range of the specified upl
2062 * Parameters: upl The upl to abort
2063 * offset The offset into the upl
2064 * size The size of the region to be aborted,
2065 * starting at the specified offset
2066 * abort_flags abort type (see below)
2068 * Returns: KERN_SUCCESS The range has been aborted
2069 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2070 * KERN_FAILURE The supplied upl does not represent
2071 * device memory, and the offset plus the
2072 * size would exceed the actual size of
2075 * Notes: IMPORTANT: If the abort is successful, and the object is now
2076 * empty, the upl will be deallocated. Since the caller cannot
2077 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2078 * should generally only be used when the offset is 0 and the size
2079 * is equal to the upl size.
2081 * The abort_flags argument is a bitmap of flags on the range of
2082 * pages in the upl to be aborted; allowable flags are:
2084 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2085 * empty and has been successfully
2087 * o UPL_ABORT_RESTART The operation must be restarted
2088 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2089 * o UPL_ABORT_ERROR An I/O error occurred
2090 * o UPL_ABORT_DUMP_PAGES Just free the pages
2091 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2092 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2094 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2095 * not be specified by the caller. It is intended to fulfill the
2096 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2097 * ubc_upl_commit_range(), but is never referenced internally.
2099 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2100 * referenced; do not use it.
2103 ubc_upl_abort_range(
2105 upl_offset_t offset
,
2110 boolean_t empty
= FALSE
;
2112 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
2113 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
2115 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
2117 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
2118 upl_deallocate(upl
);
2127 * Abort the contents of the specified upl
2129 * Parameters: upl The upl to abort
2130 * abort_type abort type (see below)
2132 * Returns: KERN_SUCCESS The range has been aborted
2133 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2134 * KERN_FAILURE The supplied upl does not represent
2135 * device memory, and the offset plus the
2136 * size would exceed the actual size of
2139 * Notes: IMPORTANT: If the abort is successful, and the object is now
2140 * empty, the upl will be deallocated. Since the caller cannot
2141 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2142 * should generally only be used when the offset is 0 and the size
2143 * is equal to the upl size.
2145 * The abort_type is a bitmap of flags on the range of
2146 * pages in the upl to be aborted; allowable flags are:
2148 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2149 * empty and has been successfully
2151 * o UPL_ABORT_RESTART The operation must be restarted
2152 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2153 * o UPL_ABORT_ERROR An I/O error occurred
2154 * o UPL_ABORT_DUMP_PAGES Just free the pages
2155 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2156 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2158 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2159 * not be specified by the caller. It is intended to fulfill the
2160 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2161 * ubc_upl_commit_range(), but is never referenced internally.
2163 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2164 * referenced; do not use it.
2173 kr
= upl_abort(upl
, abort_type
);
2174 upl_deallocate(upl
);
2182 * Retrieve the internal page list for the specified upl
2184 * Parameters: upl The upl to obtain the page list from
2186 * Returns: !NULL The (upl_page_info_t *) for the page
2187 * list internal to the upl
2188 * NULL Error/no page list associated
2190 * Notes: IMPORTANT: The function is only valid on internal objects
2191 * where the list request was made with the UPL_INTERNAL flag.
2193 * This function is a utility helper function, since some callers
2194 * may not have direct access to the header defining the macro,
2195 * due to abstraction layering constraints.
2201 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
2206 UBCINFOEXISTS(struct vnode
* vp
)
2208 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
2215 #define CS_BLOB_PAGEABLE 0
2216 static volatile SInt32 cs_blob_size
= 0;
2217 static volatile SInt32 cs_blob_count
= 0;
2218 static SInt32 cs_blob_size_peak
= 0;
2219 static UInt32 cs_blob_size_max
= 0;
2220 static SInt32 cs_blob_count_peak
= 0;
2222 int cs_validation
= 1;
2224 SYSCTL_INT(_vm
, OID_AUTO
, cs_validation
, CTLFLAG_RW
, &cs_validation
, 0, "Do validate code signatures");
2225 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count
, CTLFLAG_RD
, &cs_blob_count
, 0, "Current number of code signature blobs");
2226 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size
, CTLFLAG_RD
, &cs_blob_size
, 0, "Current size of all code signature blobs");
2227 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count_peak
, CTLFLAG_RD
, &cs_blob_count_peak
, 0, "Peak number of code signature blobs");
2228 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_peak
, CTLFLAG_RD
, &cs_blob_size_peak
, 0, "Peak size of code signature blobs");
2229 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_max
, CTLFLAG_RD
, &cs_blob_size_max
, 0, "Size of biggest code signature blob");
2232 ubc_cs_blob_allocate(
2233 vm_offset_t
*blob_addr_p
,
2234 vm_size_t
*blob_size_p
)
2238 #if CS_BLOB_PAGEABLE
2239 *blob_size_p
= round_page(*blob_size_p
);
2240 kr
= kmem_alloc(kernel_map
, blob_addr_p
, *blob_size_p
);
2241 #else /* CS_BLOB_PAGEABLE */
2242 *blob_addr_p
= (vm_offset_t
) kalloc(*blob_size_p
);
2243 if (*blob_addr_p
== 0) {
2248 #endif /* CS_BLOB_PAGEABLE */
2253 ubc_cs_blob_deallocate(
2254 vm_offset_t blob_addr
,
2255 vm_size_t blob_size
)
2257 #if CS_BLOB_PAGEABLE
2258 kmem_free(kernel_map
, blob_addr
, blob_size
);
2259 #else /* CS_BLOB_PAGEABLE */
2260 kfree((void *) blob_addr
, blob_size
);
2261 #endif /* CS_BLOB_PAGEABLE */
2273 struct ubc_info
*uip
;
2274 struct cs_blob
*blob
, *oblob
;
2276 ipc_port_t blob_handle
;
2277 memory_object_size_t blob_size
;
2278 const CS_CodeDirectory
*cd
;
2279 off_t blob_start_offset
, blob_end_offset
;
2282 blob_handle
= IPC_PORT_NULL
;
2284 blob
= (struct cs_blob
*) kalloc(sizeof (struct cs_blob
));
2289 #if CS_BLOB_PAGEABLE
2290 /* get a memory entry on the blob */
2291 blob_size
= (memory_object_size_t
) size
;
2292 kr
= mach_make_memory_entry_64(kernel_map
,
2298 if (kr
!= KERN_SUCCESS
) {
2302 if (memory_object_round_page(blob_size
) !=
2303 (memory_object_size_t
) round_page(size
)) {
2304 printf("ubc_cs_blob_add: size mismatch 0x%llx 0x%lx !?\n",
2305 blob_size
, (size_t)size
);
2306 panic("XXX FBDP size mismatch 0x%llx 0x%lx\n", blob_size
, (size_t)size
);
2311 blob_size
= (memory_object_size_t
) size
;
2312 blob_handle
= IPC_PORT_NULL
;
2315 /* fill in the new blob */
2316 blob
->csb_cpu_type
= cputype
;
2317 blob
->csb_base_offset
= base_offset
;
2318 blob
->csb_mem_size
= size
;
2319 blob
->csb_mem_offset
= 0;
2320 blob
->csb_mem_handle
= blob_handle
;
2321 blob
->csb_mem_kaddr
= addr
;
2324 * Validate the blob's contents
2326 cd
= findCodeDirectory(
2327 (const CS_SuperBlob
*) addr
,
2329 (char *) addr
+ blob
->csb_mem_size
);
2331 /* no code directory => useless blob ! */
2332 blob
->csb_flags
= 0;
2333 blob
->csb_start_offset
= 0;
2334 blob
->csb_end_offset
= 0;
2336 unsigned char *sha1_base
;
2339 blob
->csb_flags
= ntohl(cd
->flags
) | CS_VALID
;
2340 blob
->csb_end_offset
= round_page(ntohl(cd
->codeLimit
));
2341 if((ntohl(cd
->version
) >= supportsScatter
) && (ntohl(cd
->scatterOffset
))) {
2342 const struct Scatter
*scatter
= (const struct Scatter
*)
2343 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
2344 blob
->csb_start_offset
= ntohl(scatter
->base
) * PAGE_SIZE
;
2346 blob
->csb_start_offset
= (blob
->csb_end_offset
-
2347 (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
));
2349 /* compute the blob's SHA1 hash */
2350 sha1_base
= (const unsigned char *) cd
;
2351 sha1_size
= ntohl(cd
->length
);
2352 SHA1Init(&sha1ctxt
);
2353 SHA1Update(&sha1ctxt
, sha1_base
, sha1_size
);
2354 SHA1Final(blob
->csb_sha1
, &sha1ctxt
);
2358 * Let policy module check whether the blob's signature is accepted.
2361 error
= mac_vnode_check_signature(vp
, blob
->csb_sha1
, (void*)addr
, size
);
2367 * Validate the blob's coverage
2369 blob_start_offset
= blob
->csb_base_offset
+ blob
->csb_start_offset
;
2370 blob_end_offset
= blob
->csb_base_offset
+ blob
->csb_end_offset
;
2372 if (blob_start_offset
>= blob_end_offset
||
2373 blob_start_offset
< 0 ||
2374 blob_end_offset
<= 0) {
2375 /* reject empty or backwards blob */
2381 if (! UBCINFOEXISTS(vp
)) {
2386 uip
= vp
->v_ubcinfo
;
2388 /* check if this new blob overlaps with an existing blob */
2389 for (oblob
= uip
->cs_blobs
;
2391 oblob
= oblob
->csb_next
) {
2392 off_t oblob_start_offset
, oblob_end_offset
;
2394 oblob_start_offset
= (oblob
->csb_base_offset
+
2395 oblob
->csb_start_offset
);
2396 oblob_end_offset
= (oblob
->csb_base_offset
+
2397 oblob
->csb_end_offset
);
2398 if (blob_start_offset
>= oblob_end_offset
||
2399 blob_end_offset
<= oblob_start_offset
) {
2400 /* no conflict with this existing blob */
2403 if (blob_start_offset
== oblob_start_offset
&&
2404 blob_end_offset
== oblob_end_offset
&&
2405 blob
->csb_mem_size
== oblob
->csb_mem_size
&&
2406 blob
->csb_flags
== oblob
->csb_flags
&&
2407 (blob
->csb_cpu_type
== CPU_TYPE_ANY
||
2408 oblob
->csb_cpu_type
== CPU_TYPE_ANY
||
2409 blob
->csb_cpu_type
== oblob
->csb_cpu_type
) &&
2410 !bcmp(blob
->csb_sha1
,
2414 * We already have this blob:
2415 * we'll return success but
2416 * throw away the new blob.
2418 if (oblob
->csb_cpu_type
== CPU_TYPE_ANY
) {
2420 * The old blob matches this one
2421 * but doesn't have any CPU type.
2422 * Update it with whatever the caller
2423 * provided this time.
2425 oblob
->csb_cpu_type
= cputype
;
2431 /* different blob: reject the new one */
2441 /* mark this vnode's VM object as having "signed pages" */
2442 kr
= memory_object_signed(uip
->ui_control
, TRUE
);
2443 if (kr
!= KERN_SUCCESS
) {
2450 * Add this blob to the list of blobs for this vnode.
2451 * We always add at the front of the list and we never remove a
2452 * blob from the list, so ubc_cs_get_blobs() can return whatever
2453 * the top of the list was and that list will remain valid
2454 * while we validate a page, even after we release the vnode's lock.
2456 blob
->csb_next
= uip
->cs_blobs
;
2457 uip
->cs_blobs
= blob
;
2459 OSAddAtomic(+1, &cs_blob_count
);
2460 if (cs_blob_count
> cs_blob_count_peak
) {
2461 cs_blob_count_peak
= cs_blob_count
; /* XXX atomic ? */
2463 OSAddAtomic((SInt32
) +blob
->csb_mem_size
, &cs_blob_size
);
2464 if ((SInt32
) cs_blob_size
> cs_blob_size_peak
) {
2465 cs_blob_size_peak
= (SInt32
) cs_blob_size
; /* XXX atomic ? */
2467 if ((UInt32
) blob
->csb_mem_size
> cs_blob_size_max
) {
2468 cs_blob_size_max
= (UInt32
) blob
->csb_mem_size
;
2475 printf("CODE SIGNING: proc %d(%s) "
2476 "loaded %s signatures for file (%s) "
2477 "range 0x%llx:0x%llx flags 0x%x\n",
2478 p
->p_pid
, p
->p_comm
,
2479 blob
->csb_cpu_type
== -1 ? "detached" : "embedded",
2481 blob
->csb_base_offset
+ blob
->csb_start_offset
,
2482 blob
->csb_base_offset
+ blob
->csb_end_offset
,
2488 error
= 0; /* success ! */
2492 /* we failed; release what we allocated */
2494 kfree(blob
, sizeof (*blob
));
2497 if (blob_handle
!= IPC_PORT_NULL
) {
2498 mach_memory_entry_port_release(blob_handle
);
2499 blob_handle
= IPC_PORT_NULL
;
2503 if (error
== EAGAIN
) {
2505 * See above: error is EAGAIN if we were asked
2506 * to add an existing blob again. We cleaned the new
2507 * blob and we want to return success.
2511 * Since we're not failing, consume the data we received.
2513 ubc_cs_blob_deallocate(addr
, size
);
2526 struct ubc_info
*uip
;
2527 struct cs_blob
*blob
;
2528 off_t offset_in_blob
;
2530 vnode_lock_spin(vp
);
2532 if (! UBCINFOEXISTS(vp
)) {
2537 uip
= vp
->v_ubcinfo
;
2538 for (blob
= uip
->cs_blobs
;
2540 blob
= blob
->csb_next
) {
2541 if (cputype
!= -1 && blob
->csb_cpu_type
== cputype
) {
2545 offset_in_blob
= offset
- blob
->csb_base_offset
;
2546 if (offset_in_blob
>= blob
->csb_start_offset
&&
2547 offset_in_blob
< blob
->csb_end_offset
) {
2548 /* our offset is covered by this blob */
2562 struct ubc_info
*uip
)
2564 struct cs_blob
*blob
, *next_blob
;
2566 for (blob
= uip
->cs_blobs
;
2569 next_blob
= blob
->csb_next
;
2570 if (blob
->csb_mem_kaddr
!= 0) {
2571 ubc_cs_blob_deallocate(blob
->csb_mem_kaddr
,
2572 blob
->csb_mem_size
);
2573 blob
->csb_mem_kaddr
= 0;
2575 if (blob
->csb_mem_handle
!= IPC_PORT_NULL
) {
2576 mach_memory_entry_port_release(blob
->csb_mem_handle
);
2578 blob
->csb_mem_handle
= IPC_PORT_NULL
;
2579 OSAddAtomic(-1, &cs_blob_count
);
2580 OSAddAtomic((SInt32
) -blob
->csb_mem_size
, &cs_blob_size
);
2581 kfree(blob
, sizeof (*blob
));
2583 uip
->cs_blobs
= NULL
;
2590 struct ubc_info
*uip
;
2591 struct cs_blob
*blobs
;
2594 * No need to take the vnode lock here. The caller must be holding
2595 * a reference on the vnode (via a VM mapping or open file descriptor),
2596 * so the vnode will not go away. The ubc_info stays until the vnode
2597 * goes away. And we only modify "blobs" by adding to the head of the
2599 * The ubc_info could go away entirely if the vnode gets reclaimed as
2600 * part of a forced unmount. In the case of a code-signature validation
2601 * during a page fault, the "paging_in_progress" reference on the VM
2602 * object guarantess that the vnode pager (and the ubc_info) won't go
2603 * away during the fault.
2604 * Other callers need to protect against vnode reclaim by holding the
2605 * vnode lock, for example.
2608 if (! UBCINFOEXISTS(vp
)) {
2613 uip
= vp
->v_ubcinfo
;
2614 blobs
= uip
->cs_blobs
;
2620 unsigned long cs_validate_page_no_hash
= 0;
2621 unsigned long cs_validate_page_bad_hash
= 0;
2625 memory_object_offset_t page_offset
,
2630 unsigned char actual_hash
[SHA1_RESULTLEN
];
2631 unsigned char expected_hash
[SHA1_RESULTLEN
];
2632 boolean_t found_hash
;
2633 struct cs_blob
*blobs
, *blob
;
2634 const CS_CodeDirectory
*cd
;
2635 const CS_SuperBlob
*embedded
;
2636 const unsigned char *hash
;
2637 boolean_t validated
;
2638 off_t offset
; /* page offset in the file */
2640 off_t codeLimit
= 0;
2641 char *lower_bound
, *upper_bound
;
2642 vm_offset_t kaddr
, blob_addr
;
2646 offset
= page_offset
;
2648 /* retrieve the expected hash */
2650 blobs
= (struct cs_blob
*) _blobs
;
2654 blob
= blob
->csb_next
) {
2655 offset
= page_offset
- blob
->csb_base_offset
;
2656 if (offset
< blob
->csb_start_offset
||
2657 offset
>= blob
->csb_end_offset
) {
2658 /* our page is not covered by this blob */
2662 /* map the blob in the kernel address space */
2663 kaddr
= blob
->csb_mem_kaddr
;
2665 ksize
= (vm_size_t
) (blob
->csb_mem_size
+
2666 blob
->csb_mem_offset
);
2667 kr
= vm_map(kernel_map
,
2672 blob
->csb_mem_handle
,
2678 if (kr
!= KERN_SUCCESS
) {
2679 /* XXX FBDP what to do !? */
2680 printf("cs_validate_page: failed to map blob, "
2681 "size=0x%lx kr=0x%x\n",
2682 (size_t)blob
->csb_mem_size
, kr
);
2686 blob_addr
= kaddr
+ blob
->csb_mem_offset
;
2688 lower_bound
= CAST_DOWN(char *, blob_addr
);
2689 upper_bound
= lower_bound
+ blob
->csb_mem_size
;
2691 embedded
= (const CS_SuperBlob
*) blob_addr
;
2692 cd
= findCodeDirectory(embedded
, lower_bound
, upper_bound
);
2694 if (cd
->pageSize
!= PAGE_SHIFT
||
2695 cd
->hashType
!= 0x1 ||
2696 cd
->hashSize
!= SHA1_RESULTLEN
) {
2701 offset
= page_offset
- blob
->csb_base_offset
;
2702 if (offset
< blob
->csb_start_offset
||
2703 offset
>= blob
->csb_end_offset
) {
2704 /* our page is not covered by this blob */
2708 codeLimit
= ntohl(cd
->codeLimit
);
2709 hash
= hashes(cd
, atop(offset
),
2710 lower_bound
, upper_bound
);
2712 bcopy(hash
, expected_hash
,
2713 sizeof (expected_hash
));
2721 if (found_hash
== FALSE
) {
2723 * We can't verify this page because there is no signature
2724 * for it (yet). It's possible that this part of the object
2725 * is not signed, or that signatures for that part have not
2727 * Report that the page has not been validated and let the
2728 * caller decide if it wants to accept it or not.
2730 cs_validate_page_no_hash
++;
2732 printf("CODE SIGNING: cs_validate_page: "
2733 "off 0x%llx: no hash to validate !?\n",
2741 const uint32_t *asha1
, *esha1
;
2742 if ((off_t
)(offset
+ size
) > codeLimit
) {
2743 /* partial page at end of segment */
2744 assert(offset
< codeLimit
);
2745 size
= (size_t) (codeLimit
& PAGE_MASK
);
2747 /* compute the actual page's SHA1 hash */
2748 SHA1Init(&sha1ctxt
);
2749 SHA1UpdateUsePhysicalAddress(&sha1ctxt
, data
, size
);
2750 SHA1Final(actual_hash
, &sha1ctxt
);
2752 asha1
= (const uint32_t *) actual_hash
;
2753 esha1
= (const uint32_t *) expected_hash
;
2755 if (bcmp(expected_hash
, actual_hash
, SHA1_RESULTLEN
) != 0) {
2757 printf("CODE SIGNING: cs_validate_page: "
2758 "off 0x%llx size 0x%lx: "
2759 "actual [0x%x 0x%x 0x%x 0x%x 0x%x] != "
2760 "expected [0x%x 0x%x 0x%x 0x%x 0x%x]\n",
2762 asha1
[0], asha1
[1], asha1
[2],
2764 esha1
[0], esha1
[1], esha1
[2],
2765 esha1
[3], esha1
[4]);
2767 cs_validate_page_bad_hash
++;
2771 printf("CODE SIGNING: cs_validate_page: "
2772 "off 0x%llx size 0x%lx: SHA1 OK\n",
2787 unsigned char *cdhash
)
2789 struct cs_blob
*blobs
, *blob
;
2795 blobs
= ubc_get_cs_blobs(vp
);
2798 blob
= blob
->csb_next
) {
2799 /* compute offset relative to this blob */
2800 rel_offset
= offset
- blob
->csb_base_offset
;
2801 if (rel_offset
>= blob
->csb_start_offset
&&
2802 rel_offset
< blob
->csb_end_offset
) {
2803 /* this blob does cover our "offset" ! */
2809 /* we didn't find a blob covering "offset" */
2810 ret
= EBADEXEC
; /* XXX any better error ? */
2812 /* get the SHA1 hash of that blob */
2813 bcopy(blob
->csb_sha1
, cdhash
, sizeof (blob
->csb_sha1
));