]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/ubc_subr.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / bsd / kern / ubc_subr.c
1 /*
2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * File: ubc_subr.c
30 * Author: Umesh Vaishampayan [umeshv@apple.com]
31 * 05-Aug-1999 umeshv Created.
32 *
33 * Functions related to Unified Buffer cache.
34 *
35 * Caller of UBC functions MUST have a valid reference on the vnode.
36 *
37 */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/lock.h>
43 #include <sys/mman.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>
50 #include <sys/buf.h>
51 #include <sys/user.h>
52 #include <sys/codesign.h>
53
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>
59 #include <mach/upl.h>
60
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 */
67
68 #include <libkern/crypto/sha1.h>
69 #include <libkern/libkern.h>
70
71 #include <sys/kasl.h>
72 #include <sys/syslog.h>
73
74 #include <security/mac_framework.h>
75
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,
78 boolean_t *);
79 extern kern_return_t memory_object_signed(memory_object_control_t control,
80 boolean_t is_signed);
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);
83
84 extern void Debugger(const char *message);
85
86
87 /* XXX no one uses this interface! */
88 kern_return_t ubc_page_op_with_control(
89 memory_object_control_t control,
90 off_t f_offset,
91 int ops,
92 ppnum_t *phys_entryp,
93 int *flagsp);
94
95
96 #if DIAGNOSTIC
97 #if defined(assert)
98 #undef assert
99 #endif
100 #define assert(cond) \
101 ((void) ((cond) ? 0 : panic("Assert failed: %s", # cond)))
102 #else
103 #include <kern/assert.h>
104 #endif /* DIAGNOSTIC */
105
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);
110
111 struct zone *ubc_info_zone;
112
113
114 /*
115 * CODESIGNING
116 * Routines to navigate code signing data structures in the kernel...
117 */
118
119 extern int cs_debug;
120
121 static boolean_t
122 cs_valid_range(
123 const void *start,
124 const void *end,
125 const void *lower_bound,
126 const void *upper_bound)
127 {
128 if (upper_bound < lower_bound ||
129 end < start) {
130 return FALSE;
131 }
132
133 if (start < lower_bound ||
134 end > upper_bound) {
135 return FALSE;
136 }
137
138 return TRUE;
139 }
140
141 static void
142 hex_str(
143 const unsigned char *hash,
144 size_t len,
145 char *buf)
146 {
147 unsigned int n;
148 for (n = 0; n < len; n++)
149 snprintf(buf + 2*n, 3, "%02.2x", hash[n]);
150 }
151
152
153 /*
154 * Locate the CodeDirectory from an embedded signature blob
155 */
156 static const
157 CS_CodeDirectory *findCodeDirectory(
158 const CS_SuperBlob *embedded,
159 char *lower_bound,
160 char *upper_bound)
161 {
162 const CS_CodeDirectory *cd = NULL;
163
164 if (embedded &&
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;
169
170 limit = &embedded->index[ntohl(embedded->count)];
171 if (!cs_valid_range(&embedded->index[0], limit,
172 lower_bound, upper_bound)) {
173 return NULL;
174 }
175 for (p = embedded->index; p < limit; ++p) {
176 if (ntohl(p->type) == CSSLOT_CODEDIRECTORY) {
177 const unsigned char *base;
178
179 base = (const unsigned char *)embedded;
180 cd = (const CS_CodeDirectory *)(base + ntohl(p->offset));
181 break;
182 }
183 }
184 } else {
185 /*
186 * Detached signatures come as a bare CS_CodeDirectory,
187 * without a blob.
188 */
189 cd = (const CS_CodeDirectory *) embedded;
190 }
191
192 if (cd &&
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) &&
202
203 ntohl(cd->magic) == CSMAGIC_CODEDIRECTORY) {
204 return cd;
205 }
206
207 // not found or not a valid code directory
208 return NULL;
209 }
210
211
212 /*
213 * Locating a page hash
214 */
215 static const unsigned char *
216 hashes(
217 const CS_CodeDirectory *cd,
218 unsigned page,
219 char *lower_bound,
220 char *upper_bound)
221 {
222 const unsigned char *base, *top, *hash;
223 uint32_t nCodeSlots = ntohl(cd->nCodeSlots);
224
225 assert(cs_valid_range(cd, cd + 1, lower_bound, upper_bound));
226
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 */
233 do {
234 if((const char*)scatter > (const char*)cd + ntohl(cd->length)) {
235 if(cs_debug) {
236 printf("CODE SIGNING: Scatter extends past Code Directory\n");
237 }
238 return NULL;
239 }
240
241 scount = ntohl(scatter->count);
242 uint32_t new_base = ntohl(scatter->base);
243
244 /* last scatter? */
245 if (scount == 0) {
246 return NULL;
247 }
248
249 if((hashindex > 0) && (new_base <= sbase)) {
250 if(cs_debug) {
251 printf("CODE SIGNING: unordered Scatter, prev base %d, cur base %d\n",
252 sbase, new_base);
253 }
254 return NULL; /* unordered scatter array */
255 }
256 sbase = new_base;
257
258 /* this scatter beyond page we're looking for? */
259 if (sbase > page) {
260 return NULL;
261 }
262
263 if (sbase+scount >= page) {
264 /* Found the scatter struct that is
265 * referencing our page */
266
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,
273 upper_bound) ||
274 hashindex > nCodeSlots) {
275 return NULL;
276 }
277
278 break;
279 }
280
281 /* this scatter struct is before the page we're looking
282 * for. Iterate. */
283 hashindex+=scount;
284 scatter++;
285 } while(1);
286
287 hash = base + (page - sbase) * SHA1_RESULTLEN;
288 } else {
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) ||
292 page > nCodeSlots) {
293 return NULL;
294 }
295 assert(page < nCodeSlots);
296
297 hash = base + page * SHA1_RESULTLEN;
298 }
299
300 if (!cs_valid_range(hash, hash + SHA1_RESULTLEN,
301 lower_bound, upper_bound)) {
302 hash = NULL;
303 }
304
305 return hash;
306 }
307
308 /*
309 * cs_validate_codedirectory
310 *
311 * Validate that pointers inside the code directory to make sure that
312 * all offsets and lengths are constrained within the buffer.
313 *
314 * Parameters: cd Pointer to code directory buffer
315 * length Length of buffer
316 *
317 * Returns: 0 Success
318 * EBADEXEC Invalid code signature
319 */
320
321 static int
322 cs_validate_codedirectory(const CS_CodeDirectory *cd, size_t length)
323 {
324
325 if (length < sizeof(*cd))
326 return EBADEXEC;
327 if (ntohl(cd->magic) != CSMAGIC_CODEDIRECTORY)
328 return EBADEXEC;
329 if (cd->hashSize != SHA1_RESULTLEN)
330 return EBADEXEC;
331 if (cd->pageSize != PAGE_SHIFT)
332 return EBADEXEC;
333 if (cd->hashType != CS_HASHTYPE_SHA1)
334 return EBADEXEC;
335
336 if (length < ntohl(cd->hashOffset))
337 return EBADEXEC;
338
339 /* check that nSpecialSlots fits in the buffer in front of hashOffset */
340 if (ntohl(cd->hashOffset) / SHA1_RESULTLEN < ntohl(cd->nSpecialSlots))
341 return EBADEXEC;
342
343 /* check that codeslots fits in the buffer */
344 if ((length - ntohl(cd->hashOffset)) / SHA1_RESULTLEN < ntohl(cd->nCodeSlots))
345 return EBADEXEC;
346
347 if (ntohl(cd->version) >= CS_SUPPORTSSCATTER && cd->scatterOffset) {
348
349 if (length < ntohl(cd->scatterOffset))
350 return EBADEXEC;
351
352 SC_Scatter *scatter = (SC_Scatter *)
353 (((uint8_t *)cd) + ntohl(cd->scatterOffset));
354 uint32_t nPages = 0;
355
356 /*
357 * Check each scatter buffer, since we don't know the
358 * length of the scatter buffer array, we have to
359 * check each entry.
360 */
361 while(1) {
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)
364 return EBADEXEC;
365 uint32_t scount = ntohl(scatter->count);
366 if (scount == 0)
367 break;
368 if (nPages + scount < nPages)
369 return EBADEXEC;
370 nPages += scount;
371 scatter++;
372
373 /* XXX check that basees doesn't overlap */
374 /* XXX check that targetOffset doesn't overlap */
375 }
376 #if 0 /* rdar://12579439 */
377 if (nPages != ntohl(cd->nCodeSlots))
378 return EBADEXEC;
379 #endif
380 }
381
382 if (length < ntohl(cd->identOffset))
383 return EBADEXEC;
384
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)
389 return EBADEXEC;
390 }
391
392 return 0;
393 }
394
395 /*
396 *
397 */
398
399 static int
400 cs_validate_blob(const CS_GenericBlob *blob, size_t length)
401 {
402 if (length < sizeof(CS_GenericBlob) || length < ntohl(blob->length))
403 return EBADEXEC;
404 return 0;
405 }
406
407 /*
408 * cs_validate_csblob
409 *
410 * Validate that superblob/embedded code directory to make sure that
411 * all internal pointers are valid.
412 *
413 * Will validate both a superblob csblob and a "raw" code directory.
414 *
415 *
416 * Parameters: buffer Pointer to code signature
417 * length Length of buffer
418 * rcd returns pointer to code directory
419 *
420 * Returns: 0 Success
421 * EBADEXEC Invalid code signature
422 */
423
424 static int
425 cs_validate_csblob(const uint8_t *addr, size_t length,
426 const CS_CodeDirectory **rcd)
427 {
428 const CS_GenericBlob *blob = (const CS_GenericBlob *)(void *)addr;
429 int error;
430
431 *rcd = NULL;
432
433 error = cs_validate_blob(blob, length);
434 if (error)
435 return error;
436
437 length = ntohl(blob->length);
438
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);
442
443 if (length < sizeof(CS_SuperBlob))
444 return EBADEXEC;
445
446 /* check that the array of BlobIndex fits in the rest of the data */
447 if ((length - sizeof(CS_SuperBlob)) / sizeof(CS_BlobIndex) < count)
448 return EBADEXEC;
449
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))
454 return EBADEXEC;
455
456 const CS_GenericBlob *subBlob =
457 (const CS_GenericBlob *)(void *)(addr + ntohl(blobIndex->offset));
458
459 size_t subLength = length - ntohl(blobIndex->offset);
460
461 if ((error = cs_validate_blob(subBlob, subLength)) != 0)
462 return error;
463 subLength = ntohl(subBlob->length);
464
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)
469 return error;
470 *rcd = cd;
471 }
472 }
473
474 } else if (ntohl(blob->magic) == CSMAGIC_CODEDIRECTORY) {
475
476 if ((error = cs_validate_codedirectory((const CS_CodeDirectory *)(void *)addr, length)) != 0)
477 return error;
478 *rcd = (const CS_CodeDirectory *)blob;
479 } else {
480 return EBADEXEC;
481 }
482
483 if (*rcd == NULL)
484 return EBADEXEC;
485
486 return 0;
487 }
488
489 /*
490 * cs_find_blob_bytes
491 *
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.
495 *
496 * Will also find a "raw" code directory if its stored as well as
497 * searching the superblob.
498 *
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
503 *
504 * Returns: pointer Success
505 * NULL Buffer not found
506 */
507
508 static const CS_GenericBlob *
509 cs_find_blob_bytes(const uint8_t *addr, size_t length, uint32_t type, uint32_t magic)
510 {
511 const CS_GenericBlob *blob = (const CS_GenericBlob *)(void *)addr;
512
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);
516
517 for (n = 0; n < count; n++) {
518 if (ntohl(sb->index[n].type) != type)
519 continue;
520 uint32_t offset = ntohl(sb->index[n].offset);
521 if (length - sizeof(const CS_GenericBlob) < offset)
522 return NULL;
523 blob = (const CS_GenericBlob *)(void *)(addr + offset);
524 if (ntohl(blob->magic) != magic)
525 continue;
526 return blob;
527 }
528 } else if (type == CSSLOT_CODEDIRECTORY
529 && ntohl(blob->magic) == CSMAGIC_CODEDIRECTORY
530 && magic == CSMAGIC_CODEDIRECTORY)
531 return blob;
532 return NULL;
533 }
534
535
536 static const CS_GenericBlob *
537 cs_find_blob(struct cs_blob *csblob, uint32_t type, uint32_t magic)
538 {
539 if ((csblob->csb_flags & CS_VALID) == 0)
540 return NULL;
541 return cs_find_blob_bytes((const uint8_t *)csblob->csb_mem_kaddr, csblob->csb_mem_size, type, magic);
542 }
543
544 static const uint8_t *
545 cs_find_special_slot(const CS_CodeDirectory *cd, uint32_t slot)
546 {
547 /* there is no zero special slot since that is the first code slot */
548 if (ntohl(cd->nSpecialSlots) < slot || slot == 0)
549 return NULL;
550
551 return ((const uint8_t *)cd + ntohl(cd->hashOffset) - (SHA1_RESULTLEN * slot));
552 }
553
554 /*
555 * CODESIGNING
556 * End of routines to navigate code signing data structures in the kernel.
557 */
558
559 /*
560 * ENTITLEMENTS
561 * Routines to navigate entitlements in the kernel.
562 */
563
564 /* Retrieve the entitlements blob for a process.
565 * Returns:
566 * EINVAL no text vnode associated with the process
567 * EBADEXEC invalid code signing data
568 * 0 no error occurred
569 *
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.
573 */
574
575 static uint8_t sha1_zero[SHA1_RESULTLEN] = { 0 };
576
577 int
578 cs_entitlements_blob_get(proc_t p, void **out_start, size_t *out_length)
579 {
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;
585 SHA1_CTX context;
586
587 *out_start = NULL;
588 *out_length = 0;
589
590 if (NULL == p->p_textvp)
591 return EINVAL;
592
593 if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, p->p_textoff)) == NULL)
594 return 0;
595
596 if ((code_dir = (const CS_CodeDirectory *)cs_find_blob(csblob, CSSLOT_CODEDIRECTORY, CSMAGIC_CODEDIRECTORY)) == NULL)
597 return 0;
598
599 entitlements = cs_find_blob(csblob, CSSLOT_ENTITLEMENTS, CSMAGIC_EMBEDDED_ENTITLEMENTS);
600 embedded_hash = cs_find_special_slot(code_dir, CSSLOT_ENTITLEMENTS);
601
602 if (embedded_hash == NULL) {
603 if (entitlements)
604 return EBADEXEC;
605 return 0;
606 } else if (entitlements == NULL && memcmp(embedded_hash, sha1_zero, SHA1_RESULTLEN) != 0) {
607 return EBADEXEC;
608 }
609
610 SHA1Init(&context);
611 SHA1Update(&context, entitlements, ntohl(entitlements->length));
612 SHA1Final(computed_hash, &context);
613 if (memcmp(computed_hash, embedded_hash, SHA1_RESULTLEN) != 0)
614 return EBADEXEC;
615
616 *out_start = (void *)entitlements;
617 *out_length = ntohl(entitlements->length);
618
619 return 0;
620 }
621
622 /* Retrieve the codesign identity for a process.
623 * Returns:
624 * NULL an error occured
625 * string the cs_identity
626 */
627
628 const char *
629 cs_identity_get(proc_t p)
630 {
631 const CS_CodeDirectory *code_dir;
632 struct cs_blob *csblob;
633
634 if (NULL == p->p_textvp)
635 return NULL;
636
637 if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, p->p_textoff)) == NULL)
638 return NULL;
639
640 if ((code_dir = (const CS_CodeDirectory *)cs_find_blob(csblob, CSSLOT_CODEDIRECTORY, CSMAGIC_CODEDIRECTORY)) == NULL)
641 return NULL;
642
643 if (code_dir->identOffset == 0)
644 return NULL;
645
646 return ((const char *)code_dir) + ntohl(code_dir->identOffset);
647 }
648
649
650
651 /* Retrieve the codesign blob for a process.
652 * Returns:
653 * EINVAL no text vnode associated with the process
654 * 0 no error occurred
655 *
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.
659 */
660
661 int
662 cs_blob_get(proc_t p, void **out_start, size_t *out_length)
663 {
664 struct cs_blob *csblob;
665
666 *out_start = NULL;
667 *out_length = 0;
668
669 if (NULL == p->p_textvp)
670 return EINVAL;
671
672 if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, p->p_textoff)) == NULL)
673 return 0;
674
675 *out_start = (void *)csblob->csb_mem_kaddr;
676 *out_length = csblob->csb_mem_size;
677
678 return 0;
679 }
680
681 uint8_t *
682 cs_get_cdhash(struct proc *p)
683 {
684 struct cs_blob *csblob;
685
686 if (NULL == p->p_textvp)
687 return NULL;
688
689 if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, p->p_textoff)) == NULL)
690 return NULL;
691
692 return csblob->csb_sha1;
693 }
694
695 /*
696 * ENTITLEMENTS
697 * End of routines to navigate entitlements in the kernel.
698 */
699
700
701
702 /*
703 * ubc_init
704 *
705 * Initialization of the zone for Unified Buffer Cache.
706 *
707 * Parameters: (void)
708 *
709 * Returns: (void)
710 *
711 * Implicit returns:
712 * ubc_info_zone(global) initialized for subsequent allocations
713 */
714 __private_extern__ void
715 ubc_init(void)
716 {
717 int i;
718
719 i = (vm_size_t) sizeof (struct ubc_info);
720
721 ubc_info_zone = zinit (i, 10000*i, 8192, "ubc_info zone");
722
723 zone_change(ubc_info_zone, Z_NOENCRYPT, TRUE);
724 }
725
726
727 /*
728 * ubc_info_init
729 *
730 * Allocate and attach an empty ubc_info structure to a vnode
731 *
732 * Parameters: vp Pointer to the vnode
733 *
734 * Returns: 0 Success
735 * vnode_size:ENOMEM Not enough space
736 * vnode_size:??? Other error from vnode_getattr
737 *
738 */
739 int
740 ubc_info_init(struct vnode *vp)
741 {
742 return(ubc_info_init_internal(vp, 0, 0));
743 }
744
745
746 /*
747 * ubc_info_init_withsize
748 *
749 * Allocate and attach a sized ubc_info structure to a vnode
750 *
751 * Parameters: vp Pointer to the vnode
752 * filesize The size of the file
753 *
754 * Returns: 0 Success
755 * vnode_size:ENOMEM Not enough space
756 * vnode_size:??? Other error from vnode_getattr
757 */
758 int
759 ubc_info_init_withsize(struct vnode *vp, off_t filesize)
760 {
761 return(ubc_info_init_internal(vp, 1, filesize));
762 }
763
764
765 /*
766 * ubc_info_init_internal
767 *
768 * Allocate and attach a ubc_info structure to a vnode
769 *
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
774 *
775 * Returns: 0 Success
776 * vnode_size:ENOMEM Not enough space
777 * vnode_size:??? Other error from vnode_getattr
778 *
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
783 * NOT expandable.
784 *
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.
788 *
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.
792 */
793 static int
794 ubc_info_init_internal(vnode_t vp, int withfsize, off_t filesize)
795 {
796 register struct ubc_info *uip;
797 void * pager;
798 int error = 0;
799 kern_return_t kret;
800 memory_object_control_t control;
801
802 uip = vp->v_ubcinfo;
803
804 /*
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.
807 */
808 if (uip == UBC_INFO_NULL) {
809
810 uip = (struct ubc_info *) zalloc(ubc_info_zone);
811 bzero((char *)uip, sizeof(struct ubc_info));
812
813 uip->ui_vnode = vp;
814 uip->ui_flags = UI_INITED;
815 uip->ui_ucred = NOCRED;
816 }
817 assert(uip->ui_flags != UI_NONE);
818 assert(uip->ui_vnode == vp);
819
820 /* now set this ubc_info in the vnode */
821 vp->v_ubcinfo = uip;
822
823 /*
824 * Allocate a pager object for this vnode
825 *
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.
829 */
830 pager = (void *)vnode_pager_setup(vp, uip->ui_pager);
831 assert(pager);
832
833 /*
834 * Explicitly set the pager into the ubc_info, after setting the
835 * UI_HASPAGER flag.
836 */
837 SET(uip->ui_flags, UI_HASPAGER);
838 uip->ui_pager = pager;
839
840 /*
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.
845 * So use bogus size.
846 */
847
848 /*
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.
855 */
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);
861
862 assert(control);
863 uip->ui_control = control; /* cache the value of the mo control */
864 SET(uip->ui_flags, UI_HASOBJREF); /* with a named reference */
865
866 if (withfsize == 0) {
867 /* initialize the size */
868 error = vnode_size(vp, &uip->ui_size, vfs_context_current());
869 if (error)
870 uip->ui_size = 0;
871 } else {
872 uip->ui_size = filesize;
873 }
874 vp->v_lflag |= VNAMED_UBC; /* vnode has a named ubc reference */
875
876 return (error);
877 }
878
879
880 /*
881 * ubc_info_free
882 *
883 * Free a ubc_info structure
884 *
885 * Parameters: uip A pointer to the ubc_info to free
886 *
887 * Returns: (void)
888 *
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.
892 *
893 * It's actually impossible for a ubc_info.ui_control to take the
894 * value MEMORY_OBJECT_CONTROL_NULL.
895 */
896 static void
897 ubc_info_free(struct ubc_info *uip)
898 {
899 if (IS_VALID_CRED(uip->ui_ucred)) {
900 kauth_cred_unref(&uip->ui_ucred);
901 }
902
903 if (uip->ui_control != MEMORY_OBJECT_CONTROL_NULL)
904 memory_object_control_deallocate(uip->ui_control);
905
906 cluster_release(uip);
907 ubc_cs_free(uip);
908
909 zfree(ubc_info_zone, uip);
910 return;
911 }
912
913
914 void
915 ubc_info_deallocate(struct ubc_info *uip)
916 {
917 ubc_info_free(uip);
918 }
919
920
921 /*
922 * ubc_setsize
923 *
924 * Tell the VM that the the size of the file represented by the vnode has
925 * changed
926 *
927 * Parameters: vp The vp whose backing file size is
928 * being changed
929 * nsize The new size of the backing file
930 *
931 * Returns: 1 Success
932 * 0 Failure
933 *
934 * Notes: This function will indicate failure if the new size that's
935 * being attempted to be set is negative.
936 *
937 * This function will fail if there is no ubc_info currently
938 * associated with the vnode.
939 *
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).
944 *
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.
950 */
951 int
952 ubc_setsize(struct vnode *vp, off_t nsize)
953 {
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;
959
960 if (nsize < (off_t)0)
961 return (0);
962
963 if (!UBCINFOEXISTS(vp))
964 return (0);
965
966 uip = vp->v_ubcinfo;
967 osize = uip->ui_size;
968 /*
969 * Update the size before flushing the VM
970 */
971 uip->ui_size = nsize;
972
973 if (nsize >= osize) { /* Nothing more to do */
974 if (nsize > osize) {
975 lock_vnode_and_post(vp, NOTE_EXTEND);
976 }
977
978 return (1); /* return success */
979 }
980
981 /*
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
986 */
987 lastpg = trunc_page_64(nsize);
988 olastpgend = round_page_64(osize);
989 control = uip->ui_control;
990 assert(control);
991 lastoff = (nsize & PAGE_MASK_64);
992
993 if (lastoff) {
994 upl_t upl;
995 upl_page_info_t *pl;
996
997
998 /*
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
1002 */
1003 kret = ubc_create_upl(vp, lastpg, PAGE_SIZE, &upl, &pl, UPL_SET_LITE);
1004
1005 if (kret != KERN_SUCCESS)
1006 panic("ubc_setsize: ubc_create_upl (error = %d)\n", kret);
1007
1008 if (upl_valid_page(pl, 0))
1009 cluster_zero(upl, (uint32_t)lastoff, PAGE_SIZE - (uint32_t)lastoff, NULL);
1010
1011 ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
1012
1013 lastpg += PAGE_SIZE_64;
1014 }
1015 if (olastpgend > lastpg) {
1016 int flags;
1017
1018 if (lastpg == 0)
1019 flags = MEMORY_OBJECT_DATA_FLUSH_ALL;
1020 else
1021 flags = MEMORY_OBJECT_DATA_FLUSH;
1022 /*
1023 * invalidate the pages beyond the new EOF page
1024 *
1025 */
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);
1032 }
1033 return ((kret == KERN_SUCCESS) ? 1 : 0);
1034 }
1035
1036
1037 /*
1038 * ubc_getsize
1039 *
1040 * Get the size of the file assocated with the specified vnode
1041 *
1042 * Parameters: vp The vnode whose size is of interest
1043 *
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
1047 *
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().
1054 */
1055 off_t
1056 ubc_getsize(struct vnode *vp)
1057 {
1058 /* people depend on the side effect of this working this way
1059 * as they call this for directory
1060 */
1061 if (!UBCINFOEXISTS(vp))
1062 return ((off_t)0);
1063 return (vp->v_ubcinfo->ui_size);
1064 }
1065
1066
1067 /*
1068 * ubc_umount
1069 *
1070 * Call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes for this
1071 * mount point
1072 *
1073 * Parameters: mp The mount point
1074 *
1075 * Returns: 0 Success
1076 *
1077 * Notes: There is no failure indication for this function.
1078 *
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
1082 * indefinitely.
1083 *
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.
1089 */
1090 __private_extern__ int
1091 ubc_umount(struct mount *mp)
1092 {
1093 vnode_iterate(mp, 0, ubc_umcallback, 0);
1094 return(0);
1095 }
1096
1097
1098 /*
1099 * ubc_umcallback
1100 *
1101 * Used by ubc_umount() as an internal implementation detail; see ubc_umount()
1102 * and vnode_iterate() for details of implementation.
1103 */
1104 static int
1105 ubc_umcallback(vnode_t vp, __unused void * args)
1106 {
1107
1108 if (UBCINFOEXISTS(vp)) {
1109
1110 (void) ubc_msync(vp, (off_t)0, ubc_getsize(vp), NULL, UBC_PUSHALL);
1111 }
1112 return (VNODE_RETURNED);
1113 }
1114
1115
1116 /*
1117 * ubc_getcred
1118 *
1119 * Get the credentials currently active for the ubc_info associated with the
1120 * vnode.
1121 *
1122 * Parameters: vp The vnode whose ubc_info credentials
1123 * are to be retrieved
1124 *
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()
1130 */
1131 kauth_cred_t
1132 ubc_getcred(struct vnode *vp)
1133 {
1134 if (UBCINFOEXISTS(vp))
1135 return (vp->v_ubcinfo->ui_ucred);
1136
1137 return (NOCRED);
1138 }
1139
1140
1141 /*
1142 * ubc_setthreadcred
1143 *
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
1146 * them alone.
1147 *
1148 * Parameters: vp The vnode whose ubc_info creds are to
1149 * be set
1150 * p The process whose credentials are to
1151 * be used, if not running on an assumed
1152 * credential
1153 * thread The thread whose credentials are to
1154 * be used
1155 *
1156 * Returns: 1 This vnode has no associated ubc_info
1157 * 0 Success
1158 *
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
1164 * calls.
1165 *
1166 * This function is generally used only in the following cases:
1167 *
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()
1172 *
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.
1175 *
1176 * There are two potential problems with the use of this API:
1177 *
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
1182 *
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()
1186 * or map_fd().
1187 *
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.
1192 *
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
1196 * server.
1197 */
1198 int
1199 ubc_setthreadcred(struct vnode *vp, proc_t p, thread_t thread)
1200 {
1201 struct ubc_info *uip;
1202 kauth_cred_t credp;
1203 struct uthread *uthread = get_bsdthread_info(thread);
1204
1205 if (!UBCINFOEXISTS(vp))
1206 return (1);
1207
1208 vnode_lock(vp);
1209
1210 uip = vp->v_ubcinfo;
1211 credp = uip->ui_ucred;
1212
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);
1217 } else {
1218 uip->ui_ucred = uthread->uu_ucred;
1219 kauth_cred_ref(uip->ui_ucred);
1220 }
1221 }
1222 vnode_unlock(vp);
1223
1224 return (0);
1225 }
1226
1227
1228 /*
1229 * ubc_setcred
1230 *
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
1233 * alone.
1234 *
1235 * Parameters: vp The vnode whose ubc_info creds are to
1236 * be set
1237 * p The process whose credentials are to
1238 * be used
1239 *
1240 * Returns: 0 This vnode has no associated ubc_info
1241 * 1 Success
1242 *
1243 * Notes: The return values for this function are inverted from nearly
1244 * all other uses in the kernel.
1245 *
1246 * See also ubc_setthreadcred(), above.
1247 *
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.
1251 *
1252 * DEPRECATION: ubc_setcred() is being deprecated. Please use
1253 * ubc_setthreadcred() instead.
1254 */
1255 int
1256 ubc_setcred(struct vnode *vp, proc_t p)
1257 {
1258 struct ubc_info *uip;
1259 kauth_cred_t credp;
1260
1261 /* If there is no ubc_info, deny the operation */
1262 if ( !UBCINFOEXISTS(vp))
1263 return (0);
1264
1265 /*
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.
1268 */
1269 vnode_lock(vp);
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);
1274 }
1275 vnode_unlock(vp);
1276
1277 return (1);
1278 }
1279
1280 /*
1281 * ubc_getpager
1282 *
1283 * Get the pager associated with the ubc_info associated with the vnode.
1284 *
1285 * Parameters: vp The vnode to obtain the pager from
1286 *
1287 * Returns: !VNODE_PAGER_NULL The memory_object_t for the pager
1288 * VNODE_PAGER_NULL There is no ubc_info for this vnode
1289 *
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.
1294 */
1295 __private_extern__ memory_object_t
1296 ubc_getpager(struct vnode *vp)
1297 {
1298 if (UBCINFOEXISTS(vp))
1299 return (vp->v_ubcinfo->ui_pager);
1300
1301 return (0);
1302 }
1303
1304
1305 /*
1306 * ubc_getobject
1307 *
1308 * Get the memory object control associated with the ubc_info associated with
1309 * the vnode
1310 *
1311 * Parameters: vp The vnode to obtain the memory object
1312 * from
1313 * flags DEPRECATED
1314 *
1315 * Returns: !MEMORY_OBJECT_CONTROL_NULL
1316 * MEMORY_OBJECT_CONTROL_NULL
1317 *
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
1326 * another thread.
1327 */
1328 memory_object_control_t
1329 ubc_getobject(struct vnode *vp, __unused int flags)
1330 {
1331 if (UBCINFOEXISTS(vp))
1332 return((vp->v_ubcinfo->ui_control));
1333
1334 return (MEMORY_OBJECT_CONTROL_NULL);
1335 }
1336
1337 boolean_t
1338 ubc_strict_uncached_IO(struct vnode *vp)
1339 {
1340 boolean_t result = FALSE;
1341
1342 if (UBCINFOEXISTS(vp)) {
1343 result = memory_object_is_slid(vp->v_ubcinfo->ui_control);
1344 }
1345 return result;
1346 }
1347
1348 /*
1349 * ubc_blktooff
1350 *
1351 * Convert a given block number to a memory backing object (file) offset for a
1352 * given vnode
1353 *
1354 * Parameters: vp The vnode in which the block is located
1355 * blkno The block number to convert
1356 *
1357 * Returns: !-1 The offset into the backing object
1358 * -1 There is no ubc_info associated with
1359 * the vnode
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().
1366 *
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.
1372 */
1373 off_t
1374 ubc_blktooff(vnode_t vp, daddr64_t blkno)
1375 {
1376 off_t file_offset = -1;
1377 int error;
1378
1379 if (UBCINFOEXISTS(vp)) {
1380 error = VNOP_BLKTOOFF(vp, blkno, &file_offset);
1381 if (error)
1382 file_offset = -1;
1383 }
1384
1385 return (file_offset);
1386 }
1387
1388
1389 /*
1390 * ubc_offtoblk
1391 *
1392 * Convert a given offset in a memory backing object into a block number for a
1393 * given vnode
1394 *
1395 * Parameters: vp The vnode in which the offset is
1396 * located
1397 * offset The offset into the backing object
1398 *
1399 * Returns: !-1 The returned block number
1400 * -1 There is no ubc_info associated with
1401 * the vnode
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().
1408 *
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
1414 * here instead.
1415 */
1416 daddr64_t
1417 ubc_offtoblk(vnode_t vp, off_t offset)
1418 {
1419 daddr64_t blkno = -1;
1420 int error = 0;
1421
1422 if (UBCINFOEXISTS(vp)) {
1423 error = VNOP_OFFTOBLK(vp, offset, &blkno);
1424 if (error)
1425 blkno = -1;
1426 }
1427
1428 return (blkno);
1429 }
1430
1431
1432 /*
1433 * ubc_pages_resident
1434 *
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
1437 *
1438 * Parameters: vp The vnode we want to know about
1439 *
1440 * Returns: 1 Yes
1441 * 0 No
1442 */
1443 int
1444 ubc_pages_resident(vnode_t vp)
1445 {
1446 kern_return_t kret;
1447 boolean_t has_pages_resident;
1448
1449 if (!UBCINFOEXISTS(vp))
1450 return (0);
1451
1452 /*
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.
1457 */
1458 kret = memory_object_pages_resident(vp->v_ubcinfo->ui_control, &has_pages_resident);
1459
1460 if (kret != KERN_SUCCESS)
1461 return (0);
1462
1463 if (has_pages_resident == TRUE)
1464 return (1);
1465
1466 return (0);
1467 }
1468
1469
1470 /*
1471 * ubc_sync_range
1472 *
1473 * Clean and/or invalidate a range in the memory object that backs this vnode
1474 *
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()
1481 *
1482 * Returns: 1 Success
1483 * 0 Failure
1484 *
1485 * Notes: see ubc_msync_internal() for more detailed information.
1486 *
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().
1490 */
1491 int
1492 ubc_sync_range(vnode_t vp, off_t beg_off, off_t end_off, int flags)
1493 {
1494 return (ubc_msync_internal(vp, beg_off, end_off, NULL, flags, NULL));
1495 }
1496
1497
1498 /*
1499 * ubc_msync
1500 *
1501 * Clean and/or invalidate a range in the memory object that backs this vnode
1502 *
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()
1511 *
1512 * Returns: 0 Success
1513 * !0 Failure; an errno is returned
1514 *
1515 * Implicit Returns:
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.
1527 *
1528 * Notes: see ubc_msync_internal() for more detailed information.
1529 *
1530 */
1531 errno_t
1532 ubc_msync(vnode_t vp, off_t beg_off, off_t end_off, off_t *resid_off, int flags)
1533 {
1534 int retval;
1535 int io_errno = 0;
1536
1537 if (resid_off)
1538 *resid_off = beg_off;
1539
1540 retval = ubc_msync_internal(vp, beg_off, end_off, resid_off, flags, &io_errno);
1541
1542 if (retval == 0 && io_errno == 0)
1543 return (EINVAL);
1544 return (io_errno);
1545 }
1546
1547
1548 /*
1549 * Clean and/or invalidate a range in the memory object that backs this vnode
1550 *
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
1568 * ignore
1569 *
1570 * Returns: 1 Success
1571 * 0 Failure
1572 *
1573 * Implicit Returns:
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
1579 * occurred.
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.
1589 *
1590 * Notes: If there is no ubc_info associated with the vnode supplied,
1591 * this function immediately returns success.
1592 *
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
1595 * inclusive.
1596 *
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.
1602 *
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).
1608 */
1609 static int
1610 ubc_msync_internal(vnode_t vp, off_t beg_off, off_t end_off, off_t *resid_off, int flags, int *io_errno)
1611 {
1612 memory_object_size_t tsize;
1613 kern_return_t kret;
1614 int request_flags = 0;
1615 int flush_flags = MEMORY_OBJECT_RETURN_NONE;
1616
1617 if ( !UBCINFOEXISTS(vp))
1618 return (0);
1619 if ((flags & (UBC_INVALIDATE | UBC_PUSHDIRTY | UBC_PUSHALL)) == 0)
1620 return (0);
1621 if (end_off <= beg_off)
1622 return (1);
1623
1624 if (flags & UBC_INVALIDATE)
1625 /*
1626 * discard the resident pages
1627 */
1628 request_flags = (MEMORY_OBJECT_DATA_FLUSH | MEMORY_OBJECT_DATA_NO_CHANGE);
1629
1630 if (flags & UBC_SYNC)
1631 /*
1632 * wait for all the I/O to complete before returning
1633 */
1634 request_flags |= MEMORY_OBJECT_IO_SYNC;
1635
1636 if (flags & UBC_PUSHDIRTY)
1637 /*
1638 * we only return the dirty pages in the range
1639 */
1640 flush_flags = MEMORY_OBJECT_RETURN_DIRTY;
1641
1642 if (flags & UBC_PUSHALL)
1643 /*
1644 * then return all the interesting pages in the range (both
1645 * dirty and precious) to the pager
1646 */
1647 flush_flags = MEMORY_OBJECT_RETURN_ALL;
1648
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;
1652
1653 /* flush and/or invalidate pages in the range requested */
1654 kret = memory_object_lock_request(vp->v_ubcinfo->ui_control,
1655 beg_off, tsize,
1656 (memory_object_offset_t *)resid_off,
1657 io_errno, flush_flags, request_flags,
1658 VM_PROT_NO_CHANGE);
1659
1660 return ((kret == KERN_SUCCESS) ? 1 : 0);
1661 }
1662
1663
1664 /*
1665 * ubc_msync_internal
1666 *
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
1670 * we have done this
1671 *
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
1676 *
1677 * Returns: 0 Success
1678 * EPERM Permission was denied
1679 *
1680 * Notes: An I/O reference on the vnode must already be held on entry
1681 *
1682 * If there is no ubc_info associated with the vnode, this function
1683 * will return success.
1684 *
1685 * If a permission error occurs, this function will return
1686 * failure; all other failures will cause this function to return
1687 * success.
1688 *
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:
1692 *
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
1698 *
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
1702 * performance hint.
1703 *
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
1707 * for general use.
1708 *
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
1713 * address space.
1714 */
1715 __private_extern__ int
1716 ubc_map(vnode_t vp, int flags)
1717 {
1718 struct ubc_info *uip;
1719 int error = 0;
1720 int need_ref = 0;
1721 int need_wakeup = 0;
1722
1723 if (UBCINFOEXISTS(vp)) {
1724
1725 vnode_lock(vp);
1726 uip = vp->v_ubcinfo;
1727
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);
1732 }
1733 SET(uip->ui_flags, UI_MAPBUSY);
1734 vnode_unlock(vp);
1735
1736 error = VNOP_MMAP(vp, flags, vfs_context_current());
1737
1738 if (error != EPERM)
1739 error = 0;
1740
1741 vnode_lock_spin(vp);
1742
1743 if (error == 0) {
1744 if ( !ISSET(uip->ui_flags, UI_ISMAPPED))
1745 need_ref = 1;
1746 SET(uip->ui_flags, (UI_WASMAPPED | UI_ISMAPPED));
1747 }
1748 CLR(uip->ui_flags, UI_MAPBUSY);
1749
1750 if (ISSET(uip->ui_flags, UI_MAPWAITING)) {
1751 CLR(uip->ui_flags, UI_MAPWAITING);
1752 need_wakeup = 1;
1753 }
1754 vnode_unlock(vp);
1755
1756 if (need_wakeup)
1757 wakeup(&uip->ui_flags);
1758
1759 if (need_ref)
1760 vnode_ref(vp);
1761 }
1762 return (error);
1763 }
1764
1765
1766 /*
1767 * ubc_destroy_named
1768 *
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
1772 *
1773 * Parameters: vp The designated vnode
1774 *
1775 * Returns: (void)
1776 *
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.
1781 *
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().
1786 *
1787 * IMPORTANT: This is an internal use function that is used
1788 * exclusively by the internal use function vclean().
1789 */
1790 __private_extern__ void
1791 ubc_destroy_named(vnode_t vp)
1792 {
1793 memory_object_control_t control;
1794 struct ubc_info *uip;
1795 kern_return_t kret;
1796
1797 if (UBCINFOEXISTS(vp)) {
1798 uip = vp->v_ubcinfo;
1799
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");
1806 }
1807 }
1808 }
1809
1810
1811 /*
1812 * ubc_isinuse
1813 *
1814 * Determine whether or not a vnode is currently in use by ubc at a level in
1815 * excess of the requested busycount
1816 *
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
1821 *
1822 * Returns: 1 The vnode is in use over the threshold
1823 * 0 The vnode is not in use over the
1824 * threshold
1825 *
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.
1831 *
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.
1834 */
1835 int
1836 ubc_isinuse(struct vnode *vp, int busycount)
1837 {
1838 if ( !UBCINFOEXISTS(vp))
1839 return (0);
1840 return(ubc_isinuse_locked(vp, busycount, 0));
1841 }
1842
1843
1844 /*
1845 * ubc_isinuse_locked
1846 *
1847 * Determine whether or not a vnode is currently in use by ubc at a level in
1848 * excess of the requested busycount
1849 *
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
1855 * the caller
1856 *
1857 * Returns: 1 The vnode is in use over the threshold
1858 * 0 The vnode is not in use over the
1859 * threshold
1860 *
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.
1868 *
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.
1871 */
1872 int
1873 ubc_isinuse_locked(struct vnode *vp, int busycount, int locked)
1874 {
1875 int retval = 0;
1876
1877
1878 if (!locked)
1879 vnode_lock_spin(vp);
1880
1881 if ((vp->v_usecount - vp->v_kusecount) > busycount)
1882 retval = 1;
1883
1884 if (!locked)
1885 vnode_unlock(vp);
1886 return (retval);
1887 }
1888
1889
1890 /*
1891 * ubc_unmap
1892 *
1893 * Reverse the effects of a ubc_map() call for a given vnode
1894 *
1895 * Parameters: vp vnode to unmap from ubc
1896 *
1897 * Returns: (void)
1898 *
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.
1905 *
1906 * IMPORTANT: This MUST only be called by the VM
1907 * to prevent race conditions.
1908 */
1909 __private_extern__ void
1910 ubc_unmap(struct vnode *vp)
1911 {
1912 struct ubc_info *uip;
1913 int need_rele = 0;
1914 int need_wakeup = 0;
1915
1916 if (vnode_getwithref(vp))
1917 return;
1918
1919 if (UBCINFOEXISTS(vp)) {
1920 vnode_lock(vp);
1921 uip = vp->v_ubcinfo;
1922
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);
1927 }
1928 SET(uip->ui_flags, UI_MAPBUSY);
1929
1930 if (ISSET(uip->ui_flags, UI_ISMAPPED)) {
1931 CLR(uip->ui_flags, UI_ISMAPPED);
1932 need_rele = 1;
1933 }
1934 vnode_unlock(vp);
1935
1936 if (need_rele) {
1937 (void)VNOP_MNOMAP(vp, vfs_context_current());
1938 vnode_rele(vp);
1939 }
1940
1941 vnode_lock_spin(vp);
1942
1943 CLR(uip->ui_flags, UI_MAPBUSY);
1944 if (ISSET(uip->ui_flags, UI_MAPWAITING)) {
1945 CLR(uip->ui_flags, UI_MAPWAITING);
1946 need_wakeup = 1;
1947 }
1948 vnode_unlock(vp);
1949
1950 if (need_wakeup)
1951 wakeup(&uip->ui_flags);
1952
1953 }
1954 /*
1955 * the drop of the vnode ref will cleanup
1956 */
1957 vnode_put(vp);
1958 }
1959
1960
1961 /*
1962 * ubc_page_op
1963 *
1964 * Manipulate individual page state for a vnode with an associated ubc_info
1965 * with an associated memory object control.
1966 *
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
1972 * to ignore
1973 * flagsp A pointer to an int to contain flags;
1974 * may be NULL to ignore
1975 *
1976 * Returns: KERN_SUCCESS Success
1977 * KERN_INVALID_ARGUMENT If the memory object control has no VM
1978 * object associated
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
1984 *
1985 * Implicit Returns:
1986 * *phys_entryp (modified) If phys_entryp is non-NULL and
1987 * UPL_POP_PHYSICAL
1988 * *flagsp (modified) If flagsp is non-NULL and there was
1989 * !UPL_POP_PHYSICAL and a KERN_SUCCESS
1990 *
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.
1997 *
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.
2003 *
2004 * The flags field may contain a specific operation, such as
2005 * UPL_POP_PHYSICAL or UPL_POP_DUMP:
2006 *
2007 * o UPL_POP_PHYSICAL Fail if not contiguous; if
2008 * *phys_entryp and successful, set
2009 * *phys_entryp
2010 * o UPL_POP_DUMP Dump the specified page
2011 *
2012 * Otherwise, it is treated as a bitmap of one or more page
2013 * operations to perform on the final memory object; allowable
2014 * bit values are:
2015 *
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
2021 *
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:
2025 *
2026 * o UPL_POP_SET Set the current bitmap bits
2027 * o UPL_POP_CLR Clear the current bitmap bits
2028 *
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.
2035 */
2036 kern_return_t
2037 ubc_page_op(
2038 struct vnode *vp,
2039 off_t f_offset,
2040 int ops,
2041 ppnum_t *phys_entryp,
2042 int *flagsp)
2043 {
2044 memory_object_control_t control;
2045
2046 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2047 if (control == MEMORY_OBJECT_CONTROL_NULL)
2048 return KERN_INVALID_ARGUMENT;
2049
2050 return (memory_object_page_op(control,
2051 (memory_object_offset_t)f_offset,
2052 ops,
2053 phys_entryp,
2054 flagsp));
2055 }
2056
2057
2058 /*
2059 * ubc_range_op
2060 *
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).
2066 *
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
2073 * ignore
2074 *
2075 * Returns: KERN_SUCCESS Success
2076 * KERN_INVALID_ARGUMENT If the memory object control has no VM
2077 * object associated
2078 * KERN_INVALID_OBJECT If the object is physically contiguous
2079 *
2080 * Implicit Returns:
2081 * *range (modified) If range is non-NULL, its contents will
2082 * be modified to contain the number of
2083 * bytes successfully operated upon.
2084 *
2085 * Notes: IMPORTANT: This function cannot be used on a range that
2086 * consists of physically contiguous pages.
2087 *
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.
2095 *
2096 * *range will be modified only if this function succeeds.
2097 *
2098 * The flags field MUST contain a specific operation; allowable
2099 * values are:
2100 *
2101 * o UPL_ROP_ABSENT Returns the extent of the range
2102 * presented which is absent, starting
2103 * with the start address presented
2104 *
2105 * o UPL_ROP_PRESENT Returns the extent of the range
2106 * presented which is present (resident),
2107 * starting with the start address
2108 * presented
2109 * o UPL_ROP_DUMP Dump the pages which are found in the
2110 * target object for the target range.
2111 *
2112 * IMPORTANT: For UPL_ROP_ABSENT and UPL_ROP_PRESENT; if there are
2113 * multiple regions in the range, only the first matching region
2114 * is returned.
2115 */
2116 kern_return_t
2117 ubc_range_op(
2118 struct vnode *vp,
2119 off_t f_offset_beg,
2120 off_t f_offset_end,
2121 int ops,
2122 int *range)
2123 {
2124 memory_object_control_t control;
2125
2126 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2127 if (control == MEMORY_OBJECT_CONTROL_NULL)
2128 return KERN_INVALID_ARGUMENT;
2129
2130 return (memory_object_range_op(control,
2131 (memory_object_offset_t)f_offset_beg,
2132 (memory_object_offset_t)f_offset_end,
2133 ops,
2134 range));
2135 }
2136
2137
2138 /*
2139 * ubc_create_upl
2140 *
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.
2144 *
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
2153 * to ignore
2154 *
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
2163 * invalid
2164 * Implicit Returns:
2165 * *uplp (modified)
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
2171 *
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().
2175 */
2176 kern_return_t
2177 ubc_create_upl(
2178 struct vnode *vp,
2179 off_t f_offset,
2180 int bufsize,
2181 upl_t *uplp,
2182 upl_page_info_t **plp,
2183 int uplflags)
2184 {
2185 memory_object_control_t control;
2186 kern_return_t kr;
2187
2188 if (plp != NULL)
2189 *plp = NULL;
2190 *uplp = NULL;
2191
2192 if (bufsize & 0xfff)
2193 return KERN_INVALID_ARGUMENT;
2194
2195 if (bufsize > MAX_UPL_SIZE * PAGE_SIZE)
2196 return KERN_INVALID_ARGUMENT;
2197
2198 if (uplflags & (UPL_UBC_MSYNC | UPL_UBC_PAGEOUT | UPL_UBC_PAGEIN)) {
2199
2200 if (uplflags & UPL_UBC_MSYNC) {
2201 uplflags &= UPL_RET_ONLY_DIRTY;
2202
2203 uplflags |= UPL_COPYOUT_FROM | UPL_CLEAN_IN_PLACE |
2204 UPL_SET_INTERNAL | UPL_SET_LITE;
2205
2206 } else if (uplflags & UPL_UBC_PAGEOUT) {
2207 uplflags &= UPL_RET_ONLY_DIRTY;
2208
2209 if (uplflags & UPL_RET_ONLY_DIRTY)
2210 uplflags |= UPL_NOBLOCK;
2211
2212 uplflags |= UPL_FOR_PAGEOUT | UPL_CLEAN_IN_PLACE |
2213 UPL_COPYOUT_FROM | UPL_SET_INTERNAL | UPL_SET_LITE;
2214 } else {
2215 uplflags |= UPL_RET_ONLY_ABSENT |
2216 UPL_NO_SYNC | UPL_CLEAN_IN_PLACE |
2217 UPL_SET_INTERNAL | UPL_SET_LITE;
2218
2219 /*
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
2226 */
2227 if (bufsize > PAGE_SIZE)
2228 uplflags |= UPL_NOBLOCK;
2229 }
2230 } else {
2231 uplflags &= ~UPL_FOR_PAGEOUT;
2232
2233 if (uplflags & UPL_WILL_BE_DUMPED) {
2234 uplflags &= ~UPL_WILL_BE_DUMPED;
2235 uplflags |= (UPL_NO_SYNC|UPL_SET_INTERNAL);
2236 } else
2237 uplflags |= (UPL_NO_SYNC|UPL_CLEAN_IN_PLACE|UPL_SET_INTERNAL);
2238 }
2239 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2240 if (control == MEMORY_OBJECT_CONTROL_NULL)
2241 return KERN_INVALID_ARGUMENT;
2242
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);
2246 return kr;
2247 }
2248
2249
2250 /*
2251 * ubc_upl_maxbufsize
2252 *
2253 * Return the maximum bufsize ubc_create_upl( ) will take.
2254 *
2255 * Parameters: none
2256 *
2257 * Returns: maximum size buffer (in bytes) ubc_create_upl( ) will take.
2258 */
2259 upl_size_t
2260 ubc_upl_maxbufsize(
2261 void)
2262 {
2263 return(MAX_UPL_SIZE * PAGE_SIZE);
2264 }
2265
2266 /*
2267 * ubc_upl_map
2268 *
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
2272 *
2273 * Parameters: upl The upl to map
2274 * dst_addr The address at which to map the upl
2275 *
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
2282 */
2283 kern_return_t
2284 ubc_upl_map(
2285 upl_t upl,
2286 vm_offset_t *dst_addr)
2287 {
2288 return (vm_upl_map(kernel_map, upl, dst_addr));
2289 }
2290
2291
2292 /*
2293 * ubc_upl_unmap
2294 *
2295 * Unmap the page list assocated with the supplied upl from the kernel virtual
2296 * address space; the entire upl is unmapped.
2297 *
2298 * Parameters: upl The upl to unmap
2299 *
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
2303 */
2304 kern_return_t
2305 ubc_upl_unmap(
2306 upl_t upl)
2307 {
2308 return(vm_upl_unmap(kernel_map, upl));
2309 }
2310
2311
2312 /*
2313 * ubc_upl_commit
2314 *
2315 * Commit the contents of the upl to the backing store
2316 *
2317 * Parameters: upl The upl to commit
2318 *
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
2324 * the upl
2325 *
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.
2330 *
2331 * IMPORTANT: Use of this function should not be mixed with use of
2332 * ubc_upl_commit_range(), due to the unconditional deallocation
2333 * by this function.
2334 */
2335 kern_return_t
2336 ubc_upl_commit(
2337 upl_t upl)
2338 {
2339 upl_page_info_t *pl;
2340 kern_return_t kr;
2341
2342 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
2343 kr = upl_commit(upl, pl, MAX_UPL_SIZE);
2344 upl_deallocate(upl);
2345 return kr;
2346 }
2347
2348
2349 /*
2350 * ubc_upl_commit
2351 *
2352 * Commit the contents of the specified range of the upl to the backing store
2353 *
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)
2359 *
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
2365 * the upl
2366 *
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.
2372 *
2373 * The flags argument is a bitmap of flags on the rage of pages in
2374 * the upl to be committed; allowable flags are:
2375 *
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
2381 * later pageout
2382 * o UPL_COMMIT_SET_DIRTY Set each pages dirty
2383 * bit; will cause a later
2384 * pageout
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
2393 * an IOKit driver
2394 *
2395 * The flag UPL_COMMIT_NOTIFY_EMPTY is used internally, and should
2396 * not be specified by the caller.
2397 *
2398 * The UPL_COMMIT_CLEAR_DIRTY and UPL_COMMIT_SET_DIRTY flags are
2399 * mutually exclusive, and should not be combined.
2400 */
2401 kern_return_t
2402 ubc_upl_commit_range(
2403 upl_t upl,
2404 upl_offset_t offset,
2405 upl_size_t size,
2406 int flags)
2407 {
2408 upl_page_info_t *pl;
2409 boolean_t empty;
2410 kern_return_t kr;
2411
2412 if (flags & UPL_COMMIT_FREE_ON_EMPTY)
2413 flags |= UPL_COMMIT_NOTIFY_EMPTY;
2414
2415 if (flags & UPL_COMMIT_KERNEL_ONLY_FLAGS) {
2416 return KERN_INVALID_ARGUMENT;
2417 }
2418
2419 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
2420
2421 kr = upl_commit_range(upl, offset, size, flags,
2422 pl, MAX_UPL_SIZE, &empty);
2423
2424 if((flags & UPL_COMMIT_FREE_ON_EMPTY) && empty)
2425 upl_deallocate(upl);
2426
2427 return kr;
2428 }
2429
2430
2431 /*
2432 * ubc_upl_abort_range
2433 *
2434 * Abort the contents of the specified range of the specified upl
2435 *
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)
2441 *
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
2447 * the upl
2448 *
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.
2454 *
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:
2457 *
2458 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2459 * empty and has been successfully
2460 * aborted
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
2467 *
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.
2472 *
2473 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2474 * referenced; do not use it.
2475 */
2476 kern_return_t
2477 ubc_upl_abort_range(
2478 upl_t upl,
2479 upl_offset_t offset,
2480 upl_size_t size,
2481 int abort_flags)
2482 {
2483 kern_return_t kr;
2484 boolean_t empty = FALSE;
2485
2486 if (abort_flags & UPL_ABORT_FREE_ON_EMPTY)
2487 abort_flags |= UPL_ABORT_NOTIFY_EMPTY;
2488
2489 kr = upl_abort_range(upl, offset, size, abort_flags, &empty);
2490
2491 if((abort_flags & UPL_ABORT_FREE_ON_EMPTY) && empty)
2492 upl_deallocate(upl);
2493
2494 return kr;
2495 }
2496
2497
2498 /*
2499 * ubc_upl_abort
2500 *
2501 * Abort the contents of the specified upl
2502 *
2503 * Parameters: upl The upl to abort
2504 * abort_type abort type (see below)
2505 *
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
2511 * the upl
2512 *
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.
2518 *
2519 * The abort_type is a bitmap of flags on the range of
2520 * pages in the upl to be aborted; allowable flags are:
2521 *
2522 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2523 * empty and has been successfully
2524 * aborted
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
2531 *
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.
2536 *
2537 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2538 * referenced; do not use it.
2539 */
2540 kern_return_t
2541 ubc_upl_abort(
2542 upl_t upl,
2543 int abort_type)
2544 {
2545 kern_return_t kr;
2546
2547 kr = upl_abort(upl, abort_type);
2548 upl_deallocate(upl);
2549 return kr;
2550 }
2551
2552
2553 /*
2554 * ubc_upl_pageinfo
2555 *
2556 * Retrieve the internal page list for the specified upl
2557 *
2558 * Parameters: upl The upl to obtain the page list from
2559 *
2560 * Returns: !NULL The (upl_page_info_t *) for the page
2561 * list internal to the upl
2562 * NULL Error/no page list associated
2563 *
2564 * Notes: IMPORTANT: The function is only valid on internal objects
2565 * where the list request was made with the UPL_INTERNAL flag.
2566 *
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.
2570 */
2571 upl_page_info_t *
2572 ubc_upl_pageinfo(
2573 upl_t upl)
2574 {
2575 return (UPL_GET_INTERNAL_PAGE_LIST(upl));
2576 }
2577
2578
2579 int
2580 UBCINFOEXISTS(struct vnode * vp)
2581 {
2582 return((vp) && ((vp)->v_type == VREG) && ((vp)->v_ubcinfo != UBC_INFO_NULL));
2583 }
2584
2585
2586 void
2587 ubc_upl_range_needed(
2588 upl_t upl,
2589 int index,
2590 int count)
2591 {
2592 upl_range_needed(upl, index, count);
2593 }
2594
2595
2596 /*
2597 * CODE SIGNING
2598 */
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;
2605
2606 int cs_validation = 1;
2607
2608 #ifndef SECURE_KERNEL
2609 SYSCTL_INT(_vm, OID_AUTO, cs_validation, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_validation, 0, "Do validate code signatures");
2610 #endif
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");
2616
2617
2618 kern_return_t
2619 ubc_cs_blob_allocate(
2620 vm_offset_t *blob_addr_p,
2621 vm_size_t *blob_size_p)
2622 {
2623 kern_return_t kr;
2624
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) {
2631 kr = KERN_NO_SPACE;
2632 } else {
2633 kr = KERN_SUCCESS;
2634 }
2635 #endif /* CS_BLOB_PAGEABLE */
2636 return kr;
2637 }
2638
2639 void
2640 ubc_cs_blob_deallocate(
2641 vm_offset_t blob_addr,
2642 vm_size_t blob_size)
2643 {
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 */
2649 }
2650
2651 int
2652 ubc_cs_sigpup_add(
2653 struct vnode *vp,
2654 vm_address_t address,
2655 vm_size_t size)
2656 {
2657 kern_return_t kr;
2658 struct ubc_info *uip;
2659 struct cs_blob *blob;
2660 memory_object_control_t control;
2661 const CS_CodeDirectory *cd;
2662 int error;
2663
2664 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2665 if (control == MEMORY_OBJECT_CONTROL_NULL)
2666 return KERN_INVALID_ARGUMENT;
2667
2668 if (memory_object_is_signed(control))
2669 return 0;
2670
2671 blob = (struct cs_blob *) kalloc(sizeof (struct cs_blob));
2672 if (blob == NULL)
2673 return ENOMEM;
2674
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;
2683
2684 /*
2685 * Validate the blob's contents
2686 */
2687 cd = findCodeDirectory(
2688 (const CS_SuperBlob *) address,
2689 (char *) address,
2690 (char *) address + blob->csb_mem_size);
2691 if (cd == NULL) {
2692 /* no code directory => useless blob ! */
2693 error = EINVAL;
2694 goto out;
2695 }
2696
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;
2703 } else {
2704 blob->csb_start_offset = (blob->csb_end_offset - (ntohl(cd->nCodeSlots) * PAGE_SIZE));
2705 }
2706
2707 /*
2708 * We don't need to check with the policy module, since the input data is supposed to be already checked
2709 */
2710
2711 vnode_lock(vp);
2712 if (! UBCINFOEXISTS(vp)) {
2713 vnode_unlock(vp);
2714 if (cs_debug)
2715 printf("out ubc object\n");
2716 error = ENOENT;
2717 goto out;
2718 }
2719 uip = vp->v_ubcinfo;
2720
2721 /* someone raced us to adding the code directory */
2722 if (uip->cs_blobs != NULL) {
2723 if (cs_debug)
2724 printf("sigpup: vnode already have CD ?\n");
2725 vnode_unlock(vp);
2726 error = EEXIST;
2727 goto out;
2728 }
2729
2730 blob->csb_next = uip->cs_blobs;
2731 uip->cs_blobs = blob;
2732
2733 OSAddAtomic(+1, &cs_blob_count);
2734 OSAddAtomic((SInt32) +blob->csb_mem_size, &cs_blob_size);
2735
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) {
2739 vnode_unlock(vp);
2740 if (cs_debug)
2741 printf("sigpup: not signable ?\n");
2742 error = ENOENT;
2743 goto out;
2744 }
2745
2746 vnode_unlock(vp);
2747
2748 error = 0;
2749 out:
2750 if (error) {
2751 if (cs_debug)
2752 printf("sigpup: not signable ?\n");
2753 /* we failed; release what we allocated */
2754 if (blob) {
2755 kfree(blob, sizeof (*blob));
2756 blob = NULL;
2757 }
2758 }
2759
2760 return error;
2761 }
2762
2763 int
2764 ubc_cs_blob_add(
2765 struct vnode *vp,
2766 cpu_type_t cputype,
2767 off_t base_offset,
2768 vm_address_t addr,
2769 off_t blob_offset,
2770 vm_size_t size)
2771 {
2772 kern_return_t kr;
2773 struct ubc_info *uip;
2774 struct cs_blob *blob, *oblob;
2775 int error;
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;
2780 SHA1_CTX sha1ctxt;
2781 boolean_t record_mtime;
2782
2783 record_mtime = FALSE;
2784
2785 blob_handle = IPC_PORT_NULL;
2786
2787 blob = (struct cs_blob *) kalloc(sizeof (struct cs_blob));
2788 if (blob == NULL) {
2789 return ENOMEM;
2790 }
2791
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,
2796 &blob_size,
2797 addr,
2798 VM_PROT_READ,
2799 &blob_handle,
2800 IPC_PORT_NULL);
2801 if (kr != KERN_SUCCESS) {
2802 error = ENOMEM;
2803 goto out;
2804 }
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);
2810 error = EINVAL;
2811 goto out;
2812 }
2813 #else
2814 blob_size = (memory_object_size_t) size;
2815 blob_handle = IPC_PORT_NULL;
2816 #endif
2817
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;
2828
2829 /*
2830 * Validate the blob's contents
2831 */
2832
2833 error = cs_validate_csblob((const uint8_t *)addr, size, &cd);
2834 if (error) {
2835 if (cs_debug)
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 */
2842 } else {
2843 const unsigned char *sha1_base;
2844 int sha1_size;
2845
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;
2852 } else {
2853 blob->csb_start_offset = (blob->csb_end_offset -
2854 (ntohl(cd->nCodeSlots) * PAGE_SIZE));
2855 }
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);
2862 }
2863
2864 /*
2865 * Let policy module check whether the blob's signature is accepted.
2866 */
2867 #if CONFIG_MACF
2868 error = mac_vnode_check_signature(vp, base_offset, blob->csb_sha1, (void*)addr, size);
2869 if (error)
2870 goto out;
2871 #endif
2872
2873 /*
2874 * Validate the blob's coverage
2875 */
2876 blob_start_offset = blob->csb_base_offset + blob->csb_start_offset;
2877 blob_end_offset = blob->csb_base_offset + blob->csb_end_offset;
2878
2879 if (blob_start_offset >= blob_end_offset ||
2880 blob_start_offset < 0 ||
2881 blob_end_offset <= 0) {
2882 /* reject empty or backwards blob */
2883 error = EINVAL;
2884 goto out;
2885 }
2886
2887 vnode_lock(vp);
2888 if (! UBCINFOEXISTS(vp)) {
2889 vnode_unlock(vp);
2890 error = ENOENT;
2891 goto out;
2892 }
2893 uip = vp->v_ubcinfo;
2894
2895 /* check if this new blob overlaps with an existing blob */
2896 for (oblob = uip->cs_blobs;
2897 oblob != NULL;
2898 oblob = oblob->csb_next) {
2899 off_t oblob_start_offset, oblob_end_offset;
2900
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 */
2908 } else {
2909 /* conflict ! */
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,
2918 oblob->csb_sha1,
2919 SHA1_RESULTLEN)) {
2920 /*
2921 * We already have this blob:
2922 * we'll return success but
2923 * throw away the new blob.
2924 */
2925 if (oblob->csb_cpu_type == CPU_TYPE_ANY) {
2926 /*
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.
2931 */
2932 oblob->csb_cpu_type = cputype;
2933 }
2934 /*
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.
2938 */
2939 oblob->csb_blob_offset = blob_offset;
2940
2941 vnode_unlock(vp);
2942 error = EAGAIN;
2943 goto out;
2944 } else {
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];
2949 char arch_str[20];
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--) ;
2955 }
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",
2960 6,
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",
2967 NULL
2968 );
2969 printf("CODESIGNING: rejected new signature for architecture %d of file %s\n",
2970 cputype, pathbuf);
2971 vnode_unlock(vp);
2972 error = EALREADY;
2973 goto out;
2974 }
2975 }
2976
2977 }
2978
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) {
2982 vnode_unlock(vp);
2983 error = ENOENT;
2984 goto out;
2985 }
2986
2987 if (uip->cs_blobs == NULL) {
2988 /* loading 1st blob: record the file's current "modify time" */
2989 record_mtime = TRUE;
2990 }
2991
2992 /*
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.
2998 */
2999 blob->csb_next = uip->cs_blobs;
3000 uip->cs_blobs = blob;
3001
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 ? */
3005 }
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 ? */
3009 }
3010 if ((UInt32) blob->csb_mem_size > cs_blob_size_max) {
3011 cs_blob_size_max = (UInt32) blob->csb_mem_size;
3012 }
3013
3014 if (cs_debug > 1) {
3015 proc_t p;
3016 const char *name = vnode_getname_printable(vp);
3017 p = current_proc();
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",
3023 name,
3024 blob->csb_base_offset + blob->csb_start_offset,
3025 blob->csb_base_offset + blob->csb_end_offset,
3026 blob->csb_flags);
3027 vnode_putname_printable(name);
3028 }
3029
3030 vnode_unlock(vp);
3031
3032 if (record_mtime) {
3033 vnode_mtime(vp, &uip->cs_mtime, vfs_context_current());
3034 }
3035
3036 error = 0; /* success ! */
3037
3038 out:
3039 if (error) {
3040 /* we failed; release what we allocated */
3041 if (blob) {
3042 kfree(blob, sizeof (*blob));
3043 blob = NULL;
3044 }
3045 if (blob_handle != IPC_PORT_NULL) {
3046 mach_memory_entry_port_release(blob_handle);
3047 blob_handle = IPC_PORT_NULL;
3048 }
3049 }
3050
3051 if (error == EAGAIN) {
3052 /*
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.
3056 */
3057 error = 0;
3058 /*
3059 * Since we're not failing, consume the data we received.
3060 */
3061 ubc_cs_blob_deallocate(addr, size);
3062 }
3063
3064 return error;
3065 }
3066
3067
3068 struct cs_blob *
3069 ubc_cs_blob_get(
3070 struct vnode *vp,
3071 cpu_type_t cputype,
3072 off_t offset)
3073 {
3074 struct ubc_info *uip;
3075 struct cs_blob *blob;
3076 off_t offset_in_blob;
3077
3078 vnode_lock_spin(vp);
3079
3080 if (! UBCINFOEXISTS(vp)) {
3081 blob = NULL;
3082 goto out;
3083 }
3084
3085 uip = vp->v_ubcinfo;
3086 for (blob = uip->cs_blobs;
3087 blob != NULL;
3088 blob = blob->csb_next) {
3089 if (cputype != -1 && blob->csb_cpu_type == cputype) {
3090 break;
3091 }
3092 if (offset != -1) {
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 */
3097 break;
3098 }
3099 }
3100 }
3101
3102 if (cs_debug && blob != NULL && blob->csb_sigpup)
3103 printf("found sig pup blob\n");
3104 out:
3105 vnode_unlock(vp);
3106
3107 return blob;
3108 }
3109
3110 static void
3111 ubc_cs_free(
3112 struct ubc_info *uip)
3113 {
3114 struct cs_blob *blob, *next_blob;
3115
3116 for (blob = uip->cs_blobs;
3117 blob != NULL;
3118 blob = next_blob) {
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;
3124 }
3125 if (blob->csb_mem_handle != IPC_PORT_NULL) {
3126 mach_memory_entry_port_release(blob->csb_mem_handle);
3127 }
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));
3132 }
3133 #if CHECK_CS_VALIDATION_BITMAP
3134 ubc_cs_validation_bitmap_deallocate( uip->ui_vnode );
3135 #endif
3136 uip->cs_blobs = NULL;
3137 }
3138
3139 struct cs_blob *
3140 ubc_get_cs_blobs(
3141 struct vnode *vp)
3142 {
3143 struct ubc_info *uip;
3144 struct cs_blob *blobs;
3145
3146 /*
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
3151 * list.
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.
3159 */
3160
3161 if (! UBCINFOEXISTS(vp)) {
3162 blobs = NULL;
3163 goto out;
3164 }
3165
3166 uip = vp->v_ubcinfo;
3167 blobs = uip->cs_blobs;
3168
3169 out:
3170 return blobs;
3171 }
3172
3173 void
3174 ubc_get_cs_mtime(
3175 struct vnode *vp,
3176 struct timespec *cs_mtime)
3177 {
3178 struct ubc_info *uip;
3179
3180 if (! UBCINFOEXISTS(vp)) {
3181 cs_mtime->tv_sec = 0;
3182 cs_mtime->tv_nsec = 0;
3183 return;
3184 }
3185
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;
3189 }
3190
3191 unsigned long cs_validate_page_no_hash = 0;
3192 unsigned long cs_validate_page_bad_hash = 0;
3193 boolean_t
3194 cs_validate_page(
3195 void *_blobs,
3196 memory_object_t pager,
3197 memory_object_offset_t page_offset,
3198 const void *data,
3199 boolean_t *tainted)
3200 {
3201 SHA1_CTX sha1ctxt;
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 */
3211 size_t size;
3212 off_t codeLimit = 0;
3213 char *lower_bound, *upper_bound;
3214 vm_offset_t kaddr, blob_addr;
3215 vm_size_t ksize;
3216 kern_return_t kr;
3217
3218 offset = page_offset;
3219
3220 /* retrieve the expected hash */
3221 found_hash = FALSE;
3222 blobs = (struct cs_blob *) _blobs;
3223
3224 for (blob = blobs;
3225 blob != NULL;
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 */
3231 continue;
3232 }
3233
3234 /* map the blob in the kernel address space */
3235 kaddr = blob->csb_mem_kaddr;
3236 if (kaddr == 0) {
3237 ksize = (vm_size_t) (blob->csb_mem_size +
3238 blob->csb_mem_offset);
3239 kr = vm_map(kernel_map,
3240 &kaddr,
3241 ksize,
3242 0,
3243 VM_FLAGS_ANYWHERE,
3244 blob->csb_mem_handle,
3245 0,
3246 TRUE,
3247 VM_PROT_READ,
3248 VM_PROT_READ,
3249 VM_INHERIT_NONE);
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);
3255 break;
3256 }
3257 }
3258 if (blob->csb_sigpup && cs_debug)
3259 printf("checking for a sigpup CD\n");
3260
3261 blob_addr = kaddr + blob->csb_mem_offset;
3262
3263 lower_bound = CAST_DOWN(char *, blob_addr);
3264 upper_bound = lower_bound + blob->csb_mem_size;
3265
3266 embedded = (const CS_SuperBlob *) blob_addr;
3267 cd = findCodeDirectory(embedded, lower_bound, upper_bound);
3268 if (cd != NULL) {
3269 if (cd->pageSize != PAGE_SHIFT ||
3270 cd->hashType != CS_HASHTYPE_SHA1 ||
3271 cd->hashSize != SHA1_RESULTLEN) {
3272 /* bogus blob ? */
3273 if (blob->csb_sigpup && cs_debug)
3274 printf("page foo bogus sigpup CD\n");
3275 continue;
3276 }
3277
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");
3284 continue;
3285 }
3286
3287 codeLimit = ntohl(cd->codeLimit);
3288 if (blob->csb_sigpup && cs_debug)
3289 printf("sigpup codesize %d\n", (int)codeLimit);
3290
3291 hash = hashes(cd, (unsigned)atop(offset),
3292 lower_bound, upper_bound);
3293 if (hash != NULL) {
3294 bcopy(hash, expected_hash,
3295 sizeof (expected_hash));
3296 found_hash = TRUE;
3297 if (blob->csb_sigpup && cs_debug)
3298 printf("sigpup hash\n");
3299 }
3300
3301 break;
3302 } else {
3303 if (blob->csb_sigpup && cs_debug)
3304 printf("sig pup had no valid CD\n");
3305
3306 }
3307 }
3308
3309 if (found_hash == FALSE) {
3310 /*
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
3314 * been loaded yet.
3315 * Report that the page has not been validated and let the
3316 * caller decide if it wants to accept it or not.
3317 */
3318 cs_validate_page_no_hash++;
3319 if (cs_debug > 1) {
3320 printf("CODE SIGNING: cs_validate_page: "
3321 "mobj %p off 0x%llx: no hash to validate !?\n",
3322 pager, page_offset);
3323 }
3324 validated = FALSE;
3325 *tainted = FALSE;
3326 } else {
3327
3328 size = PAGE_SIZE;
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);
3333 }
3334 /* compute the actual page's SHA1 hash */
3335 SHA1Init(&sha1ctxt);
3336 SHA1UpdateUsePhysicalAddress(&sha1ctxt, data, size);
3337 SHA1Final(actual_hash, &sha1ctxt);
3338
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);
3344 if (cs_debug) {
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);
3348 }
3349 cs_validate_page_bad_hash++;
3350 if (!*tainted) {
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",
3354 5,
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",
3360 NULL
3361 );
3362 }
3363 *tainted = TRUE;
3364 } else {
3365 if (cs_debug > 10) {
3366 printf("CODE SIGNING: cs_validate_page: "
3367 "mobj %p off 0x%llx size 0x%lx: "
3368 "SHA1 OK\n",
3369 pager, page_offset, size);
3370 }
3371 *tainted = FALSE;
3372 }
3373 validated = TRUE;
3374 }
3375
3376 return validated;
3377 }
3378
3379 int
3380 ubc_cs_getcdhash(
3381 vnode_t vp,
3382 off_t offset,
3383 unsigned char *cdhash)
3384 {
3385 struct cs_blob *blobs, *blob;
3386 off_t rel_offset;
3387 int ret;
3388
3389 vnode_lock(vp);
3390
3391 blobs = ubc_get_cs_blobs(vp);
3392 for (blob = blobs;
3393 blob != NULL;
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" ! */
3400 break;
3401 }
3402 }
3403
3404 if (blob == NULL) {
3405 /* we didn't find a blob covering "offset" */
3406 ret = EBADEXEC; /* XXX any better error ? */
3407 } else {
3408 /* get the SHA1 hash of that blob */
3409 bcopy(blob->csb_sha1, cdhash, sizeof (blob->csb_sha1));
3410 ret = 0;
3411 }
3412
3413 vnode_unlock(vp);
3414
3415 return ret;
3416 }
3417
3418 #if CHECK_CS_VALIDATION_BITMAP
3419 #define stob(s) ((atop_64((s)) + 07) >> 3)
3420 extern boolean_t root_fs_upgrade_try;
3421
3422 /*
3423 * Should we use the code-sign bitmap to avoid repeated code-sign validation?
3424 * Depends:
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.
3428 */
3429 #define USE_CODE_SIGN_BITMAP(vp) ( (vp != NULL) && (vp->v_mount != NULL) && (vp->v_mount->mnt_flag & MNT_ROOTFS) && !root_fs_upgrade_try)
3430 kern_return_t
3431 ubc_cs_validation_bitmap_allocate(
3432 vnode_t vp)
3433 {
3434 kern_return_t kr = KERN_SUCCESS;
3435 struct ubc_info *uip;
3436 char *target_bitmap;
3437 vm_object_size_t bitmap_size;
3438
3439 if ( ! USE_CODE_SIGN_BITMAP(vp) || (! UBCINFOEXISTS(vp))) {
3440 kr = KERN_INVALID_ARGUMENT;
3441 } else {
3442 uip = vp->v_ubcinfo;
3443
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) {
3448 kr = KERN_NO_SPACE;
3449 } else {
3450 kr = KERN_SUCCESS;
3451 }
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;
3456 }
3457 }
3458 }
3459 return kr;
3460 }
3461
3462 kern_return_t
3463 ubc_cs_check_validation_bitmap (
3464 vnode_t vp,
3465 memory_object_offset_t offset,
3466 int optype)
3467 {
3468 kern_return_t kr = KERN_SUCCESS;
3469
3470 if ( ! USE_CODE_SIGN_BITMAP(vp) || ! UBCINFOEXISTS(vp)) {
3471 kr = KERN_INVALID_ARGUMENT;
3472 } else {
3473 struct ubc_info *uip = vp->v_ubcinfo;
3474 char *target_bitmap = uip->cs_valid_bitmap;
3475
3476 if ( target_bitmap == NULL ) {
3477 kr = KERN_INVALID_ARGUMENT;
3478 } else {
3479 uint64_t bit, byte;
3480 bit = atop_64( offset );
3481 byte = bit >> 3;
3482
3483 if ( byte > uip->cs_valid_bitmap_size ) {
3484 kr = KERN_INVALID_ARGUMENT;
3485 } else {
3486
3487 if (optype == CS_BITMAP_SET) {
3488 target_bitmap[byte] |= (1 << (bit & 07));
3489 kr = KERN_SUCCESS;
3490 } else if (optype == CS_BITMAP_CLEAR) {
3491 target_bitmap[byte] &= ~(1 << (bit & 07));
3492 kr = KERN_SUCCESS;
3493 } else if (optype == CS_BITMAP_CHECK) {
3494 if ( target_bitmap[byte] & (1 << (bit & 07))) {
3495 kr = KERN_SUCCESS;
3496 } else {
3497 kr = KERN_FAILURE;
3498 }
3499 }
3500 }
3501 }
3502 }
3503 return kr;
3504 }
3505
3506 void
3507 ubc_cs_validation_bitmap_deallocate(
3508 vnode_t vp)
3509 {
3510 struct ubc_info *uip;
3511 void *target_bitmap;
3512 vm_object_size_t bitmap_size;
3513
3514 if ( UBCINFOEXISTS(vp)) {
3515 uip = vp->v_ubcinfo;
3516
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;
3521 }
3522 }
3523 }
3524 #else
3525 kern_return_t ubc_cs_validation_bitmap_allocate(__unused vnode_t vp){
3526 return KERN_INVALID_ARGUMENT;
3527 }
3528
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){
3533
3534 return KERN_INVALID_ARGUMENT;
3535 }
3536
3537 void ubc_cs_validation_bitmap_deallocate(__unused vnode_t vp){
3538 return;
3539 }
3540 #endif /* CHECK_CS_VALIDATION_BITMAP */