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
));
1747 if (flags
& PROT_WRITE
) {
1748 SET(uip
->ui_flags
, UI_MAPPEDWRITE
);
1751 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1753 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1754 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1760 wakeup(&uip
->ui_flags
);
1772 * Destroy the named memory object associated with the ubc_info control object
1773 * associated with the designated vnode, if there is a ubc_info associated
1774 * with the vnode, and a control object is associated with it
1776 * Parameters: vp The designated vnode
1780 * Notes: This function is called on vnode termination for all vnodes,
1781 * and must therefore not assume that there is a ubc_info that is
1782 * associated with the vnode, nor that there is a control object
1783 * associated with the ubc_info.
1785 * If all the conditions necessary are present, this function
1786 * calls memory_object_destory(), which will in turn end up
1787 * calling ubc_unmap() to release any vnode references that were
1788 * established via ubc_map().
1790 * IMPORTANT: This is an internal use function that is used
1791 * exclusively by the internal use function vclean().
1793 __private_extern__
void
1794 ubc_destroy_named(vnode_t vp
)
1796 memory_object_control_t control
;
1797 struct ubc_info
*uip
;
1800 if (UBCINFOEXISTS(vp
)) {
1801 uip
= vp
->v_ubcinfo
;
1803 /* Terminate the memory object */
1804 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
1805 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1806 kret
= memory_object_destroy(control
, 0);
1807 if (kret
!= KERN_SUCCESS
)
1808 panic("ubc_destroy_named: memory_object_destroy failed");
1817 * Determine whether or not a vnode is currently in use by ubc at a level in
1818 * excess of the requested busycount
1820 * Parameters: vp The vnode to check
1821 * busycount The threshold busy count, used to bias
1822 * the count usually already held by the
1823 * caller to avoid races
1825 * Returns: 1 The vnode is in use over the threshold
1826 * 0 The vnode is not in use over the
1829 * Notes: Because the vnode is only held locked while actually asking
1830 * the use count, this function only represents a snapshot of the
1831 * current state of the vnode. If more accurate information is
1832 * required, an additional busycount should be held by the caller
1833 * and a non-zero busycount used.
1835 * If there is no ubc_info associated with the vnode, this
1836 * function will report that the vnode is not in use by ubc.
1839 ubc_isinuse(struct vnode
*vp
, int busycount
)
1841 if ( !UBCINFOEXISTS(vp
))
1843 return(ubc_isinuse_locked(vp
, busycount
, 0));
1848 * ubc_isinuse_locked
1850 * Determine whether or not a vnode is currently in use by ubc at a level in
1851 * excess of the requested busycount
1853 * Parameters: vp The vnode to check
1854 * busycount The threshold busy count, used to bias
1855 * the count usually already held by the
1856 * caller to avoid races
1857 * locked True if the vnode is already locked by
1860 * Returns: 1 The vnode is in use over the threshold
1861 * 0 The vnode is not in use over the
1864 * Notes: If the vnode is not locked on entry, it is locked while
1865 * actually asking the use count. If this is the case, this
1866 * function only represents a snapshot of the current state of
1867 * the vnode. If more accurate information is required, the
1868 * vnode lock should be held by the caller, otherwise an
1869 * additional busycount should be held by the caller and a
1870 * non-zero busycount used.
1872 * If there is no ubc_info associated with the vnode, this
1873 * function will report that the vnode is not in use by ubc.
1876 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
1882 vnode_lock_spin(vp
);
1884 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
1896 * Reverse the effects of a ubc_map() call for a given vnode
1898 * Parameters: vp vnode to unmap from ubc
1902 * Notes: This is an internal use function used by vnode_pager_unmap().
1903 * It will attempt to obtain a reference on the supplied vnode,
1904 * and if it can do so, and there is an associated ubc_info, and
1905 * the flags indicate that it was mapped via ubc_map(), then the
1906 * flag is cleared, the mapping removed, and the reference taken
1907 * by ubc_map() is released.
1909 * IMPORTANT: This MUST only be called by the VM
1910 * to prevent race conditions.
1912 __private_extern__
void
1913 ubc_unmap(struct vnode
*vp
)
1915 struct ubc_info
*uip
;
1917 int need_wakeup
= 0;
1919 if (vnode_getwithref(vp
))
1922 if (UBCINFOEXISTS(vp
)) {
1924 uip
= vp
->v_ubcinfo
;
1926 while (ISSET(uip
->ui_flags
, UI_MAPBUSY
)) {
1927 SET(uip
->ui_flags
, UI_MAPWAITING
);
1928 (void) msleep(&uip
->ui_flags
, &vp
->v_lock
,
1929 PRIBIO
, "ubc_unmap", NULL
);
1931 SET(uip
->ui_flags
, UI_MAPBUSY
);
1933 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
1934 CLR(uip
->ui_flags
, UI_ISMAPPED
);
1940 (void)VNOP_MNOMAP(vp
, vfs_context_current());
1944 vnode_lock_spin(vp
);
1946 CLR(uip
->ui_flags
, UI_MAPBUSY
);
1947 if (ISSET(uip
->ui_flags
, UI_MAPWAITING
)) {
1948 CLR(uip
->ui_flags
, UI_MAPWAITING
);
1954 wakeup(&uip
->ui_flags
);
1958 * the drop of the vnode ref will cleanup
1967 * Manipulate individual page state for a vnode with an associated ubc_info
1968 * with an associated memory object control.
1970 * Parameters: vp The vnode backing the page
1971 * f_offset A file offset interior to the page
1972 * ops The operations to perform, as a bitmap
1973 * (see below for more information)
1974 * phys_entryp The address of a ppnum_t; may be NULL
1976 * flagsp A pointer to an int to contain flags;
1977 * may be NULL to ignore
1979 * Returns: KERN_SUCCESS Success
1980 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1982 * KERN_INVALID_OBJECT If UPL_POP_PHYSICAL and the object is
1983 * not physically contiguous
1984 * KERN_INVALID_OBJECT If !UPL_POP_PHYSICAL and the object is
1985 * physically contiguous
1986 * KERN_FAILURE If the page cannot be looked up
1989 * *phys_entryp (modified) If phys_entryp is non-NULL and
1991 * *flagsp (modified) If flagsp is non-NULL and there was
1992 * !UPL_POP_PHYSICAL and a KERN_SUCCESS
1994 * Notes: For object boundaries, it is considerably more efficient to
1995 * ensure that f_offset is in fact on a page boundary, as this
1996 * will avoid internal use of the hash table to identify the
1997 * page, and would therefore skip a number of early optimizations.
1998 * Since this is a page operation anyway, the caller should try
1999 * to pass only a page aligned offset because of this.
2001 * *flagsp may be modified even if this function fails. If it is
2002 * modified, it will contain the condition of the page before the
2003 * requested operation was attempted; these will only include the
2004 * bitmap flags, and not the PL_POP_PHYSICAL, UPL_POP_DUMP,
2005 * UPL_POP_SET, or UPL_POP_CLR bits.
2007 * The flags field may contain a specific operation, such as
2008 * UPL_POP_PHYSICAL or UPL_POP_DUMP:
2010 * o UPL_POP_PHYSICAL Fail if not contiguous; if
2011 * *phys_entryp and successful, set
2013 * o UPL_POP_DUMP Dump the specified page
2015 * Otherwise, it is treated as a bitmap of one or more page
2016 * operations to perform on the final memory object; allowable
2019 * o UPL_POP_DIRTY The page is dirty
2020 * o UPL_POP_PAGEOUT The page is paged out
2021 * o UPL_POP_PRECIOUS The page is precious
2022 * o UPL_POP_ABSENT The page is absent
2023 * o UPL_POP_BUSY The page is busy
2025 * If the page status is only being queried and not modified, then
2026 * not other bits should be specified. However, if it is being
2027 * modified, exactly ONE of the following bits should be set:
2029 * o UPL_POP_SET Set the current bitmap bits
2030 * o UPL_POP_CLR Clear the current bitmap bits
2032 * Thus to effect a combination of setting an clearing, it may be
2033 * necessary to call this function twice. If this is done, the
2034 * set should be used before the clear, since clearing may trigger
2035 * a wakeup on the destination page, and if the page is backed by
2036 * an encrypted swap file, setting will trigger the decryption
2037 * needed before the wakeup occurs.
2044 ppnum_t
*phys_entryp
,
2047 memory_object_control_t control
;
2049 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2050 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2051 return KERN_INVALID_ARGUMENT
;
2053 return (memory_object_page_op(control
,
2054 (memory_object_offset_t
)f_offset
,
2064 * Manipulate page state for a range of memory for a vnode with an associated
2065 * ubc_info with an associated memory object control, when page level state is
2066 * not required to be returned from the call (i.e. there are no phys_entryp or
2067 * flagsp parameters to this call, and it takes a range which may contain
2068 * multiple pages, rather than an offset interior to a single page).
2070 * Parameters: vp The vnode backing the page
2071 * f_offset_beg A file offset interior to the start page
2072 * f_offset_end A file offset interior to the end page
2073 * ops The operations to perform, as a bitmap
2074 * (see below for more information)
2075 * range The address of an int; may be NULL to
2078 * Returns: KERN_SUCCESS Success
2079 * KERN_INVALID_ARGUMENT If the memory object control has no VM
2081 * KERN_INVALID_OBJECT If the object is physically contiguous
2084 * *range (modified) If range is non-NULL, its contents will
2085 * be modified to contain the number of
2086 * bytes successfully operated upon.
2088 * Notes: IMPORTANT: This function cannot be used on a range that
2089 * consists of physically contiguous pages.
2091 * For object boundaries, it is considerably more efficient to
2092 * ensure that f_offset_beg and f_offset_end are in fact on page
2093 * boundaries, as this will avoid internal use of the hash table
2094 * to identify the page, and would therefore skip a number of
2095 * early optimizations. Since this is an operation on a set of
2096 * pages anyway, the caller should try to pass only a page aligned
2097 * offsets because of this.
2099 * *range will be modified only if this function succeeds.
2101 * The flags field MUST contain a specific operation; allowable
2104 * o UPL_ROP_ABSENT Returns the extent of the range
2105 * presented which is absent, starting
2106 * with the start address presented
2108 * o UPL_ROP_PRESENT Returns the extent of the range
2109 * presented which is present (resident),
2110 * starting with the start address
2112 * o UPL_ROP_DUMP Dump the pages which are found in the
2113 * target object for the target range.
2115 * IMPORTANT: For UPL_ROP_ABSENT and UPL_ROP_PRESENT; if there are
2116 * multiple regions in the range, only the first matching region
2127 memory_object_control_t control
;
2129 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2130 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2131 return KERN_INVALID_ARGUMENT
;
2133 return (memory_object_range_op(control
,
2134 (memory_object_offset_t
)f_offset_beg
,
2135 (memory_object_offset_t
)f_offset_end
,
2144 * Given a vnode, cause the population of a portion of the vm_object; based on
2145 * the nature of the request, the pages returned may contain valid data, or
2146 * they may be uninitialized.
2148 * Parameters: vp The vnode from which to create the upl
2149 * f_offset The start offset into the backing store
2150 * represented by the vnode
2151 * bufsize The size of the upl to create
2152 * uplp Pointer to the upl_t to receive the
2153 * created upl; MUST NOT be NULL
2154 * plp Pointer to receive the internal page
2155 * list for the created upl; MAY be NULL
2158 * Returns: KERN_SUCCESS The requested upl has been created
2159 * KERN_INVALID_ARGUMENT The bufsize argument is not an even
2160 * multiple of the page size
2161 * KERN_INVALID_ARGUMENT There is no ubc_info associated with
2162 * the vnode, or there is no memory object
2163 * control associated with the ubc_info
2164 * memory_object_upl_request:KERN_INVALID_VALUE
2165 * The supplied upl_flags argument is
2169 * *plp (modified) If non-NULL, the value of *plp will be
2170 * modified to point to the internal page
2171 * list; this modification may occur even
2172 * if this function is unsuccessful, in
2173 * which case the contents may be invalid
2175 * Note: If successful, the returned *uplp MUST subsequently be freed
2176 * via a call to ubc_upl_commit(), ubc_upl_commit_range(),
2177 * ubc_upl_abort(), or ubc_upl_abort_range().
2185 upl_page_info_t
**plp
,
2188 memory_object_control_t control
;
2195 if (bufsize
& 0xfff)
2196 return KERN_INVALID_ARGUMENT
;
2198 if (bufsize
> MAX_UPL_SIZE
* PAGE_SIZE
)
2199 return KERN_INVALID_ARGUMENT
;
2201 if (uplflags
& (UPL_UBC_MSYNC
| UPL_UBC_PAGEOUT
| UPL_UBC_PAGEIN
)) {
2203 if (uplflags
& UPL_UBC_MSYNC
) {
2204 uplflags
&= UPL_RET_ONLY_DIRTY
;
2206 uplflags
|= UPL_COPYOUT_FROM
| UPL_CLEAN_IN_PLACE
|
2207 UPL_SET_INTERNAL
| UPL_SET_LITE
;
2209 } else if (uplflags
& UPL_UBC_PAGEOUT
) {
2210 uplflags
&= UPL_RET_ONLY_DIRTY
;
2212 if (uplflags
& UPL_RET_ONLY_DIRTY
)
2213 uplflags
|= UPL_NOBLOCK
;
2215 uplflags
|= UPL_FOR_PAGEOUT
| UPL_CLEAN_IN_PLACE
|
2216 UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
| UPL_SET_LITE
;
2218 uplflags
|= UPL_RET_ONLY_ABSENT
|
2219 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
2220 UPL_SET_INTERNAL
| UPL_SET_LITE
;
2223 * if the requested size == PAGE_SIZE, we don't want to set
2224 * the UPL_NOBLOCK since we may be trying to recover from a
2225 * previous partial pagein I/O that occurred because we were low
2226 * on memory and bailed early in order to honor the UPL_NOBLOCK...
2227 * since we're only asking for a single page, we can block w/o fear
2228 * of tying up pages while waiting for more to become available
2230 if (bufsize
> PAGE_SIZE
)
2231 uplflags
|= UPL_NOBLOCK
;
2234 uplflags
&= ~UPL_FOR_PAGEOUT
;
2236 if (uplflags
& UPL_WILL_BE_DUMPED
) {
2237 uplflags
&= ~UPL_WILL_BE_DUMPED
;
2238 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
2240 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
2242 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2243 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2244 return KERN_INVALID_ARGUMENT
;
2246 kr
= memory_object_upl_request(control
, f_offset
, bufsize
, uplp
, NULL
, NULL
, uplflags
);
2247 if (kr
== KERN_SUCCESS
&& plp
!= NULL
)
2248 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
2254 * ubc_upl_maxbufsize
2256 * Return the maximum bufsize ubc_create_upl( ) will take.
2260 * Returns: maximum size buffer (in bytes) ubc_create_upl( ) will take.
2266 return(MAX_UPL_SIZE
* PAGE_SIZE
);
2272 * Map the page list assocated with the supplied upl into the kernel virtual
2273 * address space at the virtual address indicated by the dst_addr argument;
2274 * the entire upl is mapped
2276 * Parameters: upl The upl to map
2277 * dst_addr The address at which to map the upl
2279 * Returns: KERN_SUCCESS The upl has been mapped
2280 * KERN_INVALID_ARGUMENT The upl is UPL_NULL
2281 * KERN_FAILURE The upl is already mapped
2282 * vm_map_enter:KERN_INVALID_ARGUMENT
2283 * A failure code from vm_map_enter() due
2284 * to an invalid argument
2289 vm_offset_t
*dst_addr
)
2291 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
2298 * Unmap the page list assocated with the supplied upl from the kernel virtual
2299 * address space; the entire upl is unmapped.
2301 * Parameters: upl The upl to unmap
2303 * Returns: KERN_SUCCESS The upl has been unmapped
2304 * KERN_FAILURE The upl is not currently mapped
2305 * KERN_INVALID_ARGUMENT If the upl is UPL_NULL
2311 return(vm_upl_unmap(kernel_map
, upl
));
2318 * Commit the contents of the upl to the backing store
2320 * Parameters: upl The upl to commit
2322 * Returns: KERN_SUCCESS The upl has been committed
2323 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2324 * KERN_FAILURE The supplied upl does not represent
2325 * device memory, and the offset plus the
2326 * size would exceed the actual size of
2329 * Notes: In practice, the only return value for this function should be
2330 * KERN_SUCCESS, unless there has been data structure corruption;
2331 * since the upl is deallocated regardless of success or failure,
2332 * there's really nothing to do about this other than panic.
2334 * IMPORTANT: Use of this function should not be mixed with use of
2335 * ubc_upl_commit_range(), due to the unconditional deallocation
2342 upl_page_info_t
*pl
;
2345 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2346 kr
= upl_commit(upl
, pl
, MAX_UPL_SIZE
);
2347 upl_deallocate(upl
);
2355 * Commit the contents of the specified range of the upl to the backing store
2357 * Parameters: upl The upl to commit
2358 * offset The offset into the upl
2359 * size The size of the region to be committed,
2360 * starting at the specified offset
2361 * flags commit type (see below)
2363 * Returns: KERN_SUCCESS The range has been committed
2364 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2365 * KERN_FAILURE The supplied upl does not represent
2366 * device memory, and the offset plus the
2367 * size would exceed the actual size of
2370 * Notes: IMPORTANT: If the commit is successful, and the object is now
2371 * empty, the upl will be deallocated. Since the caller cannot
2372 * check that this is the case, the UPL_COMMIT_FREE_ON_EMPTY flag
2373 * should generally only be used when the offset is 0 and the size
2374 * is equal to the upl size.
2376 * The flags argument is a bitmap of flags on the rage of pages in
2377 * the upl to be committed; allowable flags are:
2379 * o UPL_COMMIT_FREE_ON_EMPTY Free the upl when it is
2380 * both empty and has been
2381 * successfully committed
2382 * o UPL_COMMIT_CLEAR_DIRTY Clear each pages dirty
2383 * bit; will prevent a
2385 * o UPL_COMMIT_SET_DIRTY Set each pages dirty
2386 * bit; will cause a later
2388 * o UPL_COMMIT_INACTIVATE Clear each pages
2389 * reference bit; the page
2390 * will not be accessed
2391 * o UPL_COMMIT_ALLOW_ACCESS Unbusy each page; pages
2392 * become busy when an
2393 * IOMemoryDescriptor is
2394 * mapped or redirected,
2395 * and we have to wait for
2398 * The flag UPL_COMMIT_NOTIFY_EMPTY is used internally, and should
2399 * not be specified by the caller.
2401 * The UPL_COMMIT_CLEAR_DIRTY and UPL_COMMIT_SET_DIRTY flags are
2402 * mutually exclusive, and should not be combined.
2405 ubc_upl_commit_range(
2407 upl_offset_t offset
,
2411 upl_page_info_t
*pl
;
2415 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
2416 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
2418 if (flags
& UPL_COMMIT_KERNEL_ONLY_FLAGS
) {
2419 return KERN_INVALID_ARGUMENT
;
2422 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2424 kr
= upl_commit_range(upl
, offset
, size
, flags
,
2425 pl
, MAX_UPL_SIZE
, &empty
);
2427 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
2428 upl_deallocate(upl
);
2435 * ubc_upl_abort_range
2437 * Abort the contents of the specified range of the specified upl
2439 * Parameters: upl The upl to abort
2440 * offset The offset into the upl
2441 * size The size of the region to be aborted,
2442 * starting at the specified offset
2443 * abort_flags abort type (see below)
2445 * Returns: KERN_SUCCESS The range has been aborted
2446 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2447 * KERN_FAILURE The supplied upl does not represent
2448 * device memory, and the offset plus the
2449 * size would exceed the actual size of
2452 * Notes: IMPORTANT: If the abort is successful, and the object is now
2453 * empty, the upl will be deallocated. Since the caller cannot
2454 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2455 * should generally only be used when the offset is 0 and the size
2456 * is equal to the upl size.
2458 * The abort_flags argument is a bitmap of flags on the range of
2459 * pages in the upl to be aborted; allowable flags are:
2461 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2462 * empty and has been successfully
2464 * o UPL_ABORT_RESTART The operation must be restarted
2465 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2466 * o UPL_ABORT_ERROR An I/O error occurred
2467 * o UPL_ABORT_DUMP_PAGES Just free the pages
2468 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2469 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2471 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2472 * not be specified by the caller. It is intended to fulfill the
2473 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2474 * ubc_upl_commit_range(), but is never referenced internally.
2476 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2477 * referenced; do not use it.
2480 ubc_upl_abort_range(
2482 upl_offset_t offset
,
2487 boolean_t empty
= FALSE
;
2489 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
2490 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
2492 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
2494 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
2495 upl_deallocate(upl
);
2504 * Abort the contents of the specified upl
2506 * Parameters: upl The upl to abort
2507 * abort_type abort type (see below)
2509 * Returns: KERN_SUCCESS The range has been aborted
2510 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2511 * KERN_FAILURE The supplied upl does not represent
2512 * device memory, and the offset plus the
2513 * size would exceed the actual size of
2516 * Notes: IMPORTANT: If the abort is successful, and the object is now
2517 * empty, the upl will be deallocated. Since the caller cannot
2518 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2519 * should generally only be used when the offset is 0 and the size
2520 * is equal to the upl size.
2522 * The abort_type is a bitmap of flags on the range of
2523 * pages in the upl to be aborted; allowable flags are:
2525 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2526 * empty and has been successfully
2528 * o UPL_ABORT_RESTART The operation must be restarted
2529 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2530 * o UPL_ABORT_ERROR An I/O error occurred
2531 * o UPL_ABORT_DUMP_PAGES Just free the pages
2532 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2533 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2535 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2536 * not be specified by the caller. It is intended to fulfill the
2537 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2538 * ubc_upl_commit_range(), but is never referenced internally.
2540 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2541 * referenced; do not use it.
2550 kr
= upl_abort(upl
, abort_type
);
2551 upl_deallocate(upl
);
2559 * Retrieve the internal page list for the specified upl
2561 * Parameters: upl The upl to obtain the page list from
2563 * Returns: !NULL The (upl_page_info_t *) for the page
2564 * list internal to the upl
2565 * NULL Error/no page list associated
2567 * Notes: IMPORTANT: The function is only valid on internal objects
2568 * where the list request was made with the UPL_INTERNAL flag.
2570 * This function is a utility helper function, since some callers
2571 * may not have direct access to the header defining the macro,
2572 * due to abstraction layering constraints.
2578 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
2583 UBCINFOEXISTS(struct vnode
* vp
)
2585 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
2590 ubc_upl_range_needed(
2595 upl_range_needed(upl
, index
, count
);
2602 #define CS_BLOB_PAGEABLE 0
2603 static volatile SInt32 cs_blob_size
= 0;
2604 static volatile SInt32 cs_blob_count
= 0;
2605 static SInt32 cs_blob_size_peak
= 0;
2606 static UInt32 cs_blob_size_max
= 0;
2607 static SInt32 cs_blob_count_peak
= 0;
2609 int cs_validation
= 1;
2611 #ifndef SECURE_KERNEL
2612 SYSCTL_INT(_vm
, OID_AUTO
, cs_validation
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_validation
, 0, "Do validate code signatures");
2614 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");
2615 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");
2616 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_count_peak
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_blob_count_peak
, 0, "Peak number of code signature blobs");
2617 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_peak
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_blob_size_peak
, 0, "Peak size of code signature blobs");
2618 SYSCTL_INT(_vm
, OID_AUTO
, cs_blob_size_max
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_blob_size_max
, 0, "Size of biggest code signature blob");
2622 ubc_cs_blob_allocate(
2623 vm_offset_t
*blob_addr_p
,
2624 vm_size_t
*blob_size_p
)
2628 #if CS_BLOB_PAGEABLE
2629 *blob_size_p
= round_page(*blob_size_p
);
2630 kr
= kmem_alloc(kernel_map
, blob_addr_p
, *blob_size_p
);
2631 #else /* CS_BLOB_PAGEABLE */
2632 *blob_addr_p
= (vm_offset_t
) kalloc(*blob_size_p
);
2633 if (*blob_addr_p
== 0) {
2638 #endif /* CS_BLOB_PAGEABLE */
2643 ubc_cs_blob_deallocate(
2644 vm_offset_t blob_addr
,
2645 vm_size_t blob_size
)
2647 #if CS_BLOB_PAGEABLE
2648 kmem_free(kernel_map
, blob_addr
, blob_size
);
2649 #else /* CS_BLOB_PAGEABLE */
2650 kfree((void *) blob_addr
, blob_size
);
2651 #endif /* CS_BLOB_PAGEABLE */
2657 vm_address_t address
,
2661 struct ubc_info
*uip
;
2662 struct cs_blob
*blob
;
2663 memory_object_control_t control
;
2664 const CS_CodeDirectory
*cd
;
2667 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
2668 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
2669 return KERN_INVALID_ARGUMENT
;
2671 if (memory_object_is_signed(control
))
2674 blob
= (struct cs_blob
*) kalloc(sizeof (struct cs_blob
));
2678 /* fill in the new blob */
2679 blob
->csb_cpu_type
= CPU_TYPE_ANY
;
2680 blob
->csb_base_offset
= 0;
2681 blob
->csb_mem_size
= size
;
2682 blob
->csb_mem_offset
= 0;
2683 blob
->csb_mem_handle
= IPC_PORT_NULL
;
2684 blob
->csb_mem_kaddr
= address
;
2685 blob
->csb_sigpup
= 1;
2688 * Validate the blob's contents
2690 cd
= findCodeDirectory(
2691 (const CS_SuperBlob
*) address
,
2693 (char *) address
+ blob
->csb_mem_size
);
2695 /* no code directory => useless blob ! */
2700 blob
->csb_flags
= ntohl(cd
->flags
) | CS_VALID
;
2701 blob
->csb_end_offset
= round_page(ntohl(cd
->codeLimit
));
2702 if((ntohl(cd
->version
) >= CS_SUPPORTSSCATTER
) && (ntohl(cd
->scatterOffset
))) {
2703 const SC_Scatter
*scatter
= (const SC_Scatter
*)
2704 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
2705 blob
->csb_start_offset
= ntohl(scatter
->base
) * PAGE_SIZE
;
2707 blob
->csb_start_offset
= (blob
->csb_end_offset
- (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
));
2711 * We don't need to check with the policy module, since the input data is supposed to be already checked
2715 if (! UBCINFOEXISTS(vp
)) {
2718 printf("out ubc object\n");
2722 uip
= vp
->v_ubcinfo
;
2724 /* someone raced us to adding the code directory */
2725 if (uip
->cs_blobs
!= NULL
) {
2727 printf("sigpup: vnode already have CD ?\n");
2733 blob
->csb_next
= uip
->cs_blobs
;
2734 uip
->cs_blobs
= blob
;
2736 OSAddAtomic(+1, &cs_blob_count
);
2737 OSAddAtomic((SInt32
) +blob
->csb_mem_size
, &cs_blob_size
);
2739 /* mark this vnode's VM object as having "signed pages" */
2740 kr
= memory_object_signed(uip
->ui_control
, TRUE
);
2741 if (kr
!= KERN_SUCCESS
) {
2744 printf("sigpup: not signable ?\n");
2755 printf("sigpup: not signable ?\n");
2756 /* we failed; release what we allocated */
2758 kfree(blob
, sizeof (*blob
));
2776 struct ubc_info
*uip
;
2777 struct cs_blob
*blob
, *oblob
;
2779 ipc_port_t blob_handle
;
2780 memory_object_size_t blob_size
;
2781 const CS_CodeDirectory
*cd
;
2782 off_t blob_start_offset
, blob_end_offset
;
2784 boolean_t record_mtime
;
2786 record_mtime
= FALSE
;
2788 blob_handle
= IPC_PORT_NULL
;
2790 blob
= (struct cs_blob
*) kalloc(sizeof (struct cs_blob
));
2795 #if CS_BLOB_PAGEABLE
2796 /* get a memory entry on the blob */
2797 blob_size
= (memory_object_size_t
) size
;
2798 kr
= mach_make_memory_entry_64(kernel_map
,
2804 if (kr
!= KERN_SUCCESS
) {
2808 if (memory_object_round_page(blob_size
) !=
2809 (memory_object_size_t
) round_page(size
)) {
2810 printf("ubc_cs_blob_add: size mismatch 0x%llx 0x%lx !?\n",
2811 blob_size
, (size_t)size
);
2812 panic("XXX FBDP size mismatch 0x%llx 0x%lx\n", blob_size
, (size_t)size
);
2817 blob_size
= (memory_object_size_t
) size
;
2818 blob_handle
= IPC_PORT_NULL
;
2821 /* fill in the new blob */
2822 blob
->csb_cpu_type
= cputype
;
2823 blob
->csb_sigpup
= 0;
2824 blob
->csb_base_offset
= base_offset
;
2825 blob
->csb_blob_offset
= blob_offset
;
2826 blob
->csb_mem_size
= size
;
2827 blob
->csb_mem_offset
= 0;
2828 blob
->csb_mem_handle
= blob_handle
;
2829 blob
->csb_mem_kaddr
= addr
;
2830 blob
->csb_flags
= 0;
2833 * Validate the blob's contents
2836 error
= cs_validate_csblob((const uint8_t *)addr
, size
, &cd
);
2839 printf("CODESIGNING: csblob invalid: %d\n", error
);
2840 blob
->csb_flags
= 0;
2841 blob
->csb_start_offset
= 0;
2842 blob
->csb_end_offset
= 0;
2843 memset(blob
->csb_sha1
, 0, SHA1_RESULTLEN
);
2844 /* let the vnode checker determine if the signature is valid or not */
2846 const unsigned char *sha1_base
;
2849 blob
->csb_flags
= (ntohl(cd
->flags
) & CS_ALLOWED_MACHO
) | CS_VALID
;
2850 blob
->csb_end_offset
= round_page(ntohl(cd
->codeLimit
));
2851 if((ntohl(cd
->version
) >= CS_SUPPORTSSCATTER
) && (ntohl(cd
->scatterOffset
))) {
2852 const SC_Scatter
*scatter
= (const SC_Scatter
*)
2853 ((const char*)cd
+ ntohl(cd
->scatterOffset
));
2854 blob
->csb_start_offset
= ntohl(scatter
->base
) * PAGE_SIZE
;
2856 blob
->csb_start_offset
= (blob
->csb_end_offset
-
2857 (ntohl(cd
->nCodeSlots
) * PAGE_SIZE
));
2859 /* compute the blob's SHA1 hash */
2860 sha1_base
= (const unsigned char *) cd
;
2861 sha1_size
= ntohl(cd
->length
);
2862 SHA1Init(&sha1ctxt
);
2863 SHA1Update(&sha1ctxt
, sha1_base
, sha1_size
);
2864 SHA1Final(blob
->csb_sha1
, &sha1ctxt
);
2868 * Let policy module check whether the blob's signature is accepted.
2871 error
= mac_vnode_check_signature(vp
, base_offset
, blob
->csb_sha1
, (void*)addr
, size
);
2877 * Validate the blob's coverage
2879 blob_start_offset
= blob
->csb_base_offset
+ blob
->csb_start_offset
;
2880 blob_end_offset
= blob
->csb_base_offset
+ blob
->csb_end_offset
;
2882 if (blob_start_offset
>= blob_end_offset
||
2883 blob_start_offset
< 0 ||
2884 blob_end_offset
<= 0) {
2885 /* reject empty or backwards blob */
2891 if (! UBCINFOEXISTS(vp
)) {
2896 uip
= vp
->v_ubcinfo
;
2898 /* check if this new blob overlaps with an existing blob */
2899 for (oblob
= uip
->cs_blobs
;
2901 oblob
= oblob
->csb_next
) {
2902 off_t oblob_start_offset
, oblob_end_offset
;
2904 oblob_start_offset
= (oblob
->csb_base_offset
+
2905 oblob
->csb_start_offset
);
2906 oblob_end_offset
= (oblob
->csb_base_offset
+
2907 oblob
->csb_end_offset
);
2908 if (blob_start_offset
>= oblob_end_offset
||
2909 blob_end_offset
<= oblob_start_offset
) {
2910 /* no conflict with this existing blob */
2913 if (blob_start_offset
== oblob_start_offset
&&
2914 blob_end_offset
== oblob_end_offset
&&
2915 blob
->csb_mem_size
== oblob
->csb_mem_size
&&
2916 blob
->csb_flags
== oblob
->csb_flags
&&
2917 (blob
->csb_cpu_type
== CPU_TYPE_ANY
||
2918 oblob
->csb_cpu_type
== CPU_TYPE_ANY
||
2919 blob
->csb_cpu_type
== oblob
->csb_cpu_type
) &&
2920 !bcmp(blob
->csb_sha1
,
2924 * We already have this blob:
2925 * we'll return success but
2926 * throw away the new blob.
2928 if (oblob
->csb_cpu_type
== CPU_TYPE_ANY
) {
2930 * The old blob matches this one
2931 * but doesn't have any CPU type.
2932 * Update it with whatever the caller
2933 * provided this time.
2935 oblob
->csb_cpu_type
= cputype
;
2938 * If the same blob moved around in the Mach-O, we
2939 * want to remember the new blob offset to avoid
2940 * coming back here again and again.
2942 oblob
->csb_blob_offset
= blob_offset
;
2948 /* different blob: reject the new one */
2949 char pathbuf
[MAXPATHLEN
];
2950 char new_sha1_str
[2*SHA1_RESULTLEN
+1];
2951 char old_sha1_str
[2*SHA1_RESULTLEN
+1];
2953 const char *pathp
= "?unknown";
2954 int pblen
= sizeof(pathbuf
);
2955 if (vn_getpath(vp
, pathbuf
, &pblen
) == 0) {
2956 /* pblen == strlen(pathbuf) + 1. Assume strlen(pathbuf) > 0 */
2957 for (pathp
= pathbuf
+ pblen
- 2; pathp
> pathbuf
&& pathp
[-1] != '/'; pathp
--) ;
2959 snprintf(arch_str
, sizeof(arch_str
), "%x", cputype
);
2960 hex_str(oblob
->csb_sha1
, SHA1_RESULTLEN
, old_sha1_str
);
2961 hex_str(blob
->csb_sha1
, SHA1_RESULTLEN
, new_sha1_str
);
2962 kern_asl_msg(LOG_NOTICE
, "messagetracer",
2964 "com.apple.message.domain", "com.apple.kernel.cs.replace",
2965 "com.apple.message.signature", pathp
,
2966 "com.apple.message.signature2", arch_str
,
2967 "com.apple.message.signature3", old_sha1_str
,
2968 "com.apple.message.result", new_sha1_str
,
2969 "com.apple.message.summarize", "YES",
2972 printf("CODESIGNING: rejected new signature for architecture %d of file %s\n",
2982 /* mark this vnode's VM object as having "signed pages" */
2983 kr
= memory_object_signed(uip
->ui_control
, TRUE
);
2984 if (kr
!= KERN_SUCCESS
) {
2990 if (uip
->cs_blobs
== NULL
) {
2991 /* loading 1st blob: record the file's current "modify time" */
2992 record_mtime
= TRUE
;
2996 * Add this blob to the list of blobs for this vnode.
2997 * We always add at the front of the list and we never remove a
2998 * blob from the list, so ubc_cs_get_blobs() can return whatever
2999 * the top of the list was and that list will remain valid
3000 * while we validate a page, even after we release the vnode's lock.
3002 blob
->csb_next
= uip
->cs_blobs
;
3003 uip
->cs_blobs
= blob
;
3005 OSAddAtomic(+1, &cs_blob_count
);
3006 if (cs_blob_count
> cs_blob_count_peak
) {
3007 cs_blob_count_peak
= cs_blob_count
; /* XXX atomic ? */
3009 OSAddAtomic((SInt32
) +blob
->csb_mem_size
, &cs_blob_size
);
3010 if ((SInt32
) cs_blob_size
> cs_blob_size_peak
) {
3011 cs_blob_size_peak
= (SInt32
) cs_blob_size
; /* XXX atomic ? */
3013 if ((UInt32
) blob
->csb_mem_size
> cs_blob_size_max
) {
3014 cs_blob_size_max
= (UInt32
) blob
->csb_mem_size
;
3019 const char *name
= vnode_getname_printable(vp
);
3021 printf("CODE SIGNING: proc %d(%s) "
3022 "loaded %s signatures for file (%s) "
3023 "range 0x%llx:0x%llx flags 0x%x\n",
3024 p
->p_pid
, p
->p_comm
,
3025 blob
->csb_cpu_type
== -1 ? "detached" : "embedded",
3027 blob
->csb_base_offset
+ blob
->csb_start_offset
,
3028 blob
->csb_base_offset
+ blob
->csb_end_offset
,
3030 vnode_putname_printable(name
);
3036 vnode_mtime(vp
, &uip
->cs_mtime
, vfs_context_current());
3039 error
= 0; /* success ! */
3043 /* we failed; release what we allocated */
3045 kfree(blob
, sizeof (*blob
));
3048 if (blob_handle
!= IPC_PORT_NULL
) {
3049 mach_memory_entry_port_release(blob_handle
);
3050 blob_handle
= IPC_PORT_NULL
;
3054 if (error
== EAGAIN
) {
3056 * See above: error is EAGAIN if we were asked
3057 * to add an existing blob again. We cleaned the new
3058 * blob and we want to return success.
3062 * Since we're not failing, consume the data we received.
3064 ubc_cs_blob_deallocate(addr
, size
);
3077 struct ubc_info
*uip
;
3078 struct cs_blob
*blob
;
3079 off_t offset_in_blob
;
3081 vnode_lock_spin(vp
);
3083 if (! UBCINFOEXISTS(vp
)) {
3088 uip
= vp
->v_ubcinfo
;
3089 for (blob
= uip
->cs_blobs
;
3091 blob
= blob
->csb_next
) {
3092 if (cputype
!= -1 && blob
->csb_cpu_type
== cputype
) {
3096 offset_in_blob
= offset
- blob
->csb_base_offset
;
3097 if (offset_in_blob
>= blob
->csb_start_offset
&&
3098 offset_in_blob
< blob
->csb_end_offset
) {
3099 /* our offset is covered by this blob */
3105 if (cs_debug
&& blob
!= NULL
&& blob
->csb_sigpup
)
3106 printf("found sig pup blob\n");
3115 struct ubc_info
*uip
)
3117 struct cs_blob
*blob
, *next_blob
;
3119 for (blob
= uip
->cs_blobs
;
3122 next_blob
= blob
->csb_next
;
3123 if (blob
->csb_mem_kaddr
!= 0 && !blob
->csb_sigpup
) {
3124 ubc_cs_blob_deallocate(blob
->csb_mem_kaddr
,
3125 blob
->csb_mem_size
);
3126 blob
->csb_mem_kaddr
= 0;
3128 if (blob
->csb_mem_handle
!= IPC_PORT_NULL
) {
3129 mach_memory_entry_port_release(blob
->csb_mem_handle
);
3131 blob
->csb_mem_handle
= IPC_PORT_NULL
;
3132 OSAddAtomic(-1, &cs_blob_count
);
3133 OSAddAtomic((SInt32
) -blob
->csb_mem_size
, &cs_blob_size
);
3134 kfree(blob
, sizeof (*blob
));
3136 #if CHECK_CS_VALIDATION_BITMAP
3137 ubc_cs_validation_bitmap_deallocate( uip
->ui_vnode
);
3139 uip
->cs_blobs
= NULL
;
3146 struct ubc_info
*uip
;
3147 struct cs_blob
*blobs
;
3150 * No need to take the vnode lock here. The caller must be holding
3151 * a reference on the vnode (via a VM mapping or open file descriptor),
3152 * so the vnode will not go away. The ubc_info stays until the vnode
3153 * goes away. And we only modify "blobs" by adding to the head of the
3155 * The ubc_info could go away entirely if the vnode gets reclaimed as
3156 * part of a forced unmount. In the case of a code-signature validation
3157 * during a page fault, the "paging_in_progress" reference on the VM
3158 * object guarantess that the vnode pager (and the ubc_info) won't go
3159 * away during the fault.
3160 * Other callers need to protect against vnode reclaim by holding the
3161 * vnode lock, for example.
3164 if (! UBCINFOEXISTS(vp
)) {
3169 uip
= vp
->v_ubcinfo
;
3170 blobs
= uip
->cs_blobs
;
3179 struct timespec
*cs_mtime
)
3181 struct ubc_info
*uip
;
3183 if (! UBCINFOEXISTS(vp
)) {
3184 cs_mtime
->tv_sec
= 0;
3185 cs_mtime
->tv_nsec
= 0;
3189 uip
= vp
->v_ubcinfo
;
3190 cs_mtime
->tv_sec
= uip
->cs_mtime
.tv_sec
;
3191 cs_mtime
->tv_nsec
= uip
->cs_mtime
.tv_nsec
;
3194 unsigned long cs_validate_page_no_hash
= 0;
3195 unsigned long cs_validate_page_bad_hash
= 0;
3199 memory_object_t pager
,
3200 memory_object_offset_t page_offset
,
3205 unsigned char actual_hash
[SHA1_RESULTLEN
];
3206 unsigned char expected_hash
[SHA1_RESULTLEN
];
3207 boolean_t found_hash
;
3208 struct cs_blob
*blobs
, *blob
;
3209 const CS_CodeDirectory
*cd
;
3210 const CS_SuperBlob
*embedded
;
3211 const unsigned char *hash
;
3212 boolean_t validated
;
3213 off_t offset
; /* page offset in the file */
3215 off_t codeLimit
= 0;
3216 char *lower_bound
, *upper_bound
;
3217 vm_offset_t kaddr
, blob_addr
;
3221 offset
= page_offset
;
3223 /* retrieve the expected hash */
3225 blobs
= (struct cs_blob
*) _blobs
;
3229 blob
= blob
->csb_next
) {
3230 offset
= page_offset
- blob
->csb_base_offset
;
3231 if (offset
< blob
->csb_start_offset
||
3232 offset
>= blob
->csb_end_offset
) {
3233 /* our page is not covered by this blob */
3237 /* map the blob in the kernel address space */
3238 kaddr
= blob
->csb_mem_kaddr
;
3240 ksize
= (vm_size_t
) (blob
->csb_mem_size
+
3241 blob
->csb_mem_offset
);
3242 kr
= vm_map(kernel_map
,
3247 blob
->csb_mem_handle
,
3253 if (kr
!= KERN_SUCCESS
) {
3254 /* XXX FBDP what to do !? */
3255 printf("cs_validate_page: failed to map blob, "
3256 "size=0x%lx kr=0x%x\n",
3257 (size_t)blob
->csb_mem_size
, kr
);
3261 if (blob
->csb_sigpup
&& cs_debug
)
3262 printf("checking for a sigpup CD\n");
3264 blob_addr
= kaddr
+ blob
->csb_mem_offset
;
3266 lower_bound
= CAST_DOWN(char *, blob_addr
);
3267 upper_bound
= lower_bound
+ blob
->csb_mem_size
;
3269 embedded
= (const CS_SuperBlob
*) blob_addr
;
3270 cd
= findCodeDirectory(embedded
, lower_bound
, upper_bound
);
3272 if (cd
->pageSize
!= PAGE_SHIFT
||
3273 cd
->hashType
!= CS_HASHTYPE_SHA1
||
3274 cd
->hashSize
!= SHA1_RESULTLEN
) {
3276 if (blob
->csb_sigpup
&& cs_debug
)
3277 printf("page foo bogus sigpup CD\n");
3281 offset
= page_offset
- blob
->csb_base_offset
;
3282 if (offset
< blob
->csb_start_offset
||
3283 offset
>= blob
->csb_end_offset
) {
3284 /* our page is not covered by this blob */
3285 if (blob
->csb_sigpup
&& cs_debug
)
3286 printf("OOB sigpup CD\n");
3290 codeLimit
= ntohl(cd
->codeLimit
);
3291 if (blob
->csb_sigpup
&& cs_debug
)
3292 printf("sigpup codesize %d\n", (int)codeLimit
);
3294 hash
= hashes(cd
, (unsigned)atop(offset
),
3295 lower_bound
, upper_bound
);
3297 bcopy(hash
, expected_hash
,
3298 sizeof (expected_hash
));
3300 if (blob
->csb_sigpup
&& cs_debug
)
3301 printf("sigpup hash\n");
3306 if (blob
->csb_sigpup
&& cs_debug
)
3307 printf("sig pup had no valid CD\n");
3312 if (found_hash
== FALSE
) {
3314 * We can't verify this page because there is no signature
3315 * for it (yet). It's possible that this part of the object
3316 * is not signed, or that signatures for that part have not
3318 * Report that the page has not been validated and let the
3319 * caller decide if it wants to accept it or not.
3321 cs_validate_page_no_hash
++;
3323 printf("CODE SIGNING: cs_validate_page: "
3324 "mobj %p off 0x%llx: no hash to validate !?\n",
3325 pager
, page_offset
);
3332 if ((off_t
)(offset
+ size
) > codeLimit
) {
3333 /* partial page at end of segment */
3334 assert(offset
< codeLimit
);
3335 size
= (size_t) (codeLimit
& PAGE_MASK
);
3337 /* compute the actual page's SHA1 hash */
3338 SHA1Init(&sha1ctxt
);
3339 SHA1UpdateUsePhysicalAddress(&sha1ctxt
, data
, size
);
3340 SHA1Final(actual_hash
, &sha1ctxt
);
3342 if (bcmp(expected_hash
, actual_hash
, SHA1_RESULTLEN
) != 0) {
3343 char asha1_str
[2*SHA1_RESULTLEN
+1];
3344 char esha1_str
[2*SHA1_RESULTLEN
+1];
3345 hex_str(actual_hash
, SHA1_RESULTLEN
, asha1_str
);
3346 hex_str(expected_hash
, SHA1_RESULTLEN
, esha1_str
);
3348 printf("CODE SIGNING: cs_validate_page: "
3349 "mobj %p off 0x%llx size 0x%lx: actual %s expected %s\n",
3350 pager
, page_offset
, size
, asha1_str
, esha1_str
);
3352 cs_validate_page_bad_hash
++;
3354 char page_offset_str
[20];
3355 snprintf(page_offset_str
, sizeof(page_offset_str
), "%llx", page_offset
);
3356 kern_asl_msg(LOG_NOTICE
, "messagetracer",
3358 "com.apple.message.domain", "com.apple.kernel.cs.mismatch",
3359 "com.apple.message.signature", page_offset_str
,
3360 "com.apple.message.signature2", asha1_str
,
3361 "com.apple.message.signature3", esha1_str
,
3362 "com.apple.message.summarize", "YES",
3368 if (cs_debug
> 10) {
3369 printf("CODE SIGNING: cs_validate_page: "
3370 "mobj %p off 0x%llx size 0x%lx: "
3372 pager
, page_offset
, size
);
3386 unsigned char *cdhash
)
3388 struct cs_blob
*blobs
, *blob
;
3394 blobs
= ubc_get_cs_blobs(vp
);
3397 blob
= blob
->csb_next
) {
3398 /* compute offset relative to this blob */
3399 rel_offset
= offset
- blob
->csb_base_offset
;
3400 if (rel_offset
>= blob
->csb_start_offset
&&
3401 rel_offset
< blob
->csb_end_offset
) {
3402 /* this blob does cover our "offset" ! */
3408 /* we didn't find a blob covering "offset" */
3409 ret
= EBADEXEC
; /* XXX any better error ? */
3411 /* get the SHA1 hash of that blob */
3412 bcopy(blob
->csb_sha1
, cdhash
, sizeof (blob
->csb_sha1
));
3421 #if CHECK_CS_VALIDATION_BITMAP
3422 #define stob(s) ((atop_64((s)) + 07) >> 3)
3423 extern boolean_t root_fs_upgrade_try
;
3426 * Should we use the code-sign bitmap to avoid repeated code-sign validation?
3428 * a) Is the target vnode on the root filesystem?
3429 * b) Has someone tried to mount the root filesystem read-write?
3430 * If answers are (a) yes AND (b) no, then we can use the bitmap.
3432 #define USE_CODE_SIGN_BITMAP(vp) ( (vp != NULL) && (vp->v_mount != NULL) && (vp->v_mount->mnt_flag & MNT_ROOTFS) && !root_fs_upgrade_try)
3434 ubc_cs_validation_bitmap_allocate(
3437 kern_return_t kr
= KERN_SUCCESS
;
3438 struct ubc_info
*uip
;
3439 char *target_bitmap
;
3440 vm_object_size_t bitmap_size
;
3442 if ( ! USE_CODE_SIGN_BITMAP(vp
) || (! UBCINFOEXISTS(vp
))) {
3443 kr
= KERN_INVALID_ARGUMENT
;
3445 uip
= vp
->v_ubcinfo
;
3447 if ( uip
->cs_valid_bitmap
== NULL
) {
3448 bitmap_size
= stob(uip
->ui_size
);
3449 target_bitmap
= (char*) kalloc( (vm_size_t
)bitmap_size
);
3450 if (target_bitmap
== 0) {
3455 if( kr
== KERN_SUCCESS
) {
3456 memset( target_bitmap
, 0, (size_t)bitmap_size
);
3457 uip
->cs_valid_bitmap
= (void*)target_bitmap
;
3458 uip
->cs_valid_bitmap_size
= bitmap_size
;
3466 ubc_cs_check_validation_bitmap (
3468 memory_object_offset_t offset
,
3471 kern_return_t kr
= KERN_SUCCESS
;
3473 if ( ! USE_CODE_SIGN_BITMAP(vp
) || ! UBCINFOEXISTS(vp
)) {
3474 kr
= KERN_INVALID_ARGUMENT
;
3476 struct ubc_info
*uip
= vp
->v_ubcinfo
;
3477 char *target_bitmap
= uip
->cs_valid_bitmap
;
3479 if ( target_bitmap
== NULL
) {
3480 kr
= KERN_INVALID_ARGUMENT
;
3483 bit
= atop_64( offset
);
3486 if ( byte
> uip
->cs_valid_bitmap_size
) {
3487 kr
= KERN_INVALID_ARGUMENT
;
3490 if (optype
== CS_BITMAP_SET
) {
3491 target_bitmap
[byte
] |= (1 << (bit
& 07));
3493 } else if (optype
== CS_BITMAP_CLEAR
) {
3494 target_bitmap
[byte
] &= ~(1 << (bit
& 07));
3496 } else if (optype
== CS_BITMAP_CHECK
) {
3497 if ( target_bitmap
[byte
] & (1 << (bit
& 07))) {
3510 ubc_cs_validation_bitmap_deallocate(
3513 struct ubc_info
*uip
;
3514 void *target_bitmap
;
3515 vm_object_size_t bitmap_size
;
3517 if ( UBCINFOEXISTS(vp
)) {
3518 uip
= vp
->v_ubcinfo
;
3520 if ( (target_bitmap
= uip
->cs_valid_bitmap
) != NULL
) {
3521 bitmap_size
= uip
->cs_valid_bitmap_size
;
3522 kfree( target_bitmap
, (vm_size_t
) bitmap_size
);
3523 uip
->cs_valid_bitmap
= NULL
;
3528 kern_return_t
ubc_cs_validation_bitmap_allocate(__unused vnode_t vp
){
3529 return KERN_INVALID_ARGUMENT
;
3532 kern_return_t
ubc_cs_check_validation_bitmap(
3533 __unused
struct vnode
*vp
,
3534 __unused memory_object_offset_t offset
,
3535 __unused
int optype
){
3537 return KERN_INVALID_ARGUMENT
;
3540 void ubc_cs_validation_bitmap_deallocate(__unused vnode_t vp
){
3543 #endif /* CHECK_CS_VALIDATION_BITMAP */