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>
69 #include <libkern/libkern.h>
72 #include <sys/syslog.h>
74 #include <security/mac_framework.h>
76 /* XXX These should be in a BSD accessible Mach header, but aren't. */
77 extern kern_return_t
memory_object_pages_resident(memory_object_control_t
,
79 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
81 extern boolean_t
memory_object_is_slid(memory_object_control_t control
);
82 extern boolean_t
memory_object_is_signed(memory_object_control_t
);
84 extern void Debugger(const char *message
);
87 /* XXX no one uses this interface! */
88 kern_return_t
ubc_page_op_with_control(
89 memory_object_control_t control
,
100 #define assert(cond) \
101 ((void) ((cond) ? 0 : panic("Assert failed: %s", # cond)))
103 #include <kern/assert.h>
104 #endif /* DIAGNOSTIC */
106 static int ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
);
107 static int ubc_umcallback(vnode_t
, void *);
108 static int ubc_msync_internal(vnode_t
, off_t
, off_t
, off_t
*, int, int *);
109 static void ubc_cs_free(struct ubc_info
*uip
);
111 struct zone
*ubc_info_zone
;
116 * Routines to navigate code signing data structures in the kernel...
125 const void *lower_bound
,
126 const void *upper_bound
)
128 if (upper_bound
< lower_bound
||
133 if (start
< lower_bound
||
143 const unsigned char *hash
,
148 for (n
= 0; n
< len
; n
++)
149 snprintf(buf
+ 2*n
, 3, "%02.2x", hash
[n
]);
154 * Locate the CodeDirectory from an embedded signature blob
157 CS_CodeDirectory
*findCodeDirectory(
158 const CS_SuperBlob
*embedded
,
162 const CS_CodeDirectory
*cd
= NULL
;
165 cs_valid_range(embedded
, embedded
+ 1, lower_bound
, upper_bound
) &&
166 ntohl(embedded
->magic
) == CSMAGIC_EMBEDDED_SIGNATURE
) {
167 const CS_BlobIndex
*limit
;
168 const CS_BlobIndex
*p
;
170 limit
= &embedded
->index
[ntohl(embedded
->count
)];
171 if (!cs_valid_range(&embedded
->index
[0], limit
,
172 lower_bound
, upper_bound
)) {
175 for (p
= embedded
->index
; p
< limit
; ++p
) {
176 if (ntohl(p
->type
) == CSSLOT_CODEDIRECTORY
) {
177 const unsigned char *base
;
179 base
= (const unsigned char *)embedded
;
180 cd
= (const CS_CodeDirectory
*)(base
+ ntohl(p
->offset
));
186 * Detached signatures come as a bare CS_CodeDirectory,
189 cd
= (const CS_CodeDirectory
*) embedded
;
193 cs_valid_range(cd
, cd
+ 1, lower_bound
, upper_bound
) &&
194 cs_valid_range(cd
, (const char *) cd
+ ntohl(cd
->length
),
195 lower_bound
, upper_bound
) &&
196 cs_valid_range(cd
, (const char *) cd
+ ntohl(cd
->hashOffset
),
197 lower_bound
, upper_bound
) &&
198 cs_valid_range(cd
, (const char *) cd
+
199 ntohl(cd
->hashOffset
) +
200 (ntohl(cd
->nCodeSlots
) * SHA1_RESULTLEN
),
201 lower_bound
, upper_bound
) &&
203 ntohl(cd
->magic
) == CSMAGIC_CODEDIRECTORY
) {
207 // not found or not a valid code directory
213 * Locating a page hash
215 static const unsigned char *
217 const CS_CodeDirectory
*cd
,
222 const unsigned char *base
, *top
, *hash
;
223 uint32_t nCodeSlots
= ntohl(cd
->nCodeSlots
);
225 assert(cs_valid_range(cd
, cd
+ 1, lower_bound
, upper_bound
));
227 if((ntohl(cd
->version
) >= CS_SUPPORTSSCATTER
) && (ntohl(cd
->scatterOffset
))) {
228 /* Get first scatter struct */
229 const SC_Scatter
*scatter
= (const SC_Scatter
*)
230 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
231 uint32_t hashindex
=0, scount
, sbase
=0;
232 /* iterate all scatter structs */
234 if((const char*)scatter
> (const char*)cd
+ ntohl(cd
->length
)) {
236 printf("CODE SIGNING: Scatter extends past Code Directory\n");
241 scount
= ntohl(scatter
->count
);
242 uint32_t new_base
= ntohl(scatter
->base
);
249 if((hashindex
> 0) && (new_base
<= sbase
)) {
251 printf("CODE SIGNING: unordered Scatter, prev base %d, cur base %d\n",
254 return NULL
; /* unordered scatter array */
258 /* this scatter beyond page we're looking for? */
263 if (sbase
+scount
>= page
) {
264 /* Found the scatter struct that is
265 * referencing our page */
267 /* base = address of first hash covered by scatter */
268 base
= (const unsigned char *)cd
+ ntohl(cd
->hashOffset
) +
269 hashindex
* SHA1_RESULTLEN
;
270 /* top = address of first hash after this scatter */
271 top
= base
+ scount
* SHA1_RESULTLEN
;
272 if (!cs_valid_range(base
, top
, lower_bound
,
274 hashindex
> nCodeSlots
) {
281 /* this scatter struct is before the page we're looking
287 hash
= base
+ (page
- sbase
) * SHA1_RESULTLEN
;
289 base
= (const unsigned char *)cd
+ ntohl(cd
->hashOffset
);
290 top
= base
+ nCodeSlots
* SHA1_RESULTLEN
;
291 if (!cs_valid_range(base
, top
, lower_bound
, upper_bound
) ||
295 assert(page
< nCodeSlots
);
297 hash
= base
+ page
* SHA1_RESULTLEN
;
300 if (!cs_valid_range(hash
, hash
+ SHA1_RESULTLEN
,
301 lower_bound
, upper_bound
)) {
309 * cs_validate_codedirectory
311 * Validate that pointers inside the code directory to make sure that
312 * all offsets and lengths are constrained within the buffer.
314 * Parameters: cd Pointer to code directory buffer
315 * length Length of buffer
318 * EBADEXEC Invalid code signature
322 cs_validate_codedirectory(const CS_CodeDirectory
*cd
, size_t length
)
325 if (length
< sizeof(*cd
))
327 if (ntohl(cd
->magic
) != CSMAGIC_CODEDIRECTORY
)
329 if (cd
->hashSize
!= SHA1_RESULTLEN
)
331 if (cd
->pageSize
!= PAGE_SHIFT
)
333 if (cd
->hashType
!= CS_HASHTYPE_SHA1
)
336 if (length
< ntohl(cd
->hashOffset
))
339 /* check that nSpecialSlots fits in the buffer in front of hashOffset */
340 if (ntohl(cd
->hashOffset
) / SHA1_RESULTLEN
< ntohl(cd
->nSpecialSlots
))
343 /* check that codeslots fits in the buffer */
344 if ((length
- ntohl(cd
->hashOffset
)) / SHA1_RESULTLEN
< ntohl(cd
->nCodeSlots
))
347 if (ntohl(cd
->version
) >= CS_SUPPORTSSCATTER
&& cd
->scatterOffset
) {
349 if (length
< ntohl(cd
->scatterOffset
))
352 SC_Scatter
*scatter
= (SC_Scatter
*)
353 (((uint8_t *)cd
) + ntohl(cd
->scatterOffset
));
357 * Check each scatter buffer, since we don't know the
358 * length of the scatter buffer array, we have to
362 /* check that the end of each scatter buffer in within the length */
363 if (((const uint8_t *)scatter
) + sizeof(scatter
[0]) > (const uint8_t *)cd
+ length
)
365 uint32_t scount
= ntohl(scatter
->count
);
368 if (nPages
+ scount
< nPages
)
373 /* XXX check that basees doesn't overlap */
374 /* XXX check that targetOffset doesn't overlap */
376 #if 0 /* rdar://12579439 */
377 if (nPages
!= ntohl(cd
->nCodeSlots
))
382 if (length
< ntohl(cd
->identOffset
))
385 /* identifier is NUL terminated string */
386 if (cd
->identOffset
) {
387 uint8_t *ptr
= (uint8_t *)cd
+ ntohl(cd
->identOffset
);
388 if (memchr(ptr
, 0, length
- ntohl(cd
->identOffset
)) == NULL
)
400 cs_validate_blob(const CS_GenericBlob
*blob
, size_t length
)
402 if (length
< sizeof(CS_GenericBlob
) || length
< ntohl(blob
->length
))
410 * Validate that superblob/embedded code directory to make sure that
411 * all internal pointers are valid.
413 * Will validate both a superblob csblob and a "raw" code directory.
416 * Parameters: buffer Pointer to code signature
417 * length Length of buffer
418 * rcd returns pointer to code directory
421 * EBADEXEC Invalid code signature
425 cs_validate_csblob(const uint8_t *addr
, size_t length
,
426 const CS_CodeDirectory
**rcd
)
428 const CS_GenericBlob
*blob
= (const CS_GenericBlob
*)(void *)addr
;
433 error
= cs_validate_blob(blob
, length
);
437 length
= ntohl(blob
->length
);
439 if (ntohl(blob
->magic
) == CSMAGIC_EMBEDDED_SIGNATURE
) {
440 const CS_SuperBlob
*sb
= (const CS_SuperBlob
*)blob
;
441 uint32_t n
, count
= ntohl(sb
->count
);
443 if (length
< sizeof(CS_SuperBlob
))
446 /* check that the array of BlobIndex fits in the rest of the data */
447 if ((length
- sizeof(CS_SuperBlob
)) / sizeof(CS_BlobIndex
) < count
)
450 /* now check each BlobIndex */
451 for (n
= 0; n
< count
; n
++) {
452 const CS_BlobIndex
*blobIndex
= &sb
->index
[n
];
453 if (length
< ntohl(blobIndex
->offset
))
456 const CS_GenericBlob
*subBlob
=
457 (const CS_GenericBlob
*)(void *)(addr
+ ntohl(blobIndex
->offset
));
459 size_t subLength
= length
- ntohl(blobIndex
->offset
);
461 if ((error
= cs_validate_blob(subBlob
, subLength
)) != 0)
463 subLength
= ntohl(subBlob
->length
);
465 /* extra validation for CDs, that is also returned */
466 if (ntohl(blobIndex
->type
) == CSSLOT_CODEDIRECTORY
) {
467 const CS_CodeDirectory
*cd
= (const CS_CodeDirectory
*)subBlob
;
468 if ((error
= cs_validate_codedirectory(cd
, subLength
)) != 0)
474 } else if (ntohl(blob
->magic
) == CSMAGIC_CODEDIRECTORY
) {
476 if ((error
= cs_validate_codedirectory((const CS_CodeDirectory
*)(void *)addr
, length
)) != 0)
478 *rcd
= (const CS_CodeDirectory
*)blob
;
492 * Find an blob from the superblob/code directory. The blob must have
493 * been been validated by cs_validate_csblob() before calling
494 * this. Use cs_find_blob() instead.
496 * Will also find a "raw" code directory if its stored as well as
497 * searching the superblob.
499 * Parameters: buffer Pointer to code signature
500 * length Length of buffer
501 * type type of blob to find
502 * magic the magic number for that blob
504 * Returns: pointer Success
505 * NULL Buffer not found
508 static const CS_GenericBlob
*
509 cs_find_blob_bytes(const uint8_t *addr
, size_t length
, uint32_t type
, uint32_t magic
)
511 const CS_GenericBlob
*blob
= (const CS_GenericBlob
*)(void *)addr
;
513 if (ntohl(blob
->magic
) == CSMAGIC_EMBEDDED_SIGNATURE
) {
514 const CS_SuperBlob
*sb
= (const CS_SuperBlob
*)blob
;
515 size_t n
, count
= ntohl(sb
->count
);
517 for (n
= 0; n
< count
; n
++) {
518 if (ntohl(sb
->index
[n
].type
) != type
)
520 uint32_t offset
= ntohl(sb
->index
[n
].offset
);
521 if (length
- sizeof(const CS_GenericBlob
) < offset
)
523 blob
= (const CS_GenericBlob
*)(void *)(addr
+ offset
);
524 if (ntohl(blob
->magic
) != magic
)
528 } else if (type
== CSSLOT_CODEDIRECTORY
529 && ntohl(blob
->magic
) == CSMAGIC_CODEDIRECTORY
530 && magic
== CSMAGIC_CODEDIRECTORY
)
536 static const CS_GenericBlob
*
537 cs_find_blob(struct cs_blob
*csblob
, uint32_t type
, uint32_t magic
)
539 if ((csblob
->csb_flags
& CS_VALID
) == 0)
541 return cs_find_blob_bytes((const uint8_t *)csblob
->csb_mem_kaddr
, csblob
->csb_mem_size
, type
, magic
);
544 static const uint8_t *
545 cs_find_special_slot(const CS_CodeDirectory
*cd
, uint32_t slot
)
547 /* there is no zero special slot since that is the first code slot */
548 if (ntohl(cd
->nSpecialSlots
) < slot
|| slot
== 0)
551 return ((const uint8_t *)cd
+ ntohl(cd
->hashOffset
) - (SHA1_RESULTLEN
* slot
));
556 * End of routines to navigate code signing data structures in the kernel.
561 * Routines to navigate entitlements in the kernel.
564 /* Retrieve the entitlements blob for a process.
566 * EINVAL no text vnode associated with the process
567 * EBADEXEC invalid code signing data
568 * 0 no error occurred
570 * On success, out_start and out_length will point to the
571 * entitlements blob if found; or will be set to NULL/zero
572 * if there were no entitlements.
575 static uint8_t sha1_zero
[SHA1_RESULTLEN
] = { 0 };
578 cs_entitlements_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
580 uint8_t computed_hash
[SHA1_RESULTLEN
];
581 const CS_GenericBlob
*entitlements
;
582 const CS_CodeDirectory
*code_dir
;
583 struct cs_blob
*csblob
;
584 const uint8_t *embedded_hash
;
590 if (NULL
== p
->p_textvp
)
593 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
596 if ((code_dir
= (const CS_CodeDirectory
*)cs_find_blob(csblob
, CSSLOT_CODEDIRECTORY
, CSMAGIC_CODEDIRECTORY
)) == NULL
)
599 entitlements
= cs_find_blob(csblob
, CSSLOT_ENTITLEMENTS
, CSMAGIC_EMBEDDED_ENTITLEMENTS
);
600 embedded_hash
= cs_find_special_slot(code_dir
, CSSLOT_ENTITLEMENTS
);
602 if (embedded_hash
== NULL
) {
606 } else if (entitlements
== NULL
&& memcmp(embedded_hash
, sha1_zero
, SHA1_RESULTLEN
) != 0) {
611 SHA1Update(&context
, entitlements
, ntohl(entitlements
->length
));
612 SHA1Final(computed_hash
, &context
);
613 if (memcmp(computed_hash
, embedded_hash
, SHA1_RESULTLEN
) != 0)
616 *out_start
= (void *)entitlements
;
617 *out_length
= ntohl(entitlements
->length
);
622 /* Retrieve the codesign identity for a process.
624 * NULL an error occured
625 * string the cs_identity
629 cs_identity_get(proc_t p
)
631 const CS_CodeDirectory
*code_dir
;
632 struct cs_blob
*csblob
;
634 if (NULL
== p
->p_textvp
)
637 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
640 if ((code_dir
= (const CS_CodeDirectory
*)cs_find_blob(csblob
, CSSLOT_CODEDIRECTORY
, CSMAGIC_CODEDIRECTORY
)) == NULL
)
643 if (code_dir
->identOffset
== 0)
646 return ((const char *)code_dir
) + ntohl(code_dir
->identOffset
);
651 /* Retrieve the codesign blob for a process.
653 * EINVAL no text vnode associated with the process
654 * 0 no error occurred
656 * On success, out_start and out_length will point to the
657 * cms blob if found; or will be set to NULL/zero
658 * if there were no blob.
662 cs_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
664 struct cs_blob
*csblob
;
669 if (NULL
== p
->p_textvp
)
672 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
675 *out_start
= (void *)csblob
->csb_mem_kaddr
;
676 *out_length
= csblob
->csb_mem_size
;
682 cs_get_cdhash(struct proc
*p
)
684 struct cs_blob
*csblob
;
686 if (NULL
== p
->p_textvp
)
689 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
692 return csblob
->csb_sha1
;
697 * End of routines to navigate entitlements in the kernel.
705 * Initialization of the zone for Unified Buffer Cache.
712 * ubc_info_zone(global) initialized for subsequent allocations
714 __private_extern__
void
719 i
= (vm_size_t
) sizeof (struct ubc_info
);
721 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
723 zone_change(ubc_info_zone
, Z_NOENCRYPT
, TRUE
);
730 * Allocate and attach an empty ubc_info structure to a vnode
732 * Parameters: vp Pointer to the vnode
735 * vnode_size:ENOMEM Not enough space
736 * vnode_size:??? Other error from vnode_getattr
740 ubc_info_init(struct vnode
*vp
)
742 return(ubc_info_init_internal(vp
, 0, 0));
747 * ubc_info_init_withsize
749 * Allocate and attach a sized ubc_info structure to a vnode
751 * Parameters: vp Pointer to the vnode
752 * filesize The size of the file
755 * vnode_size:ENOMEM Not enough space
756 * vnode_size:??? Other error from vnode_getattr
759 ubc_info_init_withsize(struct vnode
*vp
, off_t filesize
)
761 return(ubc_info_init_internal(vp
, 1, filesize
));
766 * ubc_info_init_internal
768 * Allocate and attach a ubc_info structure to a vnode
770 * Parameters: vp Pointer to the vnode
771 * withfsize{0,1} Zero if the size should be obtained
772 * from the vnode; otherwise, use filesize
773 * filesize The size of the file, if withfsize == 1
776 * vnode_size:ENOMEM Not enough space
777 * vnode_size:??? Other error from vnode_getattr
779 * Notes: We call a blocking zalloc(), and the zone was created as an
780 * expandable and collectable zone, so if no memory is available,
781 * it is possible for zalloc() to block indefinitely. zalloc()
782 * may also panic if the zone of zones is exhausted, since it's
785 * We unconditionally call vnode_pager_setup(), even if this is
786 * a reuse of a ubc_info; in that case, we should probably assert
787 * that it does not already have a pager association, but do not.
789 * Since memory_object_create_named() can only fail from receiving
790 * an invalid pager argument, the explicit check and panic is
791 * merely precautionary.
794 ubc_info_init_internal(vnode_t vp
, int withfsize
, off_t filesize
)
796 register struct ubc_info
*uip
;
800 memory_object_control_t control
;
805 * If there is not already a ubc_info attached to the vnode, we
806 * attach one; otherwise, we will reuse the one that's there.
808 if (uip
== UBC_INFO_NULL
) {
810 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
811 bzero((char *)uip
, sizeof(struct ubc_info
));
814 uip
->ui_flags
= UI_INITED
;
815 uip
->ui_ucred
= NOCRED
;
817 assert(uip
->ui_flags
!= UI_NONE
);
818 assert(uip
->ui_vnode
== vp
);
820 /* now set this ubc_info in the vnode */
824 * Allocate a pager object for this vnode
826 * XXX The value of the pager parameter is currently ignored.
827 * XXX Presumably, this API changed to avoid the race between
828 * XXX setting the pager and the UI_HASPAGER flag.
830 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
834 * Explicitly set the pager into the ubc_info, after setting the
837 SET(uip
->ui_flags
, UI_HASPAGER
);
838 uip
->ui_pager
= pager
;
841 * Note: We can not use VNOP_GETATTR() to get accurate
842 * value of ui_size because this may be an NFS vnode, and
843 * nfs_getattr() can call vinvalbuf(); if this happens,
844 * ubc_info is not set up to deal with that event.
849 * create a vnode - vm_object association
850 * memory_object_create_named() creates a "named" reference on the
851 * memory object we hold this reference as long as the vnode is
852 * "alive." Since memory_object_create_named() took its own reference
853 * on the vnode pager we passed it, we can drop the reference
854 * vnode_pager_setup() returned here.
856 kret
= memory_object_create_named(pager
,
857 (memory_object_size_t
)uip
->ui_size
, &control
);
858 vnode_pager_deallocate(pager
);
859 if (kret
!= KERN_SUCCESS
)
860 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
863 uip
->ui_control
= control
; /* cache the value of the mo control */
864 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
866 if (withfsize
== 0) {
867 /* initialize the size */
868 error
= vnode_size(vp
, &uip
->ui_size
, vfs_context_current());
872 uip
->ui_size
= filesize
;
874 vp
->v_lflag
|= VNAMED_UBC
; /* vnode has a named ubc reference */
883 * Free a ubc_info structure
885 * Parameters: uip A pointer to the ubc_info to free
889 * Notes: If there is a credential that has subsequently been associated
890 * with the ubc_info via a call to ubc_setcred(), the reference
891 * to the credential is dropped.
893 * It's actually impossible for a ubc_info.ui_control to take the
894 * value MEMORY_OBJECT_CONTROL_NULL.
897 ubc_info_free(struct ubc_info
*uip
)
899 if (IS_VALID_CRED(uip
->ui_ucred
)) {
900 kauth_cred_unref(&uip
->ui_ucred
);
903 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
904 memory_object_control_deallocate(uip
->ui_control
);
906 cluster_release(uip
);
909 zfree(ubc_info_zone
, uip
);
915 ubc_info_deallocate(struct ubc_info
*uip
)
924 * Tell the VM that the the size of the file represented by the vnode has
927 * Parameters: vp The vp whose backing file size is
929 * nsize The new size of the backing file
934 * Notes: This function will indicate failure if the new size that's
935 * being attempted to be set is negative.
937 * This function will fail if there is no ubc_info currently
938 * associated with the vnode.
940 * This function will indicate success it the new size is the
941 * same or larger than the old size (in this case, the remainder
942 * of the file will require modification or use of an existing upl
943 * to access successfully).
945 * This function will fail if the new file size is smaller, and
946 * the memory region being invalidated was unable to actually be
947 * invalidated and/or the last page could not be flushed, if the
948 * new size is not aligned to a page boundary. This is usually
949 * indicative of an I/O error.
952 ubc_setsize(struct vnode
*vp
, off_t nsize
)
954 off_t osize
; /* ui_size before change */
955 off_t lastpg
, olastpgend
, lastoff
;
956 struct ubc_info
*uip
;
957 memory_object_control_t control
;
958 kern_return_t kret
= KERN_SUCCESS
;
960 if (nsize
< (off_t
)0)
963 if (!UBCINFOEXISTS(vp
))
967 osize
= uip
->ui_size
;
969 * Update the size before flushing the VM
971 uip
->ui_size
= nsize
;
973 if (nsize
>= osize
) { /* Nothing more to do */
975 lock_vnode_and_post(vp
, NOTE_EXTEND
);
978 return (1); /* return success */
982 * When the file shrinks, invalidate the pages beyond the
983 * new size. Also get rid of garbage beyond nsize on the
984 * last page. The ui_size already has the nsize, so any
985 * subsequent page-in will zero-fill the tail properly
987 lastpg
= trunc_page_64(nsize
);
988 olastpgend
= round_page_64(osize
);
989 control
= uip
->ui_control
;
991 lastoff
= (nsize
& PAGE_MASK_64
);
999 * new EOF ends up in the middle of a page
1000 * zero the tail of this page if its currently
1001 * present in the cache
1003 kret
= ubc_create_upl(vp
, lastpg
, PAGE_SIZE
, &upl
, &pl
, UPL_SET_LITE
);
1005 if (kret
!= KERN_SUCCESS
)
1006 panic("ubc_setsize: ubc_create_upl (error = %d)\n", kret
);
1008 if (upl_valid_page(pl
, 0))
1009 cluster_zero(upl
, (uint32_t)lastoff
, PAGE_SIZE
- (uint32_t)lastoff
, NULL
);
1011 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
1013 lastpg
+= PAGE_SIZE_64
;
1015 if (olastpgend
> lastpg
) {
1019 flags
= MEMORY_OBJECT_DATA_FLUSH_ALL
;
1021 flags
= MEMORY_OBJECT_DATA_FLUSH
;
1023 * invalidate the pages beyond the new EOF page
1026 kret
= memory_object_lock_request(control
,
1027 (memory_object_offset_t
)lastpg
,
1028 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
1029 MEMORY_OBJECT_RETURN_NONE
, flags
, VM_PROT_NO_CHANGE
);
1030 if (kret
!= KERN_SUCCESS
)
1031 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
1033 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
1040 * Get the size of the file assocated with the specified vnode
1042 * Parameters: vp The vnode whose size is of interest
1044 * Returns: 0 There is no ubc_info associated with
1045 * this vnode, or the size is zero
1046 * !0 The size of the file
1048 * Notes: Using this routine, it is not possible for a caller to
1049 * successfully distinguish between a vnode associate with a zero
1050 * length file, and a vnode with no associated ubc_info. The
1051 * caller therefore needs to not care, or needs to ensure that
1052 * they have previously successfully called ubc_info_init() or
1053 * ubc_info_init_withsize().
1056 ubc_getsize(struct vnode
*vp
)
1058 /* people depend on the side effect of this working this way
1059 * as they call this for directory
1061 if (!UBCINFOEXISTS(vp
))
1063 return (vp
->v_ubcinfo
->ui_size
);
1070 * Call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes for this
1073 * Parameters: mp The mount point
1075 * Returns: 0 Success
1077 * Notes: There is no failure indication for this function.
1079 * This function is used in the unmount path; since it may block
1080 * I/O indefinitely, it should not be used in the forced unmount
1081 * path, since a device unavailability could also block that
1084 * Because there is no device ejection interlock on USB, FireWire,
1085 * or similar devices, it's possible that an ejection that begins
1086 * subsequent to the vnode_iterate() completing, either on one of
1087 * those devices, or a network mount for which the server quits
1088 * responding, etc., may cause the caller to block indefinitely.
1090 __private_extern__
int
1091 ubc_umount(struct mount
*mp
)
1093 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
1101 * Used by ubc_umount() as an internal implementation detail; see ubc_umount()
1102 * and vnode_iterate() for details of implementation.
1105 ubc_umcallback(vnode_t vp
, __unused
void * args
)
1108 if (UBCINFOEXISTS(vp
)) {
1110 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
1112 return (VNODE_RETURNED
);
1119 * Get the credentials currently active for the ubc_info associated with the
1122 * Parameters: vp The vnode whose ubc_info credentials
1123 * are to be retrieved
1125 * Returns: !NOCRED The credentials
1126 * NOCRED If there is no ubc_info for the vnode,
1127 * or if there is one, but it has not had
1128 * any credentials associated with it via
1129 * a call to ubc_setcred()
1132 ubc_getcred(struct vnode
*vp
)
1134 if (UBCINFOEXISTS(vp
))
1135 return (vp
->v_ubcinfo
->ui_ucred
);
1144 * If they are not already set, set the credentials of the ubc_info structure
1145 * associated with the vnode to those of the supplied thread; otherwise leave
1148 * Parameters: vp The vnode whose ubc_info creds are to
1150 * p The process whose credentials are to
1151 * be used, if not running on an assumed
1153 * thread The thread whose credentials are to
1156 * Returns: 1 This vnode has no associated ubc_info
1159 * Notes: This function takes a proc parameter to account for bootstrap
1160 * issues where a task or thread may call this routine, either
1161 * before credentials have been initialized by bsd_init(), or if
1162 * there is no BSD info asscoiate with a mach thread yet. This
1163 * is known to happen in both the initial swap and memory mapping
1166 * This function is generally used only in the following cases:
1168 * o a memory mapped file via the mmap() system call
1169 * o a memory mapped file via the deprecated map_fd() call
1170 * o a swap store backing file
1171 * o subsequent to a successful write via vn_write()
1173 * The information is then used by the NFS client in order to
1174 * cons up a wire message in either the page-in or page-out path.
1176 * There are two potential problems with the use of this API:
1178 * o Because the write path only set it on a successful
1179 * write, there is a race window between setting the
1180 * credential and its use to evict the pages to the
1181 * remote file server
1183 * o Because a page-in may occur prior to a write, the
1184 * credential may not be set at this time, if the page-in
1185 * is not the result of a mapping established via mmap()
1188 * In both these cases, this will be triggered from the paging
1189 * path, which will instead use the credential of the current
1190 * process, which in this case is either the dynamic_pager or
1191 * the kernel task, both of which utilize "root" credentials.
1193 * This may potentially permit operations to occur which should
1194 * be denied, or it may cause to be denied operations which
1195 * should be permitted, depending on the configuration of the NFS
1199 ubc_setthreadcred(struct vnode
*vp
, proc_t p
, thread_t thread
)
1201 struct ubc_info
*uip
;
1203 struct uthread
*uthread
= get_bsdthread_info(thread
);
1205 if (!UBCINFOEXISTS(vp
))
1210 uip
= vp
->v_ubcinfo
;
1211 credp
= uip
->ui_ucred
;
1213 if (!IS_VALID_CRED(credp
)) {
1214 /* use per-thread cred, if assumed identity, else proc cred */
1215 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
1216 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
1218 uip
->ui_ucred
= uthread
->uu_ucred
;
1219 kauth_cred_ref(uip
->ui_ucred
);
1231 * If they are not already set, set the credentials of the ubc_info structure
1232 * associated with the vnode to those of the process; otherwise leave them
1235 * Parameters: vp The vnode whose ubc_info creds are to
1237 * p The process whose credentials are to
1240 * Returns: 0 This vnode has no associated ubc_info
1243 * Notes: The return values for this function are inverted from nearly
1244 * all other uses in the kernel.
1246 * See also ubc_setthreadcred(), above.
1248 * This function is considered deprecated, and generally should
1249 * not be used, as it is incompatible with per-thread credentials;
1250 * it exists for legacy KPI reasons.
1252 * DEPRECATION: ubc_setcred() is being deprecated. Please use
1253 * ubc_setthreadcred() instead.
1256 ubc_setcred(struct vnode
*vp
, proc_t p
)
1258 struct ubc_info
*uip
;
1261 /* If there is no ubc_info, deny the operation */
1262 if ( !UBCINFOEXISTS(vp
))
1266 * Check to see if there is already a credential reference in the
1267 * ubc_info; if there is not, take one on the supplied credential.
1270 uip
= vp
->v_ubcinfo
;
1271 credp
= uip
->ui_ucred
;
1272 if (!IS_VALID_CRED(credp
)) {
1273 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
1283 * Get the pager associated with the ubc_info associated with the vnode.
1285 * Parameters: vp The vnode to obtain the pager from
1287 * Returns: !VNODE_PAGER_NULL The memory_object_t for the pager
1288 * VNODE_PAGER_NULL There is no ubc_info for this vnode
1290 * Notes: For each vnode that has a ubc_info associated with it, that
1291 * ubc_info SHALL have a pager associated with it, so in the
1292 * normal case, it's impossible to return VNODE_PAGER_NULL for
1293 * a vnode with an associated ubc_info.
1295 __private_extern__ memory_object_t
1296 ubc_getpager(struct vnode
*vp
)
1298 if (UBCINFOEXISTS(vp
))
1299 return (vp
->v_ubcinfo
->ui_pager
);
1308 * Get the memory object control associated with the ubc_info associated with
1311 * Parameters: vp The vnode to obtain the memory object
1315 * Returns: !MEMORY_OBJECT_CONTROL_NULL
1316 * MEMORY_OBJECT_CONTROL_NULL
1318 * Notes: Historically, if the flags were not "do not reactivate", this
1319 * function would look up the memory object using the pager if
1320 * it did not exist (this could be the case if the vnode had
1321 * been previously reactivated). The flags would also permit a
1322 * hold to be requested, which would have created an object
1323 * reference, if one had not already existed. This usage is
1324 * deprecated, as it would permit a race between finding and
1325 * taking the reference vs. a single reference being dropped in
1328 memory_object_control_t
1329 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
1331 if (UBCINFOEXISTS(vp
))
1332 return((vp
->v_ubcinfo
->ui_control
));
1334 return (MEMORY_OBJECT_CONTROL_NULL
);
1338 ubc_strict_uncached_IO(struct vnode
*vp
)
1340 boolean_t result
= FALSE
;
1342 if (UBCINFOEXISTS(vp
)) {
1343 result
= memory_object_is_slid(vp
->v_ubcinfo
->ui_control
);
1351 * Convert a given block number to a memory backing object (file) offset for a
1354 * Parameters: vp The vnode in which the block is located
1355 * blkno The block number to convert
1357 * Returns: !-1 The offset into the backing object
1358 * -1 There is no ubc_info associated with
1360 * -1 An error occurred in the underlying VFS
1361 * while translating the block to an
1362 * offset; the most likely cause is that
1363 * the caller specified a block past the
1364 * end of the file, but this could also be
1365 * any other error from VNOP_BLKTOOFF().
1367 * Note: Representing the error in band loses some information, but does
1368 * not occlude a valid offset, since an off_t of -1 is normally
1369 * used to represent EOF. If we had a more reliable constant in
1370 * our header files for it (i.e. explicitly cast to an off_t), we
1371 * would use it here instead.
1374 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
1376 off_t file_offset
= -1;
1379 if (UBCINFOEXISTS(vp
)) {
1380 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
1385 return (file_offset
);
1392 * Convert a given offset in a memory backing object into a block number for a
1395 * Parameters: vp The vnode in which the offset is
1397 * offset The offset into the backing object
1399 * Returns: !-1 The returned block number
1400 * -1 There is no ubc_info associated with
1402 * -1 An error occurred in the underlying VFS
1403 * while translating the block to an
1404 * offset; the most likely cause is that
1405 * the caller specified a block past the
1406 * end of the file, but this could also be
1407 * any other error from VNOP_OFFTOBLK().
1409 * Note: Representing the error in band loses some information, but does
1410 * not occlude a valid block number, since block numbers exceed
1411 * the valid range for offsets, due to their relative sizes. If
1412 * we had a more reliable constant than -1 in our header files
1413 * for it (i.e. explicitly cast to an daddr64_t), we would use it
1417 ubc_offtoblk(vnode_t vp
, off_t offset
)
1419 daddr64_t blkno
= -1;
1422 if (UBCINFOEXISTS(vp
)) {
1423 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
1433 * ubc_pages_resident
1435 * Determine whether or not a given vnode has pages resident via the memory
1436 * object control associated with the ubc_info associated with the vnode
1438 * Parameters: vp The vnode we want to know about
1444 ubc_pages_resident(vnode_t vp
)
1447 boolean_t has_pages_resident
;
1449 if (!UBCINFOEXISTS(vp
))
1453 * The following call may fail if an invalid ui_control is specified,
1454 * or if there is no VM object associated with the control object. In
1455 * either case, reacting to it as if there were no pages resident will
1456 * result in correct behavior.
1458 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
1460 if (kret
!= KERN_SUCCESS
)
1463 if (has_pages_resident
== TRUE
)
1473 * Clean and/or invalidate a range in the memory object that backs this vnode
1475 * Parameters: vp The vnode whose associated ubc_info's
1476 * associated memory object is to have a
1477 * range invalidated within it
1478 * beg_off The start of the range, as an offset
1479 * end_off The end of the range, as an offset
1480 * flags See ubc_msync_internal()
1482 * Returns: 1 Success
1485 * Notes: see ubc_msync_internal() for more detailed information.
1487 * DEPRECATED: This interface is obsolete due to a failure to return error
1488 * information needed in order to correct failures. The currently
1489 * recommended interface is ubc_msync().
1492 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
1494 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
1501 * Clean and/or invalidate a range in the memory object that backs this vnode
1503 * Parameters: vp The vnode whose associated ubc_info's
1504 * associated memory object is to have a
1505 * range invalidated within it
1506 * beg_off The start of the range, as an offset
1507 * end_off The end of the range, as an offset
1508 * resid_off The address of an off_t supplied by the
1509 * caller; may be set to NULL to ignore
1510 * flags See ubc_msync_internal()
1512 * Returns: 0 Success
1513 * !0 Failure; an errno is returned
1516 * *resid_off, modified If non-NULL, the contents are ALWAYS
1517 * modified; they are initialized to the
1518 * beg_off, and in case of an I/O error,
1519 * the difference between beg_off and the
1520 * current value will reflect what was
1521 * able to be written before the error
1522 * occurred. If no error is returned, the
1523 * value of the resid_off is undefined; do
1524 * NOT use it in place of end_off if you
1525 * intend to increment from the end of the
1526 * last call and call iteratively.
1528 * Notes: see ubc_msync_internal() for more detailed information.
1532 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
1538 *resid_off
= beg_off
;
1540 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
1542 if (retval
== 0 && io_errno
== 0)
1549 * Clean and/or invalidate a range in the memory object that backs this vnode
1551 * Parameters: vp The vnode whose associated ubc_info's
1552 * associated memory object is to have a
1553 * range invalidated within it
1554 * beg_off The start of the range, as an offset
1555 * end_off The end of the range, as an offset
1556 * resid_off The address of an off_t supplied by the
1557 * caller; may be set to NULL to ignore
1558 * flags MUST contain at least one of the flags
1559 * UBC_INVALIDATE, UBC_PUSHDIRTY, or
1560 * UBC_PUSHALL; if UBC_PUSHDIRTY is used,
1561 * UBC_SYNC may also be specified to cause
1562 * this function to block until the
1563 * operation is complete. The behavior
1564 * of UBC_SYNC is otherwise undefined.
1565 * io_errno The address of an int to contain the
1566 * errno from a failed I/O operation, if
1567 * one occurs; may be set to NULL to
1570 * Returns: 1 Success
1574 * *resid_off, modified The contents of this offset MAY be
1575 * modified; in case of an I/O error, the
1576 * difference between beg_off and the
1577 * current value will reflect what was
1578 * able to be written before the error
1580 * *io_errno, modified The contents of this offset are set to
1581 * an errno, if an error occurs; if the
1582 * caller supplies an io_errno parameter,
1583 * they should be careful to initialize it
1584 * to 0 before calling this function to
1585 * enable them to distinguish an error
1586 * with a valid *resid_off from an invalid
1587 * one, and to avoid potentially falsely
1588 * reporting an error, depending on use.
1590 * Notes: If there is no ubc_info associated with the vnode supplied,
1591 * this function immediately returns success.
1593 * If the value of end_off is less than or equal to beg_off, this
1594 * function immediately returns success; that is, end_off is NOT
1597 * IMPORTANT: one of the flags UBC_INVALIDATE, UBC_PUSHDIRTY, or
1598 * UBC_PUSHALL MUST be specified; that is, it is NOT possible to
1599 * attempt to block on in-progress I/O by calling this function
1600 * with UBC_PUSHDIRTY, and then later call it with just UBC_SYNC
1601 * in order to block pending on the I/O already in progress.
1603 * The start offset is truncated to the page boundary and the
1604 * size is adjusted to include the last page in the range; that
1605 * is, end_off on exactly a page boundary will not change if it
1606 * is rounded, and the range of bytes written will be from the
1607 * truncate beg_off to the rounded (end_off - 1).
1610 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
1612 memory_object_size_t tsize
;
1614 int request_flags
= 0;
1615 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
1617 if ( !UBCINFOEXISTS(vp
))
1619 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
1621 if (end_off
<= beg_off
)
1624 if (flags
& UBC_INVALIDATE
)
1626 * discard the resident pages
1628 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
1630 if (flags
& UBC_SYNC
)
1632 * wait for all the I/O to complete before returning
1634 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
1636 if (flags
& UBC_PUSHDIRTY
)
1638 * we only return the dirty pages in the range
1640 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
1642 if (flags
& UBC_PUSHALL
)
1644 * then return all the interesting pages in the range (both
1645 * dirty and precious) to the pager
1647 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
1649 beg_off
= trunc_page_64(beg_off
);
1650 end_off
= round_page_64(end_off
);
1651 tsize
= (memory_object_size_t
)end_off
- beg_off
;
1653 /* flush and/or invalidate pages in the range requested */
1654 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
1656 (memory_object_offset_t
*)resid_off
,
1657 io_errno
, flush_flags
, request_flags
,
1660 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
1665 * ubc_msync_internal
1667 * Explicitly map a vnode that has an associate ubc_info, and add a reference
1668 * to it for the ubc system, if there isn't one already, so it will not be
1669 * recycled while it's in use, and set flags on the ubc_info to indicate that
1672 * Parameters: vp The vnode to map
1673 * flags The mapping flags for the vnode; this
1674 * will be a combination of one or more of
1675 * PROT_READ, PROT_WRITE, and PROT_EXEC
1677 * Returns: 0 Success
1678 * EPERM Permission was denied
1680 * Notes: An I/O reference on the vnode must already be held on entry
1682 * If there is no ubc_info associated with the vnode, this function
1683 * will return success.
1685 * If a permission error occurs, this function will return
1686 * failure; all other failures will cause this function to return
1689 * IMPORTANT: This is an internal use function, and its symbols
1690 * are not exported, hence its error checking is not very robust.
1691 * It is primarily used by:
1693 * o mmap(), when mapping a file
1694 * o The deprecated map_fd() interface, when mapping a file
1695 * o When mapping a shared file (a shared library in the
1696 * shared segment region)
1697 * o When loading a program image during the exec process
1699 * ...all of these uses ignore the return code, and any fault that
1700 * results later because of a failure is handled in the fix-up path
1701 * of the fault handler. The interface exists primarily as a
1704 * Given that third party implementation of the type of interfaces
1705 * that would use this function, such as alternative executable
1706 * formats, etc., are unsupported, this function is not exported
1709 * The extra reference is held until the VM system unmaps the
1710 * vnode from its own context to maintain a vnode reference in
1711 * cases like open()/mmap()/close(), which leave the backing
1712 * object referenced by a mapped memory region in a process
1715 __private_extern__
int
1716 ubc_map(vnode_t vp
, int flags
)
1718 struct ubc_info
*uip
;
1721 int need_wakeup
= 0;
1723 if (UBCINFOEXISTS(vp
)) {
1726 uip
= vp
->v_ubcinfo
;
1728 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1729 SET(uip
->ui_flags
, UI_MAPWAITING
);
1730 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1731 PRIBIO
, "ubc_map", NULL
);
1733 SET(uip
->ui_flags
, UI_MAPBUSY
);
1736 error
= VNOP_MMAP(vp
, flags
, vfs_context_current());
1741 vnode_lock_spin(vp
);
1744 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
1746 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
1748 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1750 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1751 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1757 wakeup(&uip
->ui_flags
);
1769 * Destroy the named memory object associated with the ubc_info control object
1770 * associated with the designated vnode, if there is a ubc_info associated
1771 * with the vnode, and a control object is associated with it
1773 * Parameters: vp The designated vnode
1777 * Notes: This function is called on vnode termination for all vnodes,
1778 * and must therefore not assume that there is a ubc_info that is
1779 * associated with the vnode, nor that there is a control object
1780 * associated with the ubc_info.
1782 * If all the conditions necessary are present, this function
1783 * calls memory_object_destory(), which will in turn end up
1784 * calling ubc_unmap() to release any vnode references that were
1785 * established via ubc_map().
1787 * IMPORTANT: This is an internal use function that is used
1788 * exclusively by the internal use function vclean().
1790 __private_extern__
void
1791 ubc_destroy_named(vnode_t vp
)
1793 memory_object_control_t control
;
1794 struct ubc_info
*uip
;
1797 if (UBCINFOEXISTS(vp
)) {
1798 uip
= vp
->v_ubcinfo
;
1800 /* Terminate the memory object */
1801 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
1802 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1803 kret
= memory_object_destroy(control
, 0);
1804 if (kret
!= KERN_SUCCESS
)
1805 panic("ubc_destroy_named: memory_object_destroy failed");
1814 * Determine whether or not a vnode is currently in use by ubc at a level in
1815 * excess of the requested busycount
1817 * Parameters: vp The vnode to check
1818 * busycount The threshold busy count, used to bias
1819 * the count usually already held by the
1820 * caller to avoid races
1822 * Returns: 1 The vnode is in use over the threshold
1823 * 0 The vnode is not in use over the
1826 * Notes: Because the vnode is only held locked while actually asking
1827 * the use count, this function only represents a snapshot of the
1828 * current state of the vnode. If more accurate information is
1829 * required, an additional busycount should be held by the caller
1830 * and a non-zero busycount used.
1832 * If there is no ubc_info associated with the vnode, this
1833 * function will report that the vnode is not in use by ubc.
1836 ubc_isinuse(struct vnode
*vp
, int busycount
)
1838 if ( !UBCINFOEXISTS(vp
))
1840 return(ubc_isinuse_locked(vp
, busycount
, 0));
1845 * ubc_isinuse_locked
1847 * Determine whether or not a vnode is currently in use by ubc at a level in
1848 * excess of the requested busycount
1850 * Parameters: vp The vnode to check
1851 * busycount The threshold busy count, used to bias
1852 * the count usually already held by the
1853 * caller to avoid races
1854 * locked True if the vnode is already locked by
1857 * Returns: 1 The vnode is in use over the threshold
1858 * 0 The vnode is not in use over the
1861 * Notes: If the vnode is not locked on entry, it is locked while
1862 * actually asking the use count. If this is the case, this
1863 * function only represents a snapshot of the current state of
1864 * the vnode. If more accurate information is required, the
1865 * vnode lock should be held by the caller, otherwise an
1866 * additional busycount should be held by the caller and a
1867 * non-zero busycount used.
1869 * If there is no ubc_info associated with the vnode, this
1870 * function will report that the vnode is not in use by ubc.
1873 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
1879 vnode_lock_spin(vp
);
1881 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
1893 * Reverse the effects of a ubc_map() call for a given vnode
1895 * Parameters: vp vnode to unmap from ubc
1899 * Notes: This is an internal use function used by vnode_pager_unmap().
1900 * It will attempt to obtain a reference on the supplied vnode,
1901 * and if it can do so, and there is an associated ubc_info, and
1902 * the flags indicate that it was mapped via ubc_map(), then the
1903 * flag is cleared, the mapping removed, and the reference taken
1904 * by ubc_map() is released.
1906 * IMPORTANT: This MUST only be called by the VM
1907 * to prevent race conditions.
1909 __private_extern__
void
1910 ubc_unmap(struct vnode
*vp
)
1912 struct ubc_info
*uip
;
1914 int need_wakeup
= 0;
1916 if (vnode_getwithref(vp
))
1919 if (UBCINFOEXISTS(vp
)) {
1921 uip
= vp
->v_ubcinfo
;
1923 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1924 SET(uip
->ui_flags
, UI_MAPWAITING
);
1925 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1926 PRIBIO
, "ubc_unmap", NULL
);
1928 SET(uip
->ui_flags
, UI_MAPBUSY
);
1930 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
1931 CLR(uip
->ui_flags
, UI_ISMAPPED
);
1937 (void)VNOP_MNOMAP(vp
, vfs_context_current());
1941 vnode_lock_spin(vp
);
1943 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1944 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1945 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1951 wakeup(&uip
->ui_flags
);
1955 * the drop of the vnode ref will cleanup
1964 * Manipulate individual page state for a vnode with an associated ubc_info
1965 * with an associated memory object control.
1967 * Parameters: vp The vnode backing the page
1968 * f_offset A file offset interior to the page
1969 * ops The operations to perform, as a bitmap
1970 * (see below for more information)
1971 * phys_entryp The address of a ppnum_t; may be NULL
1973 * flagsp A pointer to an int to contain flags;
1974 * may be NULL to ignore
1976 * Returns: KERN_SUCCESS Success
1977 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1979 * KERN_INVALID_OBJECT If UPL_POP_PHYSICAL and the object is
1980 * not physically contiguous
1981 * KERN_INVALID_OBJECT If !UPL_POP_PHYSICAL and the object is
1982 * physically contiguous
1983 * KERN_FAILURE If the page cannot be looked up
1986 * *phys_entryp (modified) If phys_entryp is non-NULL and
1988 * *flagsp (modified) If flagsp is non-NULL and there was
1989 * !UPL_POP_PHYSICAL and a KERN_SUCCESS
1991 * Notes: For object boundaries, it is considerably more efficient to
1992 * ensure that f_offset is in fact on a page boundary, as this
1993 * will avoid internal use of the hash table to identify the
1994 * page, and would therefore skip a number of early optimizations.
1995 * Since this is a page operation anyway, the caller should try
1996 * to pass only a page aligned offset because of this.
1998 * *flagsp may be modified even if this function fails. If it is
1999 * modified, it will contain the condition of the page before the
2000 * requested operation was attempted; these will only include the
2001 * bitmap flags, and not the PL_POP_PHYSICAL, UPL_POP_DUMP,
2002 * UPL_POP_SET, or UPL_POP_CLR bits.
2004 * The flags field may contain a specific operation, such as
2005 * UPL_POP_PHYSICAL or UPL_POP_DUMP:
2007 * o UPL_POP_PHYSICAL Fail if not contiguous; if
2008 * *phys_entryp and successful, set
2010 * o UPL_POP_DUMP Dump the specified page
2012 * Otherwise, it is treated as a bitmap of one or more page
2013 * operations to perform on the final memory object; allowable
2016 * o UPL_POP_DIRTY The page is dirty
2017 * o UPL_POP_PAGEOUT The page is paged out
2018 * o UPL_POP_PRECIOUS The page is precious
2019 * o UPL_POP_ABSENT The page is absent
2020 * o UPL_POP_BUSY The page is busy
2022 * If the page status is only being queried and not modified, then
2023 * not other bits should be specified. However, if it is being
2024 * modified, exactly ONE of the following bits should be set:
2026 * o UPL_POP_SET Set the current bitmap bits
2027 * o UPL_POP_CLR Clear the current bitmap bits
2029 * Thus to effect a combination of setting an clearing, it may be
2030 * necessary to call this function twice. If this is done, the
2031 * set should be used before the clear, since clearing may trigger
2032 * a wakeup on the destination page, and if the page is backed by
2033 * an encrypted swap file, setting will trigger the decryption
2034 * needed before the wakeup occurs.
2041 ppnum_t
*phys_entryp
,
2044 memory_object_control_t control
;
2046 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2047 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2048 return KERN_INVALID_ARGUMENT
;
2050 return (memory_object_page_op(control
,
2051 (memory_object_offset_t
)f_offset
,
2061 * Manipulate page state for a range of memory for a vnode with an associated
2062 * ubc_info with an associated memory object control, when page level state is
2063 * not required to be returned from the call (i.e. there are no phys_entryp or
2064 * flagsp parameters to this call, and it takes a range which may contain
2065 * multiple pages, rather than an offset interior to a single page).
2067 * Parameters: vp The vnode backing the page
2068 * f_offset_beg A file offset interior to the start page
2069 * f_offset_end A file offset interior to the end page
2070 * ops The operations to perform, as a bitmap
2071 * (see below for more information)
2072 * range The address of an int; may be NULL to
2075 * Returns: KERN_SUCCESS Success
2076 * KERN_INVALID_ARGUMENT If the memory object control has no VM
2078 * KERN_INVALID_OBJECT If the object is physically contiguous
2081 * *range (modified) If range is non-NULL, its contents will
2082 * be modified to contain the number of
2083 * bytes successfully operated upon.
2085 * Notes: IMPORTANT: This function cannot be used on a range that
2086 * consists of physically contiguous pages.
2088 * For object boundaries, it is considerably more efficient to
2089 * ensure that f_offset_beg and f_offset_end are in fact on page
2090 * boundaries, as this will avoid internal use of the hash table
2091 * to identify the page, and would therefore skip a number of
2092 * early optimizations. Since this is an operation on a set of
2093 * pages anyway, the caller should try to pass only a page aligned
2094 * offsets because of this.
2096 * *range will be modified only if this function succeeds.
2098 * The flags field MUST contain a specific operation; allowable
2101 * o UPL_ROP_ABSENT Returns the extent of the range
2102 * presented which is absent, starting
2103 * with the start address presented
2105 * o UPL_ROP_PRESENT Returns the extent of the range
2106 * presented which is present (resident),
2107 * starting with the start address
2109 * o UPL_ROP_DUMP Dump the pages which are found in the
2110 * target object for the target range.
2112 * IMPORTANT: For UPL_ROP_ABSENT and UPL_ROP_PRESENT; if there are
2113 * multiple regions in the range, only the first matching region
2124 memory_object_control_t control
;
2126 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2127 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2128 return KERN_INVALID_ARGUMENT
;
2130 return (memory_object_range_op(control
,
2131 (memory_object_offset_t
)f_offset_beg
,
2132 (memory_object_offset_t
)f_offset_end
,
2141 * Given a vnode, cause the population of a portion of the vm_object; based on
2142 * the nature of the request, the pages returned may contain valid data, or
2143 * they may be uninitialized.
2145 * Parameters: vp The vnode from which to create the upl
2146 * f_offset The start offset into the backing store
2147 * represented by the vnode
2148 * bufsize The size of the upl to create
2149 * uplp Pointer to the upl_t to receive the
2150 * created upl; MUST NOT be NULL
2151 * plp Pointer to receive the internal page
2152 * list for the created upl; MAY be NULL
2155 * Returns: KERN_SUCCESS The requested upl has been created
2156 * KERN_INVALID_ARGUMENT The bufsize argument is not an even
2157 * multiple of the page size
2158 * KERN_INVALID_ARGUMENT There is no ubc_info associated with
2159 * the vnode, or there is no memory object
2160 * control associated with the ubc_info
2161 * memory_object_upl_request:KERN_INVALID_VALUE
2162 * The supplied upl_flags argument is
2166 * *plp (modified) If non-NULL, the value of *plp will be
2167 * modified to point to the internal page
2168 * list; this modification may occur even
2169 * if this function is unsuccessful, in
2170 * which case the contents may be invalid
2172 * Note: If successful, the returned *uplp MUST subsequently be freed
2173 * via a call to ubc_upl_commit(), ubc_upl_commit_range(),
2174 * ubc_upl_abort(), or ubc_upl_abort_range().
2182 upl_page_info_t
**plp
,
2185 memory_object_control_t control
;
2192 if (bufsize
& 0xfff)
2193 return KERN_INVALID_ARGUMENT
;
2195 if (bufsize
> MAX_UPL_SIZE
* PAGE_SIZE
)
2196 return KERN_INVALID_ARGUMENT
;
2198 if (uplflags
& (UPL_UBC_MSYNC
| UPL_UBC_PAGEOUT
| UPL_UBC_PAGEIN
)) {
2200 if (uplflags
& UPL_UBC_MSYNC
) {
2201 uplflags
&= UPL_RET_ONLY_DIRTY
;
2203 uplflags
|= UPL_COPYOUT_FROM
| UPL_CLEAN_IN_PLACE
|
2204 UPL_SET_INTERNAL
| UPL_SET_LITE
;
2206 } else if (uplflags
& UPL_UBC_PAGEOUT
) {
2207 uplflags
&= UPL_RET_ONLY_DIRTY
;
2209 if (uplflags
& UPL_RET_ONLY_DIRTY
)
2210 uplflags
|= UPL_NOBLOCK
;
2212 uplflags
|= UPL_FOR_PAGEOUT
| UPL_CLEAN_IN_PLACE
|
2213 UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
| UPL_SET_LITE
;
2215 uplflags
|= UPL_RET_ONLY_ABSENT
|
2216 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
2217 UPL_SET_INTERNAL
| UPL_SET_LITE
;
2220 * if the requested size == PAGE_SIZE, we don't want to set
2221 * the UPL_NOBLOCK since we may be trying to recover from a
2222 * previous partial pagein I/O that occurred because we were low
2223 * on memory and bailed early in order to honor the UPL_NOBLOCK...
2224 * since we're only asking for a single page, we can block w/o fear
2225 * of tying up pages while waiting for more to become available
2227 if (bufsize
> PAGE_SIZE
)
2228 uplflags
|= UPL_NOBLOCK
;
2231 uplflags
&= ~UPL_FOR_PAGEOUT
;
2233 if (uplflags
& UPL_WILL_BE_DUMPED
) {
2234 uplflags
&= ~UPL_WILL_BE_DUMPED
;
2235 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
2237 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
2239 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2240 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2241 return KERN_INVALID_ARGUMENT
;
2243 kr
= memory_object_upl_request(control
, f_offset
, bufsize
, uplp
, NULL
, NULL
, uplflags
);
2244 if (kr
== KERN_SUCCESS
&& plp
!= NULL
)
2245 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
2251 * ubc_upl_maxbufsize
2253 * Return the maximum bufsize ubc_create_upl( ) will take.
2257 * Returns: maximum size buffer (in bytes) ubc_create_upl( ) will take.
2263 return(MAX_UPL_SIZE
* PAGE_SIZE
);
2269 * Map the page list assocated with the supplied upl into the kernel virtual
2270 * address space at the virtual address indicated by the dst_addr argument;
2271 * the entire upl is mapped
2273 * Parameters: upl The upl to map
2274 * dst_addr The address at which to map the upl
2276 * Returns: KERN_SUCCESS The upl has been mapped
2277 * KERN_INVALID_ARGUMENT The upl is UPL_NULL
2278 * KERN_FAILURE The upl is already mapped
2279 * vm_map_enter:KERN_INVALID_ARGUMENT
2280 * A failure code from vm_map_enter() due
2281 * to an invalid argument
2286 vm_offset_t
*dst_addr
)
2288 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
2295 * Unmap the page list assocated with the supplied upl from the kernel virtual
2296 * address space; the entire upl is unmapped.
2298 * Parameters: upl The upl to unmap
2300 * Returns: KERN_SUCCESS The upl has been unmapped
2301 * KERN_FAILURE The upl is not currently mapped
2302 * KERN_INVALID_ARGUMENT If the upl is UPL_NULL
2308 return(vm_upl_unmap(kernel_map
, upl
));
2315 * Commit the contents of the upl to the backing store
2317 * Parameters: upl The upl to commit
2319 * Returns: KERN_SUCCESS The upl has been committed
2320 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2321 * KERN_FAILURE The supplied upl does not represent
2322 * device memory, and the offset plus the
2323 * size would exceed the actual size of
2326 * Notes: In practice, the only return value for this function should be
2327 * KERN_SUCCESS, unless there has been data structure corruption;
2328 * since the upl is deallocated regardless of success or failure,
2329 * there's really nothing to do about this other than panic.
2331 * IMPORTANT: Use of this function should not be mixed with use of
2332 * ubc_upl_commit_range(), due to the unconditional deallocation
2339 upl_page_info_t
*pl
;
2342 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2343 kr
= upl_commit(upl
, pl
, MAX_UPL_SIZE
);
2344 upl_deallocate(upl
);
2352 * Commit the contents of the specified range of the upl to the backing store
2354 * Parameters: upl The upl to commit
2355 * offset The offset into the upl
2356 * size The size of the region to be committed,
2357 * starting at the specified offset
2358 * flags commit type (see below)
2360 * Returns: KERN_SUCCESS The range has been committed
2361 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2362 * KERN_FAILURE The supplied upl does not represent
2363 * device memory, and the offset plus the
2364 * size would exceed the actual size of
2367 * Notes: IMPORTANT: If the commit is successful, and the object is now
2368 * empty, the upl will be deallocated. Since the caller cannot
2369 * check that this is the case, the UPL_COMMIT_FREE_ON_EMPTY flag
2370 * should generally only be used when the offset is 0 and the size
2371 * is equal to the upl size.
2373 * The flags argument is a bitmap of flags on the rage of pages in
2374 * the upl to be committed; allowable flags are:
2376 * o UPL_COMMIT_FREE_ON_EMPTY Free the upl when it is
2377 * both empty and has been
2378 * successfully committed
2379 * o UPL_COMMIT_CLEAR_DIRTY Clear each pages dirty
2380 * bit; will prevent a
2382 * o UPL_COMMIT_SET_DIRTY Set each pages dirty
2383 * bit; will cause a later
2385 * o UPL_COMMIT_INACTIVATE Clear each pages
2386 * reference bit; the page
2387 * will not be accessed
2388 * o UPL_COMMIT_ALLOW_ACCESS Unbusy each page; pages
2389 * become busy when an
2390 * IOMemoryDescriptor is
2391 * mapped or redirected,
2392 * and we have to wait for
2395 * The flag UPL_COMMIT_NOTIFY_EMPTY is used internally, and should
2396 * not be specified by the caller.
2398 * The UPL_COMMIT_CLEAR_DIRTY and UPL_COMMIT_SET_DIRTY flags are
2399 * mutually exclusive, and should not be combined.
2402 ubc_upl_commit_range(
2404 upl_offset_t offset
,
2408 upl_page_info_t
*pl
;
2412 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
2413 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
2415 if (flags
& UPL_COMMIT_KERNEL_ONLY_FLAGS
) {
2416 return KERN_INVALID_ARGUMENT
;
2419 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2421 kr
= upl_commit_range(upl
, offset
, size
, flags
,
2422 pl
, MAX_UPL_SIZE
, &empty
);
2424 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
2425 upl_deallocate(upl
);
2432 * ubc_upl_abort_range
2434 * Abort the contents of the specified range of the specified upl
2436 * Parameters: upl The upl to abort
2437 * offset The offset into the upl
2438 * size The size of the region to be aborted,
2439 * starting at the specified offset
2440 * abort_flags abort type (see below)
2442 * Returns: KERN_SUCCESS The range has been aborted
2443 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2444 * KERN_FAILURE The supplied upl does not represent
2445 * device memory, and the offset plus the
2446 * size would exceed the actual size of
2449 * Notes: IMPORTANT: If the abort is successful, and the object is now
2450 * empty, the upl will be deallocated. Since the caller cannot
2451 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2452 * should generally only be used when the offset is 0 and the size
2453 * is equal to the upl size.
2455 * The abort_flags argument is a bitmap of flags on the range of
2456 * pages in the upl to be aborted; allowable flags are:
2458 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2459 * empty and has been successfully
2461 * o UPL_ABORT_RESTART The operation must be restarted
2462 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2463 * o UPL_ABORT_ERROR An I/O error occurred
2464 * o UPL_ABORT_DUMP_PAGES Just free the pages
2465 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2466 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2468 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2469 * not be specified by the caller. It is intended to fulfill the
2470 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2471 * ubc_upl_commit_range(), but is never referenced internally.
2473 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2474 * referenced; do not use it.
2477 ubc_upl_abort_range(
2479 upl_offset_t offset
,
2484 boolean_t empty
= FALSE
;
2486 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
2487 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
2489 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
2491 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
2492 upl_deallocate(upl
);
2501 * Abort the contents of the specified upl
2503 * Parameters: upl The upl to abort
2504 * abort_type abort type (see below)
2506 * Returns: KERN_SUCCESS The range has been aborted
2507 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2508 * KERN_FAILURE The supplied upl does not represent
2509 * device memory, and the offset plus the
2510 * size would exceed the actual size of
2513 * Notes: IMPORTANT: If the abort is successful, and the object is now
2514 * empty, the upl will be deallocated. Since the caller cannot
2515 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2516 * should generally only be used when the offset is 0 and the size
2517 * is equal to the upl size.
2519 * The abort_type is a bitmap of flags on the range of
2520 * pages in the upl to be aborted; allowable flags are:
2522 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2523 * empty and has been successfully
2525 * o UPL_ABORT_RESTART The operation must be restarted
2526 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2527 * o UPL_ABORT_ERROR An I/O error occurred
2528 * o UPL_ABORT_DUMP_PAGES Just free the pages
2529 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2530 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2532 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2533 * not be specified by the caller. It is intended to fulfill the
2534 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2535 * ubc_upl_commit_range(), but is never referenced internally.
2537 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2538 * referenced; do not use it.
2547 kr
= upl_abort(upl
, abort_type
);
2548 upl_deallocate(upl
);
2556 * Retrieve the internal page list for the specified upl
2558 * Parameters: upl The upl to obtain the page list from
2560 * Returns: !NULL The (upl_page_info_t *) for the page
2561 * list internal to the upl
2562 * NULL Error/no page list associated
2564 * Notes: IMPORTANT: The function is only valid on internal objects
2565 * where the list request was made with the UPL_INTERNAL flag.
2567 * This function is a utility helper function, since some callers
2568 * may not have direct access to the header defining the macro,
2569 * due to abstraction layering constraints.
2575 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
2580 UBCINFOEXISTS(struct vnode
* vp
)
2582 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
2587 ubc_upl_range_needed(
2592 upl_range_needed(upl
, index
, count
);
2599 #define CS_BLOB_PAGEABLE 0
2600 static volatile SInt32 cs_blob_size
= 0;
2601 static volatile SInt32 cs_blob_count
= 0;
2602 static SInt32 cs_blob_size_peak
= 0;
2603 static UInt32 cs_blob_size_max
= 0;
2604 static SInt32 cs_blob_count_peak
= 0;
2606 int cs_validation
= 1;
2608 #ifndef SECURE_KERNEL
2609 SYSCTL_INT(_vm
, OID_AUTO
, cs_validation
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_validation
, 0, "Do validate code signatures");
2611 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, (int *)(uintptr_t)&cs_blob_count
, 0, "Current number of code signature blobs");
2612 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size
, CTLFLAG_RD
| CTLFLAG_LOCKED
, (int *)(uintptr_t)&cs_blob_size
, 0, "Current size of all code signature blobs");
2613 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count_peak
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_blob_count_peak
, 0, "Peak number of code signature blobs");
2614 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_peak
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_blob_size_peak
, 0, "Peak size of code signature blobs");
2615 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_max
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_blob_size_max
, 0, "Size of biggest code signature blob");
2619 ubc_cs_blob_allocate(
2620 vm_offset_t
*blob_addr_p
,
2621 vm_size_t
*blob_size_p
)
2625 #if CS_BLOB_PAGEABLE
2626 *blob_size_p
= round_page(*blob_size_p
);
2627 kr
= kmem_alloc(kernel_map
, blob_addr_p
, *blob_size_p
);
2628 #else /* CS_BLOB_PAGEABLE */
2629 *blob_addr_p
= (vm_offset_t
) kalloc(*blob_size_p
);
2630 if (*blob_addr_p
== 0) {
2635 #endif /* CS_BLOB_PAGEABLE */
2640 ubc_cs_blob_deallocate(
2641 vm_offset_t blob_addr
,
2642 vm_size_t blob_size
)
2644 #if CS_BLOB_PAGEABLE
2645 kmem_free(kernel_map
, blob_addr
, blob_size
);
2646 #else /* CS_BLOB_PAGEABLE */
2647 kfree((void *) blob_addr
, blob_size
);
2648 #endif /* CS_BLOB_PAGEABLE */
2654 vm_address_t address
,
2658 struct ubc_info
*uip
;
2659 struct cs_blob
*blob
;
2660 memory_object_control_t control
;
2661 const CS_CodeDirectory
*cd
;
2664 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2665 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2666 return KERN_INVALID_ARGUMENT
;
2668 if (memory_object_is_signed(control
))
2671 blob
= (struct cs_blob
*) kalloc(sizeof (struct cs_blob
));
2675 /* fill in the new blob */
2676 blob
->csb_cpu_type
= CPU_TYPE_ANY
;
2677 blob
->csb_base_offset
= 0;
2678 blob
->csb_mem_size
= size
;
2679 blob
->csb_mem_offset
= 0;
2680 blob
->csb_mem_handle
= IPC_PORT_NULL
;
2681 blob
->csb_mem_kaddr
= address
;
2682 blob
->csb_sigpup
= 1;
2685 * Validate the blob's contents
2687 cd
= findCodeDirectory(
2688 (const CS_SuperBlob
*) address
,
2690 (char *) address
+ blob
->csb_mem_size
);
2692 /* no code directory => useless blob ! */
2697 blob
->csb_flags
= ntohl(cd
->flags
) | CS_VALID
;
2698 blob
->csb_end_offset
= round_page(ntohl(cd
->codeLimit
));
2699 if((ntohl(cd
->version
) >= CS_SUPPORTSSCATTER
) && (ntohl(cd
->scatterOffset
))) {
2700 const SC_Scatter
*scatter
= (const SC_Scatter
*)
2701 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
2702 blob
->csb_start_offset
= ntohl(scatter
->base
) * PAGE_SIZE
;
2704 blob
->csb_start_offset
= (blob
->csb_end_offset
- (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
));
2708 * We don't need to check with the policy module, since the input data is supposed to be already checked
2712 if (! UBCINFOEXISTS(vp
)) {
2715 printf("out ubc object\n");
2719 uip
= vp
->v_ubcinfo
;
2721 /* someone raced us to adding the code directory */
2722 if (uip
->cs_blobs
!= NULL
) {
2724 printf("sigpup: vnode already have CD ?\n");
2730 blob
->csb_next
= uip
->cs_blobs
;
2731 uip
->cs_blobs
= blob
;
2733 OSAddAtomic(+1, &cs_blob_count
);
2734 OSAddAtomic((SInt32
) +blob
->csb_mem_size
, &cs_blob_size
);
2736 /* mark this vnode's VM object as having "signed pages" */
2737 kr
= memory_object_signed(uip
->ui_control
, TRUE
);
2738 if (kr
!= KERN_SUCCESS
) {
2741 printf("sigpup: not signable ?\n");
2752 printf("sigpup: not signable ?\n");
2753 /* we failed; release what we allocated */
2755 kfree(blob
, sizeof (*blob
));
2773 struct ubc_info
*uip
;
2774 struct cs_blob
*blob
, *oblob
;
2776 ipc_port_t blob_handle
;
2777 memory_object_size_t blob_size
;
2778 const CS_CodeDirectory
*cd
;
2779 off_t blob_start_offset
, blob_end_offset
;
2781 boolean_t record_mtime
;
2783 record_mtime
= FALSE
;
2785 blob_handle
= IPC_PORT_NULL
;
2787 blob
= (struct cs_blob
*) kalloc(sizeof (struct cs_blob
));
2792 #if CS_BLOB_PAGEABLE
2793 /* get a memory entry on the blob */
2794 blob_size
= (memory_object_size_t
) size
;
2795 kr
= mach_make_memory_entry_64(kernel_map
,
2801 if (kr
!= KERN_SUCCESS
) {
2805 if (memory_object_round_page(blob_size
) !=
2806 (memory_object_size_t
) round_page(size
)) {
2807 printf("ubc_cs_blob_add: size mismatch 0x%llx 0x%lx !?\n",
2808 blob_size
, (size_t)size
);
2809 panic("XXX FBDP size mismatch 0x%llx 0x%lx\n", blob_size
, (size_t)size
);
2814 blob_size
= (memory_object_size_t
) size
;
2815 blob_handle
= IPC_PORT_NULL
;
2818 /* fill in the new blob */
2819 blob
->csb_cpu_type
= cputype
;
2820 blob
->csb_sigpup
= 0;
2821 blob
->csb_base_offset
= base_offset
;
2822 blob
->csb_blob_offset
= blob_offset
;
2823 blob
->csb_mem_size
= size
;
2824 blob
->csb_mem_offset
= 0;
2825 blob
->csb_mem_handle
= blob_handle
;
2826 blob
->csb_mem_kaddr
= addr
;
2827 blob
->csb_flags
= 0;
2830 * Validate the blob's contents
2833 error
= cs_validate_csblob((const uint8_t *)addr
, size
, &cd
);
2836 printf("CODESIGNING: csblob invalid: %d\n", error
);
2837 blob
->csb_flags
= 0;
2838 blob
->csb_start_offset
= 0;
2839 blob
->csb_end_offset
= 0;
2840 memset(blob
->csb_sha1
, 0, SHA1_RESULTLEN
);
2841 /* let the vnode checker determine if the signature is valid or not */
2843 const unsigned char *sha1_base
;
2846 blob
->csb_flags
= (ntohl(cd
->flags
) & CS_ALLOWED_MACHO
) | CS_VALID
;
2847 blob
->csb_end_offset
= round_page(ntohl(cd
->codeLimit
));
2848 if((ntohl(cd
->version
) >= CS_SUPPORTSSCATTER
) && (ntohl(cd
->scatterOffset
))) {
2849 const SC_Scatter
*scatter
= (const SC_Scatter
*)
2850 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
2851 blob
->csb_start_offset
= ntohl(scatter
->base
) * PAGE_SIZE
;
2853 blob
->csb_start_offset
= (blob
->csb_end_offset
-
2854 (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
));
2856 /* compute the blob's SHA1 hash */
2857 sha1_base
= (const unsigned char *) cd
;
2858 sha1_size
= ntohl(cd
->length
);
2859 SHA1Init(&sha1ctxt
);
2860 SHA1Update(&sha1ctxt
, sha1_base
, sha1_size
);
2861 SHA1Final(blob
->csb_sha1
, &sha1ctxt
);
2865 * Let policy module check whether the blob's signature is accepted.
2868 error
= mac_vnode_check_signature(vp
, base_offset
, blob
->csb_sha1
, (void*)addr
, size
);
2874 * Validate the blob's coverage
2876 blob_start_offset
= blob
->csb_base_offset
+ blob
->csb_start_offset
;
2877 blob_end_offset
= blob
->csb_base_offset
+ blob
->csb_end_offset
;
2879 if (blob_start_offset
>= blob_end_offset
||
2880 blob_start_offset
< 0 ||
2881 blob_end_offset
<= 0) {
2882 /* reject empty or backwards blob */
2888 if (! UBCINFOEXISTS(vp
)) {
2893 uip
= vp
->v_ubcinfo
;
2895 /* check if this new blob overlaps with an existing blob */
2896 for (oblob
= uip
->cs_blobs
;
2898 oblob
= oblob
->csb_next
) {
2899 off_t oblob_start_offset
, oblob_end_offset
;
2901 oblob_start_offset
= (oblob
->csb_base_offset
+
2902 oblob
->csb_start_offset
);
2903 oblob_end_offset
= (oblob
->csb_base_offset
+
2904 oblob
->csb_end_offset
);
2905 if (blob_start_offset
>= oblob_end_offset
||
2906 blob_end_offset
<= oblob_start_offset
) {
2907 /* no conflict with this existing blob */
2910 if (blob_start_offset
== oblob_start_offset
&&
2911 blob_end_offset
== oblob_end_offset
&&
2912 blob
->csb_mem_size
== oblob
->csb_mem_size
&&
2913 blob
->csb_flags
== oblob
->csb_flags
&&
2914 (blob
->csb_cpu_type
== CPU_TYPE_ANY
||
2915 oblob
->csb_cpu_type
== CPU_TYPE_ANY
||
2916 blob
->csb_cpu_type
== oblob
->csb_cpu_type
) &&
2917 !bcmp(blob
->csb_sha1
,
2921 * We already have this blob:
2922 * we'll return success but
2923 * throw away the new blob.
2925 if (oblob
->csb_cpu_type
== CPU_TYPE_ANY
) {
2927 * The old blob matches this one
2928 * but doesn't have any CPU type.
2929 * Update it with whatever the caller
2930 * provided this time.
2932 oblob
->csb_cpu_type
= cputype
;
2935 * If the same blob moved around in the Mach-O, we
2936 * want to remember the new blob offset to avoid
2937 * coming back here again and again.
2939 oblob
->csb_blob_offset
= blob_offset
;
2945 /* different blob: reject the new one */
2946 char pathbuf
[MAXPATHLEN
];
2947 char new_sha1_str
[2*SHA1_RESULTLEN
+1];
2948 char old_sha1_str
[2*SHA1_RESULTLEN
+1];
2950 const char *pathp
= "?unknown";
2951 int pblen
= sizeof(pathbuf
);
2952 if (vn_getpath(vp
, pathbuf
, &pblen
) == 0) {
2953 /* pblen == strlen(pathbuf) + 1. Assume strlen(pathbuf) > 0 */
2954 for (pathp
= pathbuf
+ pblen
- 2; pathp
> pathbuf
&& pathp
[-1] != '/'; pathp
--) ;
2956 snprintf(arch_str
, sizeof(arch_str
), "%x", cputype
);
2957 hex_str(oblob
->csb_sha1
, SHA1_RESULTLEN
, old_sha1_str
);
2958 hex_str(blob
->csb_sha1
, SHA1_RESULTLEN
, new_sha1_str
);
2959 kern_asl_msg(LOG_NOTICE
, "messagetracer",
2961 "com.apple.message.domain", "com.apple.kernel.cs.replace",
2962 "com.apple.message.signature", pathp
,
2963 "com.apple.message.signature2", arch_str
,
2964 "com.apple.message.signature3", old_sha1_str
,
2965 "com.apple.message.result", new_sha1_str
,
2966 "com.apple.message.summarize", "YES",
2969 printf("CODESIGNING: rejected new signature for architecture %d of file %s\n",
2979 /* mark this vnode's VM object as having "signed pages" */
2980 kr
= memory_object_signed(uip
->ui_control
, TRUE
);
2981 if (kr
!= KERN_SUCCESS
) {
2987 if (uip
->cs_blobs
== NULL
) {
2988 /* loading 1st blob: record the file's current "modify time" */
2989 record_mtime
= TRUE
;
2993 * Add this blob to the list of blobs for this vnode.
2994 * We always add at the front of the list and we never remove a
2995 * blob from the list, so ubc_cs_get_blobs() can return whatever
2996 * the top of the list was and that list will remain valid
2997 * while we validate a page, even after we release the vnode's lock.
2999 blob
->csb_next
= uip
->cs_blobs
;
3000 uip
->cs_blobs
= blob
;
3002 OSAddAtomic(+1, &cs_blob_count
);
3003 if (cs_blob_count
> cs_blob_count_peak
) {
3004 cs_blob_count_peak
= cs_blob_count
; /* XXX atomic ? */
3006 OSAddAtomic((SInt32
) +blob
->csb_mem_size
, &cs_blob_size
);
3007 if ((SInt32
) cs_blob_size
> cs_blob_size_peak
) {
3008 cs_blob_size_peak
= (SInt32
) cs_blob_size
; /* XXX atomic ? */
3010 if ((UInt32
) blob
->csb_mem_size
> cs_blob_size_max
) {
3011 cs_blob_size_max
= (UInt32
) blob
->csb_mem_size
;
3016 const char *name
= vnode_getname_printable(vp
);
3018 printf("CODE SIGNING: proc %d(%s) "
3019 "loaded %s signatures for file (%s) "
3020 "range 0x%llx:0x%llx flags 0x%x\n",
3021 p
->p_pid
, p
->p_comm
,
3022 blob
->csb_cpu_type
== -1 ? "detached" : "embedded",
3024 blob
->csb_base_offset
+ blob
->csb_start_offset
,
3025 blob
->csb_base_offset
+ blob
->csb_end_offset
,
3027 vnode_putname_printable(name
);
3033 vnode_mtime(vp
, &uip
->cs_mtime
, vfs_context_current());
3036 error
= 0; /* success ! */
3040 /* we failed; release what we allocated */
3042 kfree(blob
, sizeof (*blob
));
3045 if (blob_handle
!= IPC_PORT_NULL
) {
3046 mach_memory_entry_port_release(blob_handle
);
3047 blob_handle
= IPC_PORT_NULL
;
3051 if (error
== EAGAIN
) {
3053 * See above: error is EAGAIN if we were asked
3054 * to add an existing blob again. We cleaned the new
3055 * blob and we want to return success.
3059 * Since we're not failing, consume the data we received.
3061 ubc_cs_blob_deallocate(addr
, size
);
3074 struct ubc_info
*uip
;
3075 struct cs_blob
*blob
;
3076 off_t offset_in_blob
;
3078 vnode_lock_spin(vp
);
3080 if (! UBCINFOEXISTS(vp
)) {
3085 uip
= vp
->v_ubcinfo
;
3086 for (blob
= uip
->cs_blobs
;
3088 blob
= blob
->csb_next
) {
3089 if (cputype
!= -1 && blob
->csb_cpu_type
== cputype
) {
3093 offset_in_blob
= offset
- blob
->csb_base_offset
;
3094 if (offset_in_blob
>= blob
->csb_start_offset
&&
3095 offset_in_blob
< blob
->csb_end_offset
) {
3096 /* our offset is covered by this blob */
3102 if (cs_debug
&& blob
!= NULL
&& blob
->csb_sigpup
)
3103 printf("found sig pup blob\n");
3112 struct ubc_info
*uip
)
3114 struct cs_blob
*blob
, *next_blob
;
3116 for (blob
= uip
->cs_blobs
;
3119 next_blob
= blob
->csb_next
;
3120 if (blob
->csb_mem_kaddr
!= 0 && !blob
->csb_sigpup
) {
3121 ubc_cs_blob_deallocate(blob
->csb_mem_kaddr
,
3122 blob
->csb_mem_size
);
3123 blob
->csb_mem_kaddr
= 0;
3125 if (blob
->csb_mem_handle
!= IPC_PORT_NULL
) {
3126 mach_memory_entry_port_release(blob
->csb_mem_handle
);
3128 blob
->csb_mem_handle
= IPC_PORT_NULL
;
3129 OSAddAtomic(-1, &cs_blob_count
);
3130 OSAddAtomic((SInt32
) -blob
->csb_mem_size
, &cs_blob_size
);
3131 kfree(blob
, sizeof (*blob
));
3133 #if CHECK_CS_VALIDATION_BITMAP
3134 ubc_cs_validation_bitmap_deallocate( uip
->ui_vnode
);
3136 uip
->cs_blobs
= NULL
;
3143 struct ubc_info
*uip
;
3144 struct cs_blob
*blobs
;
3147 * No need to take the vnode lock here. The caller must be holding
3148 * a reference on the vnode (via a VM mapping or open file descriptor),
3149 * so the vnode will not go away. The ubc_info stays until the vnode
3150 * goes away. And we only modify "blobs" by adding to the head of the
3152 * The ubc_info could go away entirely if the vnode gets reclaimed as
3153 * part of a forced unmount. In the case of a code-signature validation
3154 * during a page fault, the "paging_in_progress" reference on the VM
3155 * object guarantess that the vnode pager (and the ubc_info) won't go
3156 * away during the fault.
3157 * Other callers need to protect against vnode reclaim by holding the
3158 * vnode lock, for example.
3161 if (! UBCINFOEXISTS(vp
)) {
3166 uip
= vp
->v_ubcinfo
;
3167 blobs
= uip
->cs_blobs
;
3176 struct timespec
*cs_mtime
)
3178 struct ubc_info
*uip
;
3180 if (! UBCINFOEXISTS(vp
)) {
3181 cs_mtime
->tv_sec
= 0;
3182 cs_mtime
->tv_nsec
= 0;
3186 uip
= vp
->v_ubcinfo
;
3187 cs_mtime
->tv_sec
= uip
->cs_mtime
.tv_sec
;
3188 cs_mtime
->tv_nsec
= uip
->cs_mtime
.tv_nsec
;
3191 unsigned long cs_validate_page_no_hash
= 0;
3192 unsigned long cs_validate_page_bad_hash
= 0;
3196 memory_object_t pager
,
3197 memory_object_offset_t page_offset
,
3202 unsigned char actual_hash
[SHA1_RESULTLEN
];
3203 unsigned char expected_hash
[SHA1_RESULTLEN
];
3204 boolean_t found_hash
;
3205 struct cs_blob
*blobs
, *blob
;
3206 const CS_CodeDirectory
*cd
;
3207 const CS_SuperBlob
*embedded
;
3208 const unsigned char *hash
;
3209 boolean_t validated
;
3210 off_t offset
; /* page offset in the file */
3212 off_t codeLimit
= 0;
3213 char *lower_bound
, *upper_bound
;
3214 vm_offset_t kaddr
, blob_addr
;
3218 offset
= page_offset
;
3220 /* retrieve the expected hash */
3222 blobs
= (struct cs_blob
*) _blobs
;
3226 blob
= blob
->csb_next
) {
3227 offset
= page_offset
- blob
->csb_base_offset
;
3228 if (offset
< blob
->csb_start_offset
||
3229 offset
>= blob
->csb_end_offset
) {
3230 /* our page is not covered by this blob */
3234 /* map the blob in the kernel address space */
3235 kaddr
= blob
->csb_mem_kaddr
;
3237 ksize
= (vm_size_t
) (blob
->csb_mem_size
+
3238 blob
->csb_mem_offset
);
3239 kr
= vm_map(kernel_map
,
3244 blob
->csb_mem_handle
,
3250 if (kr
!= KERN_SUCCESS
) {
3251 /* XXX FBDP what to do !? */
3252 printf("cs_validate_page: failed to map blob, "
3253 "size=0x%lx kr=0x%x\n",
3254 (size_t)blob
->csb_mem_size
, kr
);
3258 if (blob
->csb_sigpup
&& cs_debug
)
3259 printf("checking for a sigpup CD\n");
3261 blob_addr
= kaddr
+ blob
->csb_mem_offset
;
3263 lower_bound
= CAST_DOWN(char *, blob_addr
);
3264 upper_bound
= lower_bound
+ blob
->csb_mem_size
;
3266 embedded
= (const CS_SuperBlob
*) blob_addr
;
3267 cd
= findCodeDirectory(embedded
, lower_bound
, upper_bound
);
3269 if (cd
->pageSize
!= PAGE_SHIFT
||
3270 cd
->hashType
!= CS_HASHTYPE_SHA1
||
3271 cd
->hashSize
!= SHA1_RESULTLEN
) {
3273 if (blob
->csb_sigpup
&& cs_debug
)
3274 printf("page foo bogus sigpup CD\n");
3278 offset
= page_offset
- blob
->csb_base_offset
;
3279 if (offset
< blob
->csb_start_offset
||
3280 offset
>= blob
->csb_end_offset
) {
3281 /* our page is not covered by this blob */
3282 if (blob
->csb_sigpup
&& cs_debug
)
3283 printf("OOB sigpup CD\n");
3287 codeLimit
= ntohl(cd
->codeLimit
);
3288 if (blob
->csb_sigpup
&& cs_debug
)
3289 printf("sigpup codesize %d\n", (int)codeLimit
);
3291 hash
= hashes(cd
, (unsigned)atop(offset
),
3292 lower_bound
, upper_bound
);
3294 bcopy(hash
, expected_hash
,
3295 sizeof (expected_hash
));
3297 if (blob
->csb_sigpup
&& cs_debug
)
3298 printf("sigpup hash\n");
3303 if (blob
->csb_sigpup
&& cs_debug
)
3304 printf("sig pup had no valid CD\n");
3309 if (found_hash
== FALSE
) {
3311 * We can't verify this page because there is no signature
3312 * for it (yet). It's possible that this part of the object
3313 * is not signed, or that signatures for that part have not
3315 * Report that the page has not been validated and let the
3316 * caller decide if it wants to accept it or not.
3318 cs_validate_page_no_hash
++;
3320 printf("CODE SIGNING: cs_validate_page: "
3321 "mobj %p off 0x%llx: no hash to validate !?\n",
3322 pager
, page_offset
);
3329 if ((off_t
)(offset
+ size
) > codeLimit
) {
3330 /* partial page at end of segment */
3331 assert(offset
< codeLimit
);
3332 size
= (size_t) (codeLimit
& PAGE_MASK
);
3334 /* compute the actual page's SHA1 hash */
3335 SHA1Init(&sha1ctxt
);
3336 SHA1UpdateUsePhysicalAddress(&sha1ctxt
, data
, size
);
3337 SHA1Final(actual_hash
, &sha1ctxt
);
3339 if (bcmp(expected_hash
, actual_hash
, SHA1_RESULTLEN
) != 0) {
3340 char asha1_str
[2*SHA1_RESULTLEN
+1];
3341 char esha1_str
[2*SHA1_RESULTLEN
+1];
3342 hex_str(actual_hash
, SHA1_RESULTLEN
, asha1_str
);
3343 hex_str(expected_hash
, SHA1_RESULTLEN
, esha1_str
);
3345 printf("CODE SIGNING: cs_validate_page: "
3346 "mobj %p off 0x%llx size 0x%lx: actual %s expected %s\n",
3347 pager
, page_offset
, size
, asha1_str
, esha1_str
);
3349 cs_validate_page_bad_hash
++;
3351 char page_offset_str
[20];
3352 snprintf(page_offset_str
, sizeof(page_offset_str
), "%llx", page_offset
);
3353 kern_asl_msg(LOG_NOTICE
, "messagetracer",
3355 "com.apple.message.domain", "com.apple.kernel.cs.mismatch",
3356 "com.apple.message.signature", page_offset_str
,
3357 "com.apple.message.signature2", asha1_str
,
3358 "com.apple.message.signature3", esha1_str
,
3359 "com.apple.message.summarize", "YES",
3365 if (cs_debug
> 10) {
3366 printf("CODE SIGNING: cs_validate_page: "
3367 "mobj %p off 0x%llx size 0x%lx: "
3369 pager
, page_offset
, size
);
3383 unsigned char *cdhash
)
3385 struct cs_blob
*blobs
, *blob
;
3391 blobs
= ubc_get_cs_blobs(vp
);
3394 blob
= blob
->csb_next
) {
3395 /* compute offset relative to this blob */
3396 rel_offset
= offset
- blob
->csb_base_offset
;
3397 if (rel_offset
>= blob
->csb_start_offset
&&
3398 rel_offset
< blob
->csb_end_offset
) {
3399 /* this blob does cover our "offset" ! */
3405 /* we didn't find a blob covering "offset" */
3406 ret
= EBADEXEC
; /* XXX any better error ? */
3408 /* get the SHA1 hash of that blob */
3409 bcopy(blob
->csb_sha1
, cdhash
, sizeof (blob
->csb_sha1
));
3418 #if CHECK_CS_VALIDATION_BITMAP
3419 #define stob(s) ((atop_64((s)) + 07) >> 3)
3420 extern boolean_t root_fs_upgrade_try
;
3423 * Should we use the code-sign bitmap to avoid repeated code-sign validation?
3425 * a) Is the target vnode on the root filesystem?
3426 * b) Has someone tried to mount the root filesystem read-write?
3427 * If answers are (a) yes AND (b) no, then we can use the bitmap.
3429 #define USE_CODE_SIGN_BITMAP(vp) ( (vp != NULL) && (vp->v_mount != NULL) && (vp->v_mount->mnt_flag & MNT_ROOTFS) && !root_fs_upgrade_try)
3431 ubc_cs_validation_bitmap_allocate(
3434 kern_return_t kr
= KERN_SUCCESS
;
3435 struct ubc_info
*uip
;
3436 char *target_bitmap
;
3437 vm_object_size_t bitmap_size
;
3439 if ( ! USE_CODE_SIGN_BITMAP(vp
) || (! UBCINFOEXISTS(vp
))) {
3440 kr
= KERN_INVALID_ARGUMENT
;
3442 uip
= vp
->v_ubcinfo
;
3444 if ( uip
->cs_valid_bitmap
== NULL
) {
3445 bitmap_size
= stob(uip
->ui_size
);
3446 target_bitmap
= (char*) kalloc( (vm_size_t
)bitmap_size
);
3447 if (target_bitmap
== 0) {
3452 if( kr
== KERN_SUCCESS
) {
3453 memset( target_bitmap
, 0, (size_t)bitmap_size
);
3454 uip
->cs_valid_bitmap
= (void*)target_bitmap
;
3455 uip
->cs_valid_bitmap_size
= bitmap_size
;
3463 ubc_cs_check_validation_bitmap (
3465 memory_object_offset_t offset
,
3468 kern_return_t kr
= KERN_SUCCESS
;
3470 if ( ! USE_CODE_SIGN_BITMAP(vp
) || ! UBCINFOEXISTS(vp
)) {
3471 kr
= KERN_INVALID_ARGUMENT
;
3473 struct ubc_info
*uip
= vp
->v_ubcinfo
;
3474 char *target_bitmap
= uip
->cs_valid_bitmap
;
3476 if ( target_bitmap
== NULL
) {
3477 kr
= KERN_INVALID_ARGUMENT
;
3480 bit
= atop_64( offset
);
3483 if ( byte
> uip
->cs_valid_bitmap_size
) {
3484 kr
= KERN_INVALID_ARGUMENT
;
3487 if (optype
== CS_BITMAP_SET
) {
3488 target_bitmap
[byte
] |= (1 << (bit
& 07));
3490 } else if (optype
== CS_BITMAP_CLEAR
) {
3491 target_bitmap
[byte
] &= ~(1 << (bit
& 07));
3493 } else if (optype
== CS_BITMAP_CHECK
) {
3494 if ( target_bitmap
[byte
] & (1 << (bit
& 07))) {
3507 ubc_cs_validation_bitmap_deallocate(
3510 struct ubc_info
*uip
;
3511 void *target_bitmap
;
3512 vm_object_size_t bitmap_size
;
3514 if ( UBCINFOEXISTS(vp
)) {
3515 uip
= vp
->v_ubcinfo
;
3517 if ( (target_bitmap
= uip
->cs_valid_bitmap
) != NULL
) {
3518 bitmap_size
= uip
->cs_valid_bitmap_size
;
3519 kfree( target_bitmap
, (vm_size_t
) bitmap_size
);
3520 uip
->cs_valid_bitmap
= NULL
;
3525 kern_return_t
ubc_cs_validation_bitmap_allocate(__unused vnode_t vp
){
3526 return KERN_INVALID_ARGUMENT
;
3529 kern_return_t
ubc_cs_check_validation_bitmap(
3530 __unused
struct vnode
*vp
,
3531 __unused memory_object_offset_t offset
,
3532 __unused
int optype
){
3534 return KERN_INVALID_ARGUMENT
;
3537 void ubc_cs_validation_bitmap_deallocate(__unused vnode_t vp
){
3540 #endif /* CHECK_CS_VALIDATION_BITMAP */