2 * Copyright (c) 1999-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@
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>
60 #include <kern/kern_types.h>
61 #include <kern/kalloc.h>
62 #include <kern/zalloc.h>
63 #include <kern/thread.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_protos.h> /* last */
67 #include <libkern/crypto/sha1.h>
69 /* XXX These should be in a BSD accessible Mach header, but aren't. */
70 extern kern_return_t
memory_object_pages_resident(memory_object_control_t
,
72 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
74 extern void Debugger(const char *message
);
77 /* XXX no one uses this interface! */
78 kern_return_t
ubc_page_op_with_control(
79 memory_object_control_t control
,
90 #define assert(cond) \
91 ((void) ((cond) ? 0 : panic("Assert failed: %s", # cond)))
93 #include <kern/assert.h>
94 #endif /* DIAGNOSTIC */
96 static int ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
);
97 static int ubc_umcallback(vnode_t
, void *);
98 static int ubc_msync_internal(vnode_t
, off_t
, off_t
, off_t
*, int, int *);
99 static void ubc_cs_free(struct ubc_info
*uip
);
101 struct zone
*ubc_info_zone
;
106 * Routines to navigate code signing data structures in the kernel...
112 const void *lower_bound
,
113 const void *upper_bound
)
115 if (upper_bound
< lower_bound
||
120 if (start
< lower_bound
||
129 * Magic numbers used by Code Signing
132 CSMAGIC_REQUIREMENT
= 0xfade0c00, /* single Requirement blob */
133 CSMAGIC_REQUIREMENTS
= 0xfade0c01, /* Requirements vector (internal requirements) */
134 CSMAGIC_CODEDIRECTORY
= 0xfade0c02, /* CodeDirectory blob */
135 CSMAGIC_EMBEDDED_SIGNATURE
= 0xfade0cc0, /* embedded form of signature data */
136 CSMAGIC_EMBEDDED_SIGNATURE_OLD
= 0xfade0b02, /* XXX */
137 CSMAGIC_DETACHED_SIGNATURE
= 0xfade0cc1, /* multi-arch collection of embedded signatures */
139 CSSLOT_CODEDIRECTORY
= 0, /* slot index for CodeDirectory */
144 * Structure of an embedded-signature SuperBlob
146 typedef struct __BlobIndex
{
147 uint32_t type
; /* type of entry */
148 uint32_t offset
; /* offset of entry */
151 typedef struct __SuperBlob
{
152 uint32_t magic
; /* magic number */
153 uint32_t length
; /* total length of SuperBlob */
154 uint32_t count
; /* number of index entries following */
155 CS_BlobIndex index
[]; /* (count) entries */
156 /* followed by Blobs in no particular order as indicated by offsets in index */
161 * C form of a CodeDirectory.
163 typedef struct __CodeDirectory
{
164 uint32_t magic
; /* magic number (CSMAGIC_CODEDIRECTORY) */
165 uint32_t length
; /* total length of CodeDirectory blob */
166 uint32_t version
; /* compatibility version */
167 uint32_t flags
; /* setup and mode flags */
168 uint32_t hashOffset
; /* offset of hash slot element at index zero */
169 uint32_t identOffset
; /* offset of identifier string */
170 uint32_t nSpecialSlots
; /* number of special hash slots */
171 uint32_t nCodeSlots
; /* number of ordinary (code) hash slots */
172 uint32_t codeLimit
; /* limit to main image signature range */
173 uint8_t hashSize
; /* size of each hash in bytes */
174 uint8_t hashType
; /* type of hash (cdHashType* constants) */
175 uint8_t spare1
; /* unused (must be zero) */
176 uint8_t pageSize
; /* log2(page size in bytes); 0 => infinite */
177 uint32_t spare2
; /* unused (must be zero) */
178 /* followed by dynamic content as located by offset fields above */
183 * Locate the CodeDirectory from an embedded signature blob
186 CS_CodeDirectory
*findCodeDirectory(
187 const CS_SuperBlob
*embedded
,
191 const CS_CodeDirectory
*cd
= NULL
;
194 cs_valid_range(embedded
, embedded
+ 1, lower_bound
, upper_bound
) &&
195 ntohl(embedded
->magic
) == CSMAGIC_EMBEDDED_SIGNATURE
) {
196 const CS_BlobIndex
*limit
;
197 const CS_BlobIndex
*p
;
199 limit
= &embedded
->index
[ntohl(embedded
->count
)];
200 if (!cs_valid_range(&embedded
->index
[0], limit
,
201 lower_bound
, upper_bound
)) {
204 for (p
= embedded
->index
; p
< limit
; ++p
) {
205 if (ntohl(p
->type
) == CSSLOT_CODEDIRECTORY
) {
206 const unsigned char *base
;
208 base
= (const unsigned char *)embedded
;
209 cd
= (const CS_CodeDirectory
*)(base
+ ntohl(p
->offset
));
215 * Detached signatures come as a bare CS_CodeDirectory,
218 cd
= (const CS_CodeDirectory
*) embedded
;
222 cs_valid_range(cd
, cd
+ 1, lower_bound
, upper_bound
) &&
223 cs_valid_range(cd
, (const char *) cd
+ ntohl(cd
->length
),
224 lower_bound
, upper_bound
) &&
225 ntohl(cd
->magic
) == CSMAGIC_CODEDIRECTORY
) {
229 // not found or not a valid code directory
235 * Locating a page hash
237 static const unsigned char *
239 const CS_CodeDirectory
*cd
,
244 const unsigned char *base
, *top
, *hash
;
247 assert(cs_valid_range(cd
, cd
+ 1, lower_bound
, upper_bound
));
249 base
= (const unsigned char *)cd
+ ntohl(cd
->hashOffset
);
250 nCodeSlots
= ntohl(cd
->nCodeSlots
);
251 top
= base
+ nCodeSlots
* SHA1_RESULTLEN
;
252 if (!cs_valid_range(base
, top
,
253 lower_bound
, upper_bound
) ||
257 assert(page
< nCodeSlots
);
259 hash
= base
+ page
* SHA1_RESULTLEN
;
260 if (!cs_valid_range(hash
, hash
+ SHA1_RESULTLEN
,
261 lower_bound
, upper_bound
)) {
269 * End of routines to navigate code signing data structures in the kernel.
276 * Initialization of the zone for Unified Buffer Cache.
283 * ubc_info_zone(global) initialized for subsequent allocations
285 __private_extern__
void
290 i
= (vm_size_t
) sizeof (struct ubc_info
);
292 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
299 * Allocate and attach an empty ubc_info structure to a vnode
301 * Parameters: vp Pointer to the vnode
304 * vnode_size:ENOMEM Not enough space
305 * vnode_size:??? Other error from vnode_getattr
309 ubc_info_init(struct vnode
*vp
)
311 return(ubc_info_init_internal(vp
, 0, 0));
316 * ubc_info_init_withsize
318 * Allocate and attach a sized ubc_info structure to a vnode
320 * Parameters: vp Pointer to the vnode
321 * filesize The size of the file
324 * vnode_size:ENOMEM Not enough space
325 * vnode_size:??? Other error from vnode_getattr
328 ubc_info_init_withsize(struct vnode
*vp
, off_t filesize
)
330 return(ubc_info_init_internal(vp
, 1, filesize
));
335 * ubc_info_init_internal
337 * Allocate and attach a ubc_info structure to a vnode
339 * Parameters: vp Pointer to the vnode
340 * withfsize{0,1} Zero if the size should be obtained
341 * from the vnode; otherwise, use filesize
342 * filesize The size of the file, if withfsize == 1
345 * vnode_size:ENOMEM Not enough space
346 * vnode_size:??? Other error from vnode_getattr
348 * Notes: We call a blocking zalloc(), and the zone was created as an
349 * expandable and collectable zone, so if no memory is available,
350 * it is possible for zalloc() to block indefinitely. zalloc()
351 * may also panic if the zone of zones is exhausted, since it's
354 * We unconditionally call vnode_pager_setup(), even if this is
355 * a reuse of a ubc_info; in that case, we should probably assert
356 * that it does not already have a pager association, but do not.
358 * Since memory_object_create_named() can only fail from receiving
359 * an invalid pager argument, the explicit check and panic is
360 * merely precautionary.
363 ubc_info_init_internal(vnode_t vp
, int withfsize
, off_t filesize
)
365 register struct ubc_info
*uip
;
369 memory_object_control_t control
;
374 * If there is not already a ubc_info attached to the vnode, we
375 * attach one; otherwise, we will reuse the one that's there.
377 if (uip
== UBC_INFO_NULL
) {
379 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
380 bzero((char *)uip
, sizeof(struct ubc_info
));
383 uip
->ui_flags
= UI_INITED
;
384 uip
->ui_ucred
= NOCRED
;
386 assert(uip
->ui_flags
!= UI_NONE
);
387 assert(uip
->ui_vnode
== vp
);
389 /* now set this ubc_info in the vnode */
393 * Allocate a pager object for this vnode
395 * XXX The value of the pager parameter is currently ignored.
396 * XXX Presumably, this API changed to avoid the race between
397 * XXX setting the pager and the UI_HASPAGER flag.
399 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
403 * Explicitly set the pager into the ubc_info, after setting the
406 SET(uip
->ui_flags
, UI_HASPAGER
);
407 uip
->ui_pager
= pager
;
410 * Note: We can not use VNOP_GETATTR() to get accurate
411 * value of ui_size because this may be an NFS vnode, and
412 * nfs_getattr() can call vinvalbuf(); if this happens,
413 * ubc_info is not set up to deal with that event.
418 * create a vnode - vm_object association
419 * memory_object_create_named() creates a "named" reference on the
420 * memory object we hold this reference as long as the vnode is
421 * "alive." Since memory_object_create_named() took its own reference
422 * on the vnode pager we passed it, we can drop the reference
423 * vnode_pager_setup() returned here.
425 kret
= memory_object_create_named(pager
,
426 (memory_object_size_t
)uip
->ui_size
, &control
);
427 vnode_pager_deallocate(pager
);
428 if (kret
!= KERN_SUCCESS
)
429 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
432 uip
->ui_control
= control
; /* cache the value of the mo control */
433 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
435 if (withfsize
== 0) {
436 /* initialize the size */
437 error
= vnode_size(vp
, &uip
->ui_size
, vfs_context_current());
441 uip
->ui_size
= filesize
;
443 vp
->v_lflag
|= VNAMED_UBC
; /* vnode has a named ubc reference */
452 * Free a ubc_info structure
454 * Parameters: uip A pointer to the ubc_info to free
458 * Notes: If there is a credential that has subsequently been associated
459 * with the ubc_info via a call to ubc_setcred(), the reference
460 * to the credential is dropped.
462 * It's actually impossible for a ubc_info.ui_control to take the
463 * value MEMORY_OBJECT_CONTROL_NULL.
466 ubc_info_free(struct ubc_info
*uip
)
468 if (IS_VALID_CRED(uip
->ui_ucred
)) {
469 kauth_cred_unref(&uip
->ui_ucred
);
472 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
473 memory_object_control_deallocate(uip
->ui_control
);
475 cluster_release(uip
);
478 zfree(ubc_info_zone
, uip
);
484 ubc_info_deallocate(struct ubc_info
*uip
)
493 * Tell the VM that the the size of the file represented by the vnode has
496 * Parameters: vp The vp whose backing file size is
498 * nsize The new size of the backing file
503 * Notes: This function will indicate failure if the new size that's
504 * being attempted to be set is negative.
506 * This function will fail if there is no ubc_info currently
507 * associated with the vnode.
509 * This function will indicate success it the new size is the
510 * same or larger than the old size (in this case, the remainder
511 * of the file will require modification or use of an existing upl
512 * to access successfully).
514 * This function will fail if the new file size is smaller, and
515 * the memory region being invalidated was unable to actually be
516 * invalidated and/or the last page could not be flushed, if the
517 * new size is not aligned to a page boundary. This is usually
518 * indicative of an I/O error.
521 ubc_setsize(struct vnode
*vp
, off_t nsize
)
523 off_t osize
; /* ui_size before change */
524 off_t lastpg
, olastpgend
, lastoff
;
525 struct ubc_info
*uip
;
526 memory_object_control_t control
;
527 kern_return_t kret
= KERN_SUCCESS
;
529 if (nsize
< (off_t
)0)
532 if (!UBCINFOEXISTS(vp
))
536 osize
= uip
->ui_size
;
538 * Update the size before flushing the VM
540 uip
->ui_size
= nsize
;
542 if (nsize
>= osize
) /* Nothing more to do */
543 return (1); /* return success */
546 * When the file shrinks, invalidate the pages beyond the
547 * new size. Also get rid of garbage beyond nsize on the
548 * last page. The ui_size already has the nsize, so any
549 * subsequent page-in will zero-fill the tail properly
551 lastpg
= trunc_page_64(nsize
);
552 olastpgend
= round_page_64(osize
);
553 control
= uip
->ui_control
;
555 lastoff
= (nsize
& PAGE_MASK_64
);
563 * new EOF ends up in the middle of a page
564 * zero the tail of this page if its currently
565 * present in the cache
567 kret
= ubc_create_upl(vp
, lastpg
, PAGE_SIZE
, &upl
, &pl
, UPL_SET_LITE
);
569 if (kret
!= KERN_SUCCESS
)
570 panic("ubc_setsize: ubc_create_upl (error = %d)\n", kret
);
572 if (upl_valid_page(pl
, 0))
573 cluster_zero(upl
, (uint32_t)lastoff
, PAGE_SIZE
- (uint32_t)lastoff
, NULL
);
575 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
577 lastpg
+= PAGE_SIZE_64
;
579 if (olastpgend
> lastpg
) {
581 * invalidate the pages beyond the new EOF page
584 kret
= memory_object_lock_request(control
,
585 (memory_object_offset_t
)lastpg
,
586 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
587 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
589 if (kret
!= KERN_SUCCESS
)
590 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
592 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
599 * Get the size of the file assocated with the specified vnode
601 * Parameters: vp The vnode whose size is of interest
603 * Returns: 0 There is no ubc_info associated with
604 * this vnode, or the size is zero
605 * !0 The size of the file
607 * Notes: Using this routine, it is not possible for a caller to
608 * successfully distinguish between a vnode associate with a zero
609 * length file, and a vnode with no associated ubc_info. The
610 * caller therefore needs to not care, or needs to ensure that
611 * they have previously successfully called ubc_info_init() or
612 * ubc_info_init_withsize().
615 ubc_getsize(struct vnode
*vp
)
617 /* people depend on the side effect of this working this way
618 * as they call this for directory
620 if (!UBCINFOEXISTS(vp
))
622 return (vp
->v_ubcinfo
->ui_size
);
629 * Call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes for this
632 * Parameters: mp The mount point
636 * Notes: There is no failure indication for this function.
638 * This function is used in the unmount path; since it may block
639 * I/O indefinitely, it should not be used in the forced unmount
640 * path, since a device unavailability could also block that
643 * Because there is no device ejection interlock on USB, FireWire,
644 * or similar devices, it's possible that an ejection that begins
645 * subsequent to the vnode_iterate() completing, either on one of
646 * those devices, or a network mount for which the server quits
647 * responding, etc., may cause the caller to block indefinitely.
649 __private_extern__
int
650 ubc_umount(struct mount
*mp
)
652 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
660 * Used by ubc_umount() as an internal implementation detail; see ubc_umount()
661 * and vnode_iterate() for details of implementation.
664 ubc_umcallback(vnode_t vp
, __unused
void * args
)
667 if (UBCINFOEXISTS(vp
)) {
669 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
671 return (VNODE_RETURNED
);
678 * Get the credentials currently active for the ubc_info associated with the
681 * Parameters: vp The vnode whose ubc_info credentials
682 * are to be retrieved
684 * Returns: !NOCRED The credentials
685 * NOCRED If there is no ubc_info for the vnode,
686 * or if there is one, but it has not had
687 * any credentials associated with it via
688 * a call to ubc_setcred()
691 ubc_getcred(struct vnode
*vp
)
693 if (UBCINFOEXISTS(vp
))
694 return (vp
->v_ubcinfo
->ui_ucred
);
703 * If they are not already set, set the credentials of the ubc_info structure
704 * associated with the vnode to those of the supplied thread; otherwise leave
707 * Parameters: vp The vnode whose ubc_info creds are to
709 * p The process whose credentials are to
710 * be used, if not running on an assumed
712 * thread The thread whose credentials are to
715 * Returns: 1 This vnode has no associated ubc_info
718 * Notes: This function takes a proc parameter to account for bootstrap
719 * issues where a task or thread may call this routine, either
720 * before credentials have been initialized by bsd_init(), or if
721 * there is no BSD info asscoiate with a mach thread yet. This
722 * is known to happen in both the initial swap and memory mapping
725 * This function is generally used only in the following cases:
727 * o a memory mapped file via the mmap() system call
728 * o a memory mapped file via the deprecated map_fd() call
729 * o a swap store backing file
730 * o subsequent to a successful write via vn_write()
732 * The information is then used by the NFS client in order to
733 * cons up a wire message in either the page-in or page-out path.
735 * There are two potential problems with the use of this API:
737 * o Because the write path only set it on a successful
738 * write, there is a race window between setting the
739 * credential and its use to evict the pages to the
742 * o Because a page-in may occur prior to a write, the
743 * credential may not be set at this time, if the page-in
744 * is not the result of a mapping established via mmap()
747 * In both these cases, this will be triggered from the paging
748 * path, which will instead use the credential of the current
749 * process, which in this case is either the dynamic_pager or
750 * the kernel task, both of which utilize "root" credentials.
752 * This may potentially permit operations to occur which should
753 * be denied, or it may cause to be denied operations which
754 * should be permitted, depending on the configuration of the NFS
758 ubc_setthreadcred(struct vnode
*vp
, proc_t p
, thread_t thread
)
760 struct ubc_info
*uip
;
762 struct uthread
*uthread
= get_bsdthread_info(thread
);
764 if (!UBCINFOEXISTS(vp
))
770 credp
= uip
->ui_ucred
;
772 if (!IS_VALID_CRED(credp
)) {
773 /* use per-thread cred, if assumed identity, else proc cred */
774 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
775 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
777 uip
->ui_ucred
= uthread
->uu_ucred
;
778 kauth_cred_ref(uip
->ui_ucred
);
790 * If they are not already set, set the credentials of the ubc_info structure
791 * associated with the vnode to those of the process; otherwise leave them
794 * Parameters: vp The vnode whose ubc_info creds are to
796 * p The process whose credentials are to
799 * Returns: 0 This vnode has no associated ubc_info
802 * Notes: The return values for this function are inverted from nearly
803 * all other uses in the kernel.
805 * See also ubc_setthreadcred(), above.
807 * This function is considered deprecated, and generally should
808 * not be used, as it is incompatible with per-thread credentials;
809 * it exists for legacy KPI reasons.
811 * DEPRECATION: ubc_setcred() is being deprecated. Please use
812 * ubc_setthreadcred() instead.
815 ubc_setcred(struct vnode
*vp
, proc_t p
)
817 struct ubc_info
*uip
;
820 /* If there is no ubc_info, deny the operation */
821 if ( !UBCINFOEXISTS(vp
))
825 * Check to see if there is already a credential reference in the
826 * ubc_info; if there is not, take one on the supplied credential.
830 credp
= uip
->ui_ucred
;
831 if (!IS_VALID_CRED(credp
)) {
832 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
843 * Get the pager associated with the ubc_info associated with the vnode.
845 * Parameters: vp The vnode to obtain the pager from
847 * Returns: !VNODE_PAGER_NULL The memory_object_t for the pager
848 * VNODE_PAGER_NULL There is no ubc_info for this vnode
850 * Notes: For each vnode that has a ubc_info associated with it, that
851 * ubc_info SHALL have a pager associated with it, so in the
852 * normal case, it's impossible to return VNODE_PAGER_NULL for
853 * a vnode with an associated ubc_info.
855 __private_extern__ memory_object_t
856 ubc_getpager(struct vnode
*vp
)
858 if (UBCINFOEXISTS(vp
))
859 return (vp
->v_ubcinfo
->ui_pager
);
868 * Get the memory object control associated with the ubc_info associated with
871 * Parameters: vp The vnode to obtain the memory object
875 * Returns: !MEMORY_OBJECT_CONTROL_NULL
876 * MEMORY_OBJECT_CONTROL_NULL
878 * Notes: Historically, if the flags were not "do not reactivate", this
879 * function would look up the memory object using the pager if
880 * it did not exist (this could be the case if the vnode had
881 * been previously reactivated). The flags would also permit a
882 * hold to be requested, which would have created an object
883 * reference, if one had not already existed. This usage is
884 * deprecated, as it would permit a race between finding and
885 * taking the reference vs. a single reference being dropped in
888 memory_object_control_t
889 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
891 if (UBCINFOEXISTS(vp
))
892 return((vp
->v_ubcinfo
->ui_control
));
894 return (MEMORY_OBJECT_CONTROL_NULL
);
901 * Convert a given block number to a memory backing object (file) offset for a
904 * Parameters: vp The vnode in which the block is located
905 * blkno The block number to convert
907 * Returns: !-1 The offset into the backing object
908 * -1 There is no ubc_info associated with
910 * -1 An error occurred in the underlying VFS
911 * while translating the block to an
912 * offset; the most likely cause is that
913 * the caller specified a block past the
914 * end of the file, but this could also be
915 * any other error from VNOP_BLKTOOFF().
917 * Note: Representing the error in band loses some information, but does
918 * not occlude a valid offset, since an off_t of -1 is normally
919 * used to represent EOF. If we had a more reliable constant in
920 * our header files for it (i.e. explicitly cast to an off_t), we
921 * would use it here instead.
924 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
926 off_t file_offset
= -1;
929 if (UBCINFOEXISTS(vp
)) {
930 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
935 return (file_offset
);
942 * Convert a given offset in a memory backing object into a block number for a
945 * Parameters: vp The vnode in which the offset is
947 * offset The offset into the backing object
949 * Returns: !-1 The returned block number
950 * -1 There is no ubc_info associated with
952 * -1 An error occurred in the underlying VFS
953 * while translating the block to an
954 * offset; the most likely cause is that
955 * the caller specified a block past the
956 * end of the file, but this could also be
957 * any other error from VNOP_OFFTOBLK().
959 * Note: Representing the error in band loses some information, but does
960 * not occlude a valid block number, since block numbers exceed
961 * the valid range for offsets, due to their relative sizes. If
962 * we had a more reliable constant than -1 in our header files
963 * for it (i.e. explicitly cast to an daddr64_t), we would use it
967 ubc_offtoblk(vnode_t vp
, off_t offset
)
969 daddr64_t blkno
= -1;
972 if (UBCINFOEXISTS(vp
)) {
973 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
985 * Determine whether or not a given vnode has pages resident via the memory
986 * object control associated with the ubc_info associated with the vnode
988 * Parameters: vp The vnode we want to know about
994 ubc_pages_resident(vnode_t vp
)
997 boolean_t has_pages_resident
;
999 if (!UBCINFOEXISTS(vp
))
1003 * The following call may fail if an invalid ui_control is specified,
1004 * or if there is no VM object associated with the control object. In
1005 * either case, reacting to it as if there were no pages resident will
1006 * result in correct behavior.
1008 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
1010 if (kret
!= KERN_SUCCESS
)
1013 if (has_pages_resident
== TRUE
)
1023 * Clean and/or invalidate a range in the memory object that backs this vnode
1025 * Parameters: vp The vnode whose associated ubc_info's
1026 * associated memory object is to have a
1027 * range invalidated within it
1028 * beg_off The start of the range, as an offset
1029 * end_off The end of the range, as an offset
1030 * flags See ubc_msync_internal()
1032 * Returns: 1 Success
1035 * Notes: see ubc_msync_internal() for more detailed information.
1037 * DEPRECATED: This interface is obsolete due to a failure to return error
1038 * information needed in order to correct failures. The currently
1039 * recommended interface is ubc_msync().
1042 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
1044 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
1051 * Clean and/or invalidate a range in the memory object that backs this vnode
1053 * Parameters: vp The vnode whose associated ubc_info's
1054 * associated memory object is to have a
1055 * range invalidated within it
1056 * beg_off The start of the range, as an offset
1057 * end_off The end of the range, as an offset
1058 * resid_off The address of an off_t supplied by the
1059 * caller; may be set to NULL to ignore
1060 * flags See ubc_msync_internal()
1062 * Returns: 0 Success
1063 * !0 Failure; an errno is returned
1066 * *resid_off, modified If non-NULL, the contents are ALWAYS
1067 * modified; they are initialized to the
1068 * beg_off, and in case of an I/O error,
1069 * the difference between beg_off and the
1070 * current value will reflect what was
1071 * able to be written before the error
1072 * occurred. If no error is returned, the
1073 * value of the resid_off is undefined; do
1074 * NOT use it in place of end_off if you
1075 * intend to increment from the end of the
1076 * last call and call iteratively.
1078 * Notes: see ubc_msync_internal() for more detailed information.
1082 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
1088 *resid_off
= beg_off
;
1090 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
1092 if (retval
== 0 && io_errno
== 0)
1099 * Clean and/or invalidate a range in the memory object that backs this vnode
1101 * Parameters: vp The vnode whose associated ubc_info's
1102 * associated memory object is to have a
1103 * range invalidated within it
1104 * beg_off The start of the range, as an offset
1105 * end_off The end of the range, as an offset
1106 * resid_off The address of an off_t supplied by the
1107 * caller; may be set to NULL to ignore
1108 * flags MUST contain at least one of the flags
1109 * UBC_INVALIDATE, UBC_PUSHDIRTY, or
1110 * UBC_PUSHALL; if UBC_PUSHDIRTY is used,
1111 * UBC_SYNC may also be specified to cause
1112 * this function to block until the
1113 * operation is complete. The behavior
1114 * of UBC_SYNC is otherwise undefined.
1115 * io_errno The address of an int to contain the
1116 * errno from a failed I/O operation, if
1117 * one occurs; may be set to NULL to
1120 * Returns: 1 Success
1124 * *resid_off, modified The contents of this offset MAY be
1125 * modified; in case of an I/O error, the
1126 * difference between beg_off and the
1127 * current value will reflect what was
1128 * able to be written before the error
1130 * *io_errno, modified The contents of this offset are set to
1131 * an errno, if an error occurs; if the
1132 * caller supplies an io_errno parameter,
1133 * they should be careful to initialize it
1134 * to 0 before calling this function to
1135 * enable them to distinguish an error
1136 * with a valid *resid_off from an invalid
1137 * one, and to avoid potentially falsely
1138 * reporting an error, depending on use.
1140 * Notes: If there is no ubc_info associated with the vnode supplied,
1141 * this function immediately returns success.
1143 * If the value of end_off is less than or equal to beg_off, this
1144 * function immediately returns success; that is, end_off is NOT
1147 * IMPORTANT: one of the flags UBC_INVALIDATE, UBC_PUSHDIRTY, or
1148 * UBC_PUSHALL MUST be specified; that is, it is NOT possible to
1149 * attempt to block on in-progress I/O by calling this function
1150 * with UBC_PUSHDIRTY, and then later call it with just UBC_SYNC
1151 * in order to block pending on the I/O already in progress.
1153 * The start offset is truncated to the page boundary and the
1154 * size is adjusted to include the last page in the range; that
1155 * is, end_off on exactly a page boundary will not change if it
1156 * is rounded, and the range of bytes written will be from the
1157 * truncate beg_off to the rounded (end_off - 1).
1160 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
1162 memory_object_size_t tsize
;
1164 int request_flags
= 0;
1165 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
1167 if ( !UBCINFOEXISTS(vp
))
1169 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
1171 if (end_off
<= beg_off
)
1174 if (flags
& UBC_INVALIDATE
)
1176 * discard the resident pages
1178 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
1180 if (flags
& UBC_SYNC
)
1182 * wait for all the I/O to complete before returning
1184 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
1186 if (flags
& UBC_PUSHDIRTY
)
1188 * we only return the dirty pages in the range
1190 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
1192 if (flags
& UBC_PUSHALL
)
1194 * then return all the interesting pages in the range (both
1195 * dirty and precious) to the pager
1197 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
1199 beg_off
= trunc_page_64(beg_off
);
1200 end_off
= round_page_64(end_off
);
1201 tsize
= (memory_object_size_t
)end_off
- beg_off
;
1203 /* flush and/or invalidate pages in the range requested */
1204 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
1206 (memory_object_offset_t
*)resid_off
,
1207 io_errno
, flush_flags
, request_flags
,
1210 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
1215 * ubc_msync_internal
1217 * Explicitly map a vnode that has an associate ubc_info, and add a reference
1218 * to it for the ubc system, if there isn't one already, so it will not be
1219 * recycled while it's in use, and set flags on the ubc_info to indicate that
1222 * Parameters: vp The vnode to map
1223 * flags The mapping flags for the vnode; this
1224 * will be a combination of one or more of
1225 * PROT_READ, PROT_WRITE, and PROT_EXEC
1227 * Returns: 0 Success
1228 * EPERM Permission was denied
1230 * Notes: An I/O reference on the vnode must already be held on entry
1232 * If there is no ubc_info associated with the vnode, this function
1233 * will return success.
1235 * If a permission error occurs, this function will return
1236 * failure; all other failures will cause this function to return
1239 * IMPORTANT: This is an internal use function, and its symbols
1240 * are not exported, hence its error checking is not very robust.
1241 * It is primarily used by:
1243 * o mmap(), when mapping a file
1244 * o The deprecated map_fd() interface, when mapping a file
1245 * o When mapping a shared file (a shared library in the
1246 * shared segment region)
1247 * o When loading a program image during the exec process
1249 * ...all of these uses ignore the return code, and any fault that
1250 * results later because of a failure is handled in the fix-up path
1251 * of the fault handler. The interface exists primarily as a
1254 * Given that third party implementation of the type of interfaces
1255 * that would use this function, such as alternative executable
1256 * formats, etc., are unsupported, this function is not exported
1259 * The extra reference is held until the VM system unmaps the
1260 * vnode from its own context to maintain a vnode reference in
1261 * cases like open()/mmap()/close(), which leave the backing
1262 * object referenced by a mapped memory region in a process
1265 __private_extern__
int
1266 ubc_map(vnode_t vp
, int flags
)
1268 struct ubc_info
*uip
;
1271 int need_wakeup
= 0;
1273 if (UBCINFOEXISTS(vp
)) {
1276 uip
= vp
->v_ubcinfo
;
1278 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1279 SET(uip
->ui_flags
, UI_MAPWAITING
);
1280 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1281 PRIBIO
, "ubc_map", NULL
);
1283 SET(uip
->ui_flags
, UI_MAPBUSY
);
1286 error
= VNOP_MMAP(vp
, flags
, vfs_context_current());
1291 vnode_lock_spin(vp
);
1294 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
1296 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
1298 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1300 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1301 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1307 wakeup(&uip
->ui_flags
);
1319 * Destroy the named memory object associated with the ubc_info control object
1320 * associated with the designated vnode, if there is a ubc_info associated
1321 * with the vnode, and a control object is associated with it
1323 * Parameters: vp The designated vnode
1327 * Notes: This function is called on vnode termination for all vnodes,
1328 * and must therefore not assume that there is a ubc_info that is
1329 * associated with the vnode, nor that there is a control object
1330 * associated with the ubc_info.
1332 * If all the conditions necessary are present, this function
1333 * calls memory_object_destory(), which will in turn end up
1334 * calling ubc_unmap() to release any vnode references that were
1335 * established via ubc_map().
1337 * IMPORTANT: This is an internal use function that is used
1338 * exclusively by the internal use function vclean().
1340 __private_extern__
void
1341 ubc_destroy_named(vnode_t vp
)
1343 memory_object_control_t control
;
1344 struct ubc_info
*uip
;
1347 if (UBCINFOEXISTS(vp
)) {
1348 uip
= vp
->v_ubcinfo
;
1350 /* Terminate the memory object */
1351 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
1352 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1353 kret
= memory_object_destroy(control
, 0);
1354 if (kret
!= KERN_SUCCESS
)
1355 panic("ubc_destroy_named: memory_object_destroy failed");
1364 * Determine whether or not a vnode is currently in use by ubc at a level in
1365 * excess of the requested busycount
1367 * Parameters: vp The vnode to check
1368 * busycount The threshold busy count, used to bias
1369 * the count usually already held by the
1370 * caller to avoid races
1372 * Returns: 1 The vnode is in use over the threshold
1373 * 0 The vnode is not in use over the
1376 * Notes: Because the vnode is only held locked while actually asking
1377 * the use count, this function only represents a snapshot of the
1378 * current state of the vnode. If more accurate information is
1379 * required, an additional busycount should be held by the caller
1380 * and a non-zero busycount used.
1382 * If there is no ubc_info associated with the vnode, this
1383 * function will report that the vnode is not in use by ubc.
1386 ubc_isinuse(struct vnode
*vp
, int busycount
)
1388 if ( !UBCINFOEXISTS(vp
))
1390 return(ubc_isinuse_locked(vp
, busycount
, 0));
1395 * ubc_isinuse_locked
1397 * Determine whether or not a vnode is currently in use by ubc at a level in
1398 * excess of the requested busycount
1400 * Parameters: vp The vnode to check
1401 * busycount The threshold busy count, used to bias
1402 * the count usually already held by the
1403 * caller to avoid races
1404 * locked True if the vnode is already locked by
1407 * Returns: 1 The vnode is in use over the threshold
1408 * 0 The vnode is not in use over the
1411 * Notes: If the vnode is not locked on entry, it is locked while
1412 * actually asking the use count. If this is the case, this
1413 * function only represents a snapshot of the current state of
1414 * the vnode. If more accurate information is required, the
1415 * vnode lock should be held by the caller, otherwise an
1416 * additional busycount should be held by the caller and a
1417 * non-zero busycount used.
1419 * If there is no ubc_info associated with the vnode, this
1420 * function will report that the vnode is not in use by ubc.
1423 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
1431 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
1443 * Reverse the effects of a ubc_map() call for a given vnode
1445 * Parameters: vp vnode to unmap from ubc
1449 * Notes: This is an internal use function used by vnode_pager_unmap().
1450 * It will attempt to obtain a reference on the supplied vnode,
1451 * and if it can do so, and there is an associated ubc_info, and
1452 * the flags indicate that it was mapped via ubc_map(), then the
1453 * flag is cleared, the mapping removed, and the reference taken
1454 * by ubc_map() is released.
1456 * IMPORTANT: This MUST only be called by the VM
1457 * to prevent race conditions.
1459 __private_extern__
void
1460 ubc_unmap(struct vnode
*vp
)
1462 struct ubc_info
*uip
;
1464 int need_wakeup
= 0;
1469 if (vnode_getwithref(vp
))
1472 if (UBCINFOEXISTS(vp
)) {
1474 uip
= vp
->v_ubcinfo
;
1476 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1477 SET(uip
->ui_flags
, UI_MAPWAITING
);
1478 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1479 PRIBIO
, "ubc_unmap", NULL
);
1481 SET(uip
->ui_flags
, UI_MAPBUSY
);
1484 if ((vp
->v_flag
& VISNAMEDSTREAM
) &&
1485 (vp
->v_parent
!= NULLVP
) &&
1486 !(vp
->v_parent
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
)) {
1491 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
1492 CLR(uip
->ui_flags
, UI_ISMAPPED
);
1498 (void)VNOP_MNOMAP(vp
, vfs_context_current());
1502 vnode_relenamedstream(vp
->v_parent
, vp
, vfs_context_current());
1509 vnode_lock_spin(vp
);
1511 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1512 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1513 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1519 wakeup(&uip
->ui_flags
);
1523 * the drop of the vnode ref will cleanup
1532 * Manipulate individual page state for a vnode with an associated ubc_info
1533 * with an associated memory object control.
1535 * Parameters: vp The vnode backing the page
1536 * f_offset A file offset interior to the page
1537 * ops The operations to perform, as a bitmap
1538 * (see below for more information)
1539 * phys_entryp The address of a ppnum_t; may be NULL
1541 * flagsp A pointer to an int to contain flags;
1542 * may be NULL to ignore
1544 * Returns: KERN_SUCCESS Success
1545 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1547 * KERN_INVALID_OBJECT If UPL_POP_PHYSICAL and the object is
1548 * not physically contiguous
1549 * KERN_INVALID_OBJECT If !UPL_POP_PHYSICAL and the object is
1550 * physically contiguous
1551 * KERN_FAILURE If the page cannot be looked up
1554 * *phys_entryp (modified) If phys_entryp is non-NULL and
1556 * *flagsp (modified) If flagsp is non-NULL and there was
1557 * !UPL_POP_PHYSICAL and a KERN_SUCCESS
1559 * Notes: For object boundaries, it is considerably more efficient to
1560 * ensure that f_offset is in fact on a page boundary, as this
1561 * will avoid internal use of the hash table to identify the
1562 * page, and would therefore skip a number of early optimizations.
1563 * Since this is a page operation anyway, the caller should try
1564 * to pass only a page aligned offset because of this.
1566 * *flagsp may be modified even if this function fails. If it is
1567 * modified, it will contain the condition of the page before the
1568 * requested operation was attempted; these will only include the
1569 * bitmap flags, and not the PL_POP_PHYSICAL, UPL_POP_DUMP,
1570 * UPL_POP_SET, or UPL_POP_CLR bits.
1572 * The flags field may contain a specific operation, such as
1573 * UPL_POP_PHYSICAL or UPL_POP_DUMP:
1575 * o UPL_POP_PHYSICAL Fail if not contiguous; if
1576 * *phys_entryp and successful, set
1578 * o UPL_POP_DUMP Dump the specified page
1580 * Otherwise, it is treated as a bitmap of one or more page
1581 * operations to perform on the final memory object; allowable
1584 * o UPL_POP_DIRTY The page is dirty
1585 * o UPL_POP_PAGEOUT The page is paged out
1586 * o UPL_POP_PRECIOUS The page is precious
1587 * o UPL_POP_ABSENT The page is absent
1588 * o UPL_POP_BUSY The page is busy
1590 * If the page status is only being queried and not modified, then
1591 * not other bits should be specified. However, if it is being
1592 * modified, exactly ONE of the following bits should be set:
1594 * o UPL_POP_SET Set the current bitmap bits
1595 * o UPL_POP_CLR Clear the current bitmap bits
1597 * Thus to effect a combination of setting an clearing, it may be
1598 * necessary to call this function twice. If this is done, the
1599 * set should be used before the clear, since clearing may trigger
1600 * a wakeup on the destination page, and if the page is backed by
1601 * an encrypted swap file, setting will trigger the decryption
1602 * needed before the wakeup occurs.
1609 ppnum_t
*phys_entryp
,
1612 memory_object_control_t control
;
1614 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1615 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1616 return KERN_INVALID_ARGUMENT
;
1618 return (memory_object_page_op(control
,
1619 (memory_object_offset_t
)f_offset
,
1629 * Manipulate page state for a range of memory for a vnode with an associated
1630 * ubc_info with an associated memory object control, when page level state is
1631 * not required to be returned from the call (i.e. there are no phys_entryp or
1632 * flagsp parameters to this call, and it takes a range which may contain
1633 * multiple pages, rather than an offset interior to a single page).
1635 * Parameters: vp The vnode backing the page
1636 * f_offset_beg A file offset interior to the start page
1637 * f_offset_end A file offset interior to the end page
1638 * ops The operations to perform, as a bitmap
1639 * (see below for more information)
1640 * range The address of an int; may be NULL to
1643 * Returns: KERN_SUCCESS Success
1644 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1646 * KERN_INVALID_OBJECT If the object is physically contiguous
1649 * *range (modified) If range is non-NULL, its contents will
1650 * be modified to contain the number of
1651 * bytes successfully operated upon.
1653 * Notes: IMPORTANT: This function cannot be used on a range that
1654 * consists of physically contiguous pages.
1656 * For object boundaries, it is considerably more efficient to
1657 * ensure that f_offset_beg and f_offset_end are in fact on page
1658 * boundaries, as this will avoid internal use of the hash table
1659 * to identify the page, and would therefore skip a number of
1660 * early optimizations. Since this is an operation on a set of
1661 * pages anyway, the caller should try to pass only a page aligned
1662 * offsets because of this.
1664 * *range will be modified only if this function succeeds.
1666 * The flags field MUST contain a specific operation; allowable
1669 * o UPL_ROP_ABSENT Returns the extent of the range
1670 * presented which is absent, starting
1671 * with the start address presented
1673 * o UPL_ROP_PRESENT Returns the extent of the range
1674 * presented which is present (resident),
1675 * starting with the start address
1677 * o UPL_ROP_DUMP Dump the pages which are found in the
1678 * target object for the target range.
1680 * IMPORTANT: For UPL_ROP_ABSENT and UPL_ROP_PRESENT; if there are
1681 * multiple regions in the range, only the first matching region
1692 memory_object_control_t control
;
1694 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1695 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1696 return KERN_INVALID_ARGUMENT
;
1698 return (memory_object_range_op(control
,
1699 (memory_object_offset_t
)f_offset_beg
,
1700 (memory_object_offset_t
)f_offset_end
,
1709 * Given a vnode, cause the population of a portion of the vm_object; based on
1710 * the nature of the request, the pages returned may contain valid data, or
1711 * they may be uninitialized.
1713 * Parameters: vp The vnode from which to create the upl
1714 * f_offset The start offset into the backing store
1715 * represented by the vnode
1716 * bufsize The size of the upl to create
1717 * uplp Pointer to the upl_t to receive the
1718 * created upl; MUST NOT be NULL
1719 * plp Pointer to receive the internal page
1720 * list for the created upl; MAY be NULL
1723 * Returns: KERN_SUCCESS The requested upl has been created
1724 * KERN_INVALID_ARGUMENT The bufsize argument is not an even
1725 * multiple of the page size
1726 * KERN_INVALID_ARGUMENT There is no ubc_info associated with
1727 * the vnode, or there is no memory object
1728 * control associated with the ubc_info
1729 * memory_object_upl_request:KERN_INVALID_VALUE
1730 * The supplied upl_flags argument is
1734 * *plp (modified) If non-NULL, the value of *plp will be
1735 * modified to point to the internal page
1736 * list; this modification may occur even
1737 * if this function is unsuccessful, in
1738 * which case the contents may be invalid
1740 * Note: If successful, the returned *uplp MUST subsequently be freed
1741 * via a call to ubc_upl_commit(), ubc_upl_commit_range(),
1742 * ubc_upl_abort(), or ubc_upl_abort_range().
1750 upl_page_info_t
**plp
,
1753 memory_object_control_t control
;
1754 mach_msg_type_number_t count
;
1758 if (bufsize
& 0xfff)
1759 return KERN_INVALID_ARGUMENT
;
1761 if (uplflags
& UPL_FOR_PAGEOUT
) {
1762 uplflags
&= ~UPL_FOR_PAGEOUT
;
1763 ubcflags
= UBC_FOR_PAGEOUT
;
1765 ubcflags
= UBC_FLAGS_NONE
;
1767 control
= ubc_getobject(vp
, ubcflags
);
1768 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1769 return KERN_INVALID_ARGUMENT
;
1771 if (uplflags
& UPL_WILL_BE_DUMPED
) {
1772 uplflags
&= ~UPL_WILL_BE_DUMPED
;
1773 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
1775 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
1778 kr
= memory_object_upl_request(control
, f_offset
, bufsize
, uplp
, NULL
, &count
, uplflags
);
1780 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
1786 * ubc_upl_maxbufsize
1788 * Return the maximum bufsize ubc_create_upl( ) will take.
1792 * Returns: maximum size buffer (in bytes) ubc_create_upl( ) will take.
1798 return(MAX_UPL_TRANSFER
* PAGE_SIZE
);
1804 * Map the page list assocated with the supplied upl into the kernel virtual
1805 * address space at the virtual address indicated by the dst_addr argument;
1806 * the entire upl is mapped
1808 * Parameters: upl The upl to map
1809 * dst_addr The address at which to map the upl
1811 * Returns: KERN_SUCCESS The upl has been mapped
1812 * KERN_INVALID_ARGUMENT The upl is UPL_NULL
1813 * KERN_FAILURE The upl is already mapped
1814 * vm_map_enter:KERN_INVALID_ARGUMENT
1815 * A failure code from vm_map_enter() due
1816 * to an invalid argument
1821 vm_offset_t
*dst_addr
)
1823 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
1830 * Unmap the page list assocated with the supplied upl from the kernel virtual
1831 * address space; the entire upl is unmapped.
1833 * Parameters: upl The upl to unmap
1835 * Returns: KERN_SUCCESS The upl has been unmapped
1836 * KERN_FAILURE The upl is not currently mapped
1837 * KERN_INVALID_ARGUMENT If the upl is UPL_NULL
1843 return(vm_upl_unmap(kernel_map
, upl
));
1850 * Commit the contents of the upl to the backing store
1852 * Parameters: upl The upl to commit
1854 * Returns: KERN_SUCCESS The upl has been committed
1855 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
1856 * KERN_FAILURE The supplied upl does not represent
1857 * device memory, and the offset plus the
1858 * size would exceed the actual size of
1861 * Notes: In practice, the only return value for this function should be
1862 * KERN_SUCCESS, unless there has been data structure corruption;
1863 * since the upl is deallocated regardless of success or failure,
1864 * there's really nothing to do about this other than panic.
1866 * IMPORTANT: Use of this function should not be mixed with use of
1867 * ubc_upl_commit_range(), due to the unconditional deallocation
1874 upl_page_info_t
*pl
;
1877 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1878 kr
= upl_commit(upl
, pl
, MAX_UPL_TRANSFER
);
1879 upl_deallocate(upl
);
1887 * Commit the contents of the specified range of the upl to the backing store
1889 * Parameters: upl The upl to commit
1890 * offset The offset into the upl
1891 * size The size of the region to be committed,
1892 * starting at the specified offset
1893 * flags commit type (see below)
1895 * Returns: KERN_SUCCESS The range has been committed
1896 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
1897 * KERN_FAILURE The supplied upl does not represent
1898 * device memory, and the offset plus the
1899 * size would exceed the actual size of
1902 * Notes: IMPORTANT: If the commit is successful, and the object is now
1903 * empty, the upl will be deallocated. Since the caller cannot
1904 * check that this is the case, the UPL_COMMIT_FREE_ON_EMPTY flag
1905 * should generally only be used when the offset is 0 and the size
1906 * is equal to the upl size.
1908 * The flags argument is a bitmap of flags on the rage of pages in
1909 * the upl to be committed; allowable flags are:
1911 * o UPL_COMMIT_FREE_ON_EMPTY Free the upl when it is
1912 * both empty and has been
1913 * successfully committed
1914 * o UPL_COMMIT_CLEAR_DIRTY Clear each pages dirty
1915 * bit; will prevent a
1917 * o UPL_COMMIT_SET_DIRTY Set each pages dirty
1918 * bit; will cause a later
1920 * o UPL_COMMIT_INACTIVATE Clear each pages
1921 * reference bit; the page
1922 * will not be accessed
1923 * o UPL_COMMIT_ALLOW_ACCESS Unbusy each page; pages
1924 * become busy when an
1925 * IOMemoryDescriptor is
1926 * mapped or redirected,
1927 * and we have to wait for
1930 * The flag UPL_COMMIT_NOTIFY_EMPTY is used internally, and should
1931 * not be specified by the caller.
1933 * The UPL_COMMIT_CLEAR_DIRTY and UPL_COMMIT_SET_DIRTY flags are
1934 * mutually exclusive, and should not be combined.
1937 ubc_upl_commit_range(
1943 upl_page_info_t
*pl
;
1947 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
1948 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
1950 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1952 kr
= upl_commit_range(upl
, offset
, size
, flags
,
1953 pl
, MAX_UPL_TRANSFER
, &empty
);
1955 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
1956 upl_deallocate(upl
);
1963 * ubc_upl_abort_range
1965 * Abort the contents of the specified range of the specified upl
1967 * Parameters: upl The upl to abort
1968 * offset The offset into the upl
1969 * size The size of the region to be aborted,
1970 * starting at the specified offset
1971 * abort_flags abort type (see below)
1973 * Returns: KERN_SUCCESS The range has been aborted
1974 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
1975 * KERN_FAILURE The supplied upl does not represent
1976 * device memory, and the offset plus the
1977 * size would exceed the actual size of
1980 * Notes: IMPORTANT: If the abort is successful, and the object is now
1981 * empty, the upl will be deallocated. Since the caller cannot
1982 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
1983 * should generally only be used when the offset is 0 and the size
1984 * is equal to the upl size.
1986 * The abort_flags argument is a bitmap of flags on the range of
1987 * pages in the upl to be aborted; allowable flags are:
1989 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
1990 * empty and has been successfully
1992 * o UPL_ABORT_RESTART The operation must be restarted
1993 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
1994 * o UPL_ABORT_ERROR An I/O error occurred
1995 * o UPL_ABORT_DUMP_PAGES Just free the pages
1996 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
1997 * o UPL_ABORT_ALLOW_ACCESS RESERVED
1999 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2000 * not be specified by the caller. It is intended to fulfill the
2001 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2002 * ubc_upl_commit_range(), but is never referenced internally.
2004 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2005 * referenced; do not use it.
2008 ubc_upl_abort_range(
2015 boolean_t empty
= FALSE
;
2017 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
2018 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
2020 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
2022 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
2023 upl_deallocate(upl
);
2032 * Abort the contents of the specified upl
2034 * Parameters: upl The upl to abort
2035 * abort_type abort type (see below)
2037 * Returns: KERN_SUCCESS The range has been aborted
2038 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2039 * KERN_FAILURE The supplied upl does not represent
2040 * device memory, and the offset plus the
2041 * size would exceed the actual size of
2044 * Notes: IMPORTANT: If the abort is successful, and the object is now
2045 * empty, the upl will be deallocated. Since the caller cannot
2046 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2047 * should generally only be used when the offset is 0 and the size
2048 * is equal to the upl size.
2050 * The abort_type is a bitmap of flags on the range of
2051 * pages in the upl to be aborted; allowable flags are:
2053 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2054 * empty and has been successfully
2056 * o UPL_ABORT_RESTART The operation must be restarted
2057 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2058 * o UPL_ABORT_ERROR An I/O error occurred
2059 * o UPL_ABORT_DUMP_PAGES Just free the pages
2060 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2061 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2063 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2064 * not be specified by the caller. It is intended to fulfill the
2065 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2066 * ubc_upl_commit_range(), but is never referenced internally.
2068 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2069 * referenced; do not use it.
2078 kr
= upl_abort(upl
, abort_type
);
2079 upl_deallocate(upl
);
2087 * Retrieve the internal page list for the specified upl
2089 * Parameters: upl The upl to obtain the page list from
2091 * Returns: !NULL The (upl_page_info_t *) for the page
2092 * list internal to the upl
2093 * NULL Error/no page list associated
2095 * Notes: IMPORTANT: The function is only valid on internal objects
2096 * where the list request was made with the UPL_INTERNAL flag.
2098 * This function is a utility helper function, since some callers
2099 * may not have direct access to the header defining the macro,
2100 * due to abstraction layering constraints.
2106 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
2111 UBCINFOEXISTS(struct vnode
* vp
)
2113 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
2120 #define CS_BLOB_KEEP_IN_KERNEL 1
2121 static volatile SInt32 cs_blob_size
= 0;
2122 static volatile SInt32 cs_blob_count
= 0;
2123 static SInt32 cs_blob_size_peak
= 0;
2124 static UInt32 cs_blob_size_max
= 0;
2125 static SInt32 cs_blob_count_peak
= 0;
2126 extern int cs_debug
;
2128 int cs_validation
= 1;
2130 SYSCTL_INT(_vm
, OID_AUTO
, cs_validation
, CTLFLAG_RW
, &cs_validation
, 0, "Do validate code signatures");
2131 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count
, CTLFLAG_RD
, &cs_blob_count
, 0, "Current number of code signature blobs");
2132 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size
, CTLFLAG_RD
, &cs_blob_size
, 0, "Current size of all code signature blobs");
2133 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count_peak
, CTLFLAG_RD
, &cs_blob_count_peak
, 0, "Peak number of code signature blobs");
2134 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_peak
, CTLFLAG_RD
, &cs_blob_size_peak
, 0, "Peak size of code signature blobs");
2135 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_max
, CTLFLAG_RD
, &cs_blob_size_max
, 0, "Size of biggest code signature blob");
2146 struct ubc_info
*uip
;
2147 struct cs_blob
*blob
, *oblob
;
2149 ipc_port_t blob_handle
;
2150 memory_object_size_t blob_size
;
2151 const CS_CodeDirectory
*cd
;
2152 off_t blob_start_offset
, blob_end_offset
;
2155 blob_handle
= IPC_PORT_NULL
;
2157 blob
= (struct cs_blob
*) kalloc(sizeof (struct cs_blob
));
2162 /* get a memory entry on the blob */
2163 blob_size
= (memory_object_size_t
) size
;
2164 kr
= mach_make_memory_entry_64(kernel_map
,
2170 if (kr
!= KERN_SUCCESS
) {
2174 if (memory_object_round_page(blob_size
) !=
2175 (memory_object_size_t
) round_page(size
)) {
2176 printf("ubc_cs_blob_add: size mismatch 0x%llx 0x%x !?\n",
2178 panic("XXX FBDP size mismatch 0x%llx 0x%x\n", blob_size
, size
);
2184 /* fill in the new blob */
2185 blob
->csb_cpu_type
= cputype
;
2186 blob
->csb_base_offset
= base_offset
;
2187 blob
->csb_mem_size
= size
;
2188 blob
->csb_mem_offset
= 0;
2189 blob
->csb_mem_handle
= blob_handle
;
2190 blob
->csb_mem_kaddr
= addr
;
2194 * Validate the blob's contents
2196 cd
= findCodeDirectory(
2197 (const CS_SuperBlob
*) addr
,
2199 (char *) addr
+ blob
->csb_mem_size
);
2201 /* no code directory => useless blob ! */
2202 blob
->csb_flags
= 0;
2203 blob
->csb_start_offset
= 0;
2204 blob
->csb_end_offset
= 0;
2206 unsigned char *sha1_base
;
2209 blob
->csb_flags
= ntohl(cd
->flags
) | CS_VALID
;
2210 blob
->csb_end_offset
= round_page(ntohl(cd
->codeLimit
));
2211 blob
->csb_start_offset
= (blob
->csb_end_offset
-
2212 (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
));
2213 /* compute the blob's SHA1 hash */
2214 sha1_base
= (const unsigned char *) cd
;
2215 sha1_size
= ntohl(cd
->length
);
2216 SHA1Init(&sha1ctxt
);
2217 SHA1Update(&sha1ctxt
, sha1_base
, sha1_size
);
2218 SHA1Final(blob
->csb_sha1
, &sha1ctxt
);
2223 * Validate the blob's coverage
2225 blob_start_offset
= blob
->csb_base_offset
+ blob
->csb_start_offset
;
2226 blob_end_offset
= blob
->csb_base_offset
+ blob
->csb_end_offset
;
2228 if (blob_start_offset
>= blob_end_offset
) {
2229 /* reject empty or backwards blob */
2235 if (! UBCINFOEXISTS(vp
)) {
2240 uip
= vp
->v_ubcinfo
;
2242 /* check if this new blob overlaps with an existing blob */
2243 for (oblob
= uip
->cs_blobs
;
2245 oblob
= oblob
->csb_next
) {
2246 off_t oblob_start_offset
, oblob_end_offset
;
2248 oblob_start_offset
= (oblob
->csb_base_offset
+
2249 oblob
->csb_start_offset
);
2250 oblob_end_offset
= (oblob
->csb_base_offset
+
2251 oblob
->csb_end_offset
);
2252 if (blob_start_offset
>= oblob_end_offset
||
2253 blob_end_offset
<= oblob_start_offset
) {
2254 /* no conflict with this existing blob */
2257 if (blob_start_offset
== oblob_start_offset
&&
2258 blob_end_offset
== oblob_end_offset
&&
2259 blob
->csb_mem_size
== oblob
->csb_mem_size
&&
2260 blob
->csb_flags
== oblob
->csb_flags
&&
2261 (blob
->csb_cpu_type
== CPU_TYPE_ANY
||
2262 oblob
->csb_cpu_type
== CPU_TYPE_ANY
||
2263 blob
->csb_cpu_type
== oblob
->csb_cpu_type
) &&
2264 !bcmp(blob
->csb_sha1
,
2268 * We already have this blob:
2269 * we'll return success but
2270 * throw away the new blob.
2272 if (oblob
->csb_cpu_type
== CPU_TYPE_ANY
) {
2274 * The old blob matches this one
2275 * but doesn't have any CPU type.
2276 * Update it with whatever the caller
2277 * provided this time.
2279 oblob
->csb_cpu_type
= cputype
;
2285 /* different blob: reject the new one */
2295 /* mark this vnode's VM object as having "signed pages" */
2296 kr
= memory_object_signed(uip
->ui_control
, TRUE
);
2297 if (kr
!= KERN_SUCCESS
) {
2304 * Add this blob to the list of blobs for this vnode.
2305 * We always add at the front of the list and we never remove a
2306 * blob from the list, so ubc_cs_get_blobs() can return whatever
2307 * the top of the list was and that list will remain valid
2308 * while we validate a page, even after we release the vnode's lock.
2310 blob
->csb_next
= uip
->cs_blobs
;
2311 uip
->cs_blobs
= blob
;
2313 OSAddAtomic(+1, &cs_blob_count
);
2314 if (cs_blob_count
> cs_blob_count_peak
) {
2315 cs_blob_count_peak
= cs_blob_count
; /* XXX atomic ? */
2317 OSAddAtomic(+blob
->csb_mem_size
, &cs_blob_size
);
2318 if (cs_blob_size
> cs_blob_size_peak
) {
2319 cs_blob_size_peak
= cs_blob_size
; /* XXX atomic ? */
2321 if (blob
->csb_mem_size
> cs_blob_size_max
) {
2322 cs_blob_size_max
= blob
->csb_mem_size
;
2329 printf("CODE SIGNING: proc %d(%s) "
2330 "loaded %s signatures for file (%s) "
2331 "range 0x%llx:0x%llx flags 0x%x\n",
2332 p
->p_pid
, p
->p_comm
,
2333 blob
->csb_cpu_type
== -1 ? "detached" : "embedded",
2335 blob
->csb_base_offset
+ blob
->csb_start_offset
,
2336 blob
->csb_base_offset
+ blob
->csb_end_offset
,
2340 #if !CS_BLOB_KEEP_IN_KERNEL
2341 blob
->csb_mem_kaddr
= 0;
2342 #endif /* CS_BLOB_KEEP_IN_KERNEL */
2346 error
= 0; /* success ! */
2350 /* we failed; release what we allocated */
2352 kfree(blob
, sizeof (*blob
));
2355 if (blob_handle
!= IPC_PORT_NULL
) {
2356 mach_memory_entry_port_release(blob_handle
);
2357 blob_handle
= IPC_PORT_NULL
;
2360 #if !CS_BLOB_KEEP_IN_KERNEL
2361 kmem_free(kernel_map
, addr
, size
);
2362 #endif /* CS_BLOB_KEEP_IN_KERNEL */
2365 if (error
== EAGAIN
) {
2367 * See above: error is EAGAIN if we were asked
2368 * to add an existing blob again. We cleaned the new
2369 * blob and we want to return success.
2373 * Since we're not failing, consume the data we received.
2375 kmem_free(kernel_map
, addr
, size
);
2388 struct ubc_info
*uip
;
2389 struct cs_blob
*blob
;
2390 off_t offset_in_blob
;
2392 vnode_lock_spin(vp
);
2394 if (! UBCINFOEXISTS(vp
)) {
2399 uip
= vp
->v_ubcinfo
;
2400 for (blob
= uip
->cs_blobs
;
2402 blob
= blob
->csb_next
) {
2403 if (cputype
!= -1 && blob
->csb_cpu_type
== cputype
) {
2407 offset_in_blob
= offset
- blob
->csb_base_offset
;
2408 if (offset_in_blob
>= blob
->csb_start_offset
&&
2409 offset_in_blob
< blob
->csb_end_offset
) {
2410 /* our offset is covered by this blob */
2424 struct ubc_info
*uip
)
2426 struct cs_blob
*blob
, *next_blob
;
2428 for (blob
= uip
->cs_blobs
;
2431 next_blob
= blob
->csb_next
;
2432 if (blob
->csb_mem_kaddr
!= 0) {
2433 kmem_free(kernel_map
,
2434 blob
->csb_mem_kaddr
,
2435 blob
->csb_mem_size
);
2436 blob
->csb_mem_kaddr
= 0;
2438 mach_memory_entry_port_release(blob
->csb_mem_handle
);
2439 blob
->csb_mem_handle
= IPC_PORT_NULL
;
2440 OSAddAtomic(-1, &cs_blob_count
);
2441 OSAddAtomic(-blob
->csb_mem_size
, &cs_blob_size
);
2442 kfree(blob
, sizeof (*blob
));
2444 uip
->cs_blobs
= NULL
;
2451 struct ubc_info
*uip
;
2452 struct cs_blob
*blobs
;
2454 vnode_lock_spin(vp
);
2456 if (! UBCINFOEXISTS(vp
)) {
2461 uip
= vp
->v_ubcinfo
;
2462 blobs
= uip
->cs_blobs
;
2470 unsigned long cs_validate_page_no_hash
= 0;
2471 unsigned long cs_validate_page_bad_hash
= 0;
2475 memory_object_offset_t page_offset
,
2480 unsigned char actual_hash
[SHA1_RESULTLEN
];
2481 unsigned char expected_hash
[SHA1_RESULTLEN
];
2482 boolean_t found_hash
;
2483 struct cs_blob
*blobs
, *blob
;
2484 const CS_CodeDirectory
*cd
;
2485 const CS_SuperBlob
*embedded
;
2486 off_t start_offset
, end_offset
;
2487 const unsigned char *hash
;
2488 boolean_t validated
;
2489 off_t offset
; /* page offset in the file */
2491 off_t codeLimit
= 0;
2492 char *lower_bound
, *upper_bound
;
2493 vm_offset_t kaddr
, blob_addr
;
2497 offset
= page_offset
;
2499 /* retrieve the expected hash */
2501 blobs
= (struct cs_blob
*) _blobs
;
2505 blob
= blob
->csb_next
) {
2506 offset
= page_offset
- blob
->csb_base_offset
;
2507 if (offset
< blob
->csb_start_offset
||
2508 offset
>= blob
->csb_end_offset
) {
2509 /* our page is not covered by this blob */
2513 /* map the blob in the kernel address space */
2514 kaddr
= blob
->csb_mem_kaddr
;
2516 ksize
= (vm_size_t
) (blob
->csb_mem_size
+
2517 blob
->csb_mem_offset
);
2518 kr
= vm_map(kernel_map
,
2523 blob
->csb_mem_handle
,
2529 if (kr
!= KERN_SUCCESS
) {
2530 /* XXX FBDP what to do !? */
2531 printf("cs_validate_page: failed to map blob, "
2532 "size=0x%x kr=0x%x\n",
2533 blob
->csb_mem_size
, kr
);
2537 blob_addr
= kaddr
+ blob
->csb_mem_offset
;
2539 lower_bound
= CAST_DOWN(char *, blob_addr
);
2540 upper_bound
= lower_bound
+ blob
->csb_mem_size
;
2542 embedded
= (const CS_SuperBlob
*) blob_addr
;
2543 cd
= findCodeDirectory(embedded
, lower_bound
, upper_bound
);
2545 if (cd
->pageSize
!= PAGE_SHIFT
||
2546 cd
->hashType
!= 0x1 ||
2547 cd
->hashSize
!= SHA1_RESULTLEN
) {
2549 #if !CS_BLOB_KEEP_IN_KERNEL
2550 kmem_free(kernel_map
, kaddr
, ksize
);
2551 #endif /* CS_BLOB_KEEP_IN_KERNEL */
2555 end_offset
= round_page(ntohl(cd
->codeLimit
));
2556 start_offset
= end_offset
- (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
);
2557 offset
= page_offset
- blob
->csb_base_offset
;
2558 if (offset
< start_offset
||
2559 offset
>= end_offset
) {
2560 /* our page is not covered by this blob */
2561 #if !CS_BLOB_KEEP_IN_KERNEL
2562 kmem_free(kernel_map
, kaddr
, ksize
);
2563 #endif /* CS_BLOB_KEEP_IN_KERNEL */
2567 codeLimit
= ntohl(cd
->codeLimit
);
2568 hash
= hashes(cd
, atop(offset
),
2569 lower_bound
, upper_bound
);
2570 bcopy(hash
, expected_hash
, sizeof (expected_hash
));
2573 #if !CS_BLOB_KEEP_IN_KERNEL
2574 /* we no longer need that blob in the kernel map */
2575 kmem_free(kernel_map
, kaddr
, ksize
);
2576 #endif /* CS_BLOB_KEEP_IN_KERNEL */
2582 if (found_hash
== FALSE
) {
2584 * We can't verify this page because there is no signature
2585 * for it (yet). It's possible that this part of the object
2586 * is not signed, or that signatures for that part have not
2588 * Report that the page has not been validated and let the
2589 * caller decide if it wants to accept it or not.
2591 cs_validate_page_no_hash
++;
2593 printf("CODE SIGNING: cs_validate_page: "
2594 "off 0x%llx: no hash to validate !?\n",
2600 const uint32_t *asha1
, *esha1
;
2603 if (offset
+ size
> codeLimit
) {
2604 /* partial page at end of segment */
2605 assert(offset
< codeLimit
);
2606 size
= codeLimit
& PAGE_MASK
;
2608 /* compute the actual page's SHA1 hash */
2609 SHA1Init(&sha1ctxt
);
2610 SHA1Update(&sha1ctxt
, data
, size
);
2611 SHA1Final(actual_hash
, &sha1ctxt
);
2613 asha1
= (const uint32_t *) actual_hash
;
2614 esha1
= (const uint32_t *) expected_hash
;
2616 if (bcmp(expected_hash
, actual_hash
, SHA1_RESULTLEN
) != 0) {
2618 printf("CODE SIGNING: cs_validate_page: "
2619 "off 0x%llx size 0x%lx: "
2620 "actual [0x%x 0x%x 0x%x 0x%x 0x%x] != "
2621 "expected [0x%x 0x%x 0x%x 0x%x 0x%x]\n",
2623 asha1
[0], asha1
[1], asha1
[2],
2625 esha1
[0], esha1
[1], esha1
[2],
2626 esha1
[3], esha1
[4]);
2628 cs_validate_page_bad_hash
++;
2632 printf("CODE SIGNING: cs_validate_page: "
2633 "off 0x%llx size 0x%lx: SHA1 OK\n",
2648 unsigned char *cdhash
)
2650 struct cs_blob
*blobs
, *blob
;
2653 blobs
= ubc_get_cs_blobs(vp
);
2656 blob
= blob
->csb_next
) {
2657 /* compute offset relative to this blob */
2658 rel_offset
= offset
- blob
->csb_base_offset
;
2659 if (rel_offset
>= blob
->csb_start_offset
&&
2660 rel_offset
< blob
->csb_end_offset
) {
2661 /* this blob does cover our "offset" ! */
2667 /* we didn't find a blob covering "offset" */
2668 return EBADEXEC
; /* XXX any better error ? */
2671 /* get the SHA1 hash of that blob */
2672 bcopy(blob
->csb_sha1
, cdhash
, sizeof (blob
->csb_sha1
));