]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/ubc_subr.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / bsd / kern / ubc_subr.c
CommitLineData
1c79356b 1/*
cf7d32b8 2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 *
0b4e3aa0
A
35 * Caller of UBC functions MUST have a valid reference on the vnode.
36 *
1c79356b
A
37 */
38
1c79356b
A
39#include <sys/types.h>
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/lock.h>
91447636
A
43#include <sys/mman.h>
44#include <sys/mount_internal.h>
45#include <sys/vnode_internal.h>
46#include <sys/ubc_internal.h>
1c79356b 47#include <sys/ucred.h>
91447636
A
48#include <sys/proc_internal.h>
49#include <sys/kauth.h>
1c79356b 50#include <sys/buf.h>
13fec989 51#include <sys/user.h>
2d21ac55 52#include <sys/codesign.h>
1c79356b
A
53
54#include <mach/mach_types.h>
55#include <mach/memory_object_types.h>
91447636
A
56#include <mach/memory_object_control.h>
57#include <mach/vm_map.h>
b0d623f7 58#include <mach/mach_vm.h>
91447636 59#include <mach/upl.h>
1c79356b 60
91447636 61#include <kern/kern_types.h>
2d21ac55 62#include <kern/kalloc.h>
1c79356b 63#include <kern/zalloc.h>
13fec989 64#include <kern/thread.h>
91447636
A
65#include <vm/vm_kern.h>
66#include <vm/vm_protos.h> /* last */
1c79356b 67
2d21ac55 68#include <libkern/crypto/sha1.h>
39236c6e
A
69#include <libkern/libkern.h>
70
71#include <sys/kasl.h>
72#include <sys/syslog.h>
2d21ac55 73
593a1d5f
A
74#include <security/mac_framework.h>
75
2d21ac55
A
76/* XXX These should be in a BSD accessible Mach header, but aren't. */
77extern kern_return_t memory_object_pages_resident(memory_object_control_t,
78 boolean_t *);
79extern kern_return_t memory_object_signed(memory_object_control_t control,
80 boolean_t is_signed);
6d2010ae 81extern boolean_t memory_object_is_slid(memory_object_control_t control);
39236c6e 82extern boolean_t memory_object_is_signed(memory_object_control_t);
6d2010ae 83
2d21ac55
A
84extern void Debugger(const char *message);
85
86
87/* XXX no one uses this interface! */
88kern_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
1c79356b
A
96#if DIAGNOSTIC
97#if defined(assert)
b0d623f7 98#undef assert
1c79356b
A
99#endif
100#define assert(cond) \
2d21ac55 101 ((void) ((cond) ? 0 : panic("Assert failed: %s", # cond)))
1c79356b
A
102#else
103#include <kern/assert.h>
104#endif /* DIAGNOSTIC */
105
2d21ac55 106static int ubc_info_init_internal(struct vnode *vp, int withfsize, off_t filesize);
0c530ab8 107static int ubc_umcallback(vnode_t, void *);
0c530ab8 108static int ubc_msync_internal(vnode_t, off_t, off_t, off_t *, int, int *);
2d21ac55 109static void ubc_cs_free(struct ubc_info *uip);
b4c24cb9 110
91447636 111struct zone *ubc_info_zone;
b4c24cb9 112
2d21ac55
A
113
114/*
115 * CODESIGNING
116 * Routines to navigate code signing data structures in the kernel...
117 */
b0d623f7
A
118
119extern int cs_debug;
120
2d21ac55
A
121static boolean_t
122cs_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
39236c6e
A
141static void
142hex_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}
2d21ac55
A
151
152
153/*
154 * Locate the CodeDirectory from an embedded signature blob
155 */
156static const
157CS_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 }
b0d623f7 191
2d21ac55
A
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) &&
cf7d32b8
A
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
2d21ac55
A
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 */
215static const unsigned char *
216hashes(
217 const CS_CodeDirectory *cd,
218 unsigned page,
219 char *lower_bound,
220 char *upper_bound)
221{
222 const unsigned char *base, *top, *hash;
b0d623f7 223 uint32_t nCodeSlots = ntohl(cd->nCodeSlots);
2d21ac55
A
224
225 assert(cs_valid_range(cd, cd + 1, lower_bound, upper_bound));
226
39236c6e 227 if((ntohl(cd->version) >= CS_SUPPORTSSCATTER) && (ntohl(cd->scatterOffset))) {
b0d623f7 228 /* Get first scatter struct */
39236c6e 229 const SC_Scatter *scatter = (const SC_Scatter*)
b0d623f7
A
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);
2d21ac55 296
b0d623f7
A
297 hash = base + page * SHA1_RESULTLEN;
298 }
299
2d21ac55
A
300 if (!cs_valid_range(hash, hash + SHA1_RESULTLEN,
301 lower_bound, upper_bound)) {
302 hash = NULL;
303 }
304
305 return hash;
306}
39236c6e
A
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
321static int
322cs_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
399static int
400cs_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
424static int
425cs_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
508static const CS_GenericBlob *
509cs_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
536static const CS_GenericBlob *
537cs_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
544static const uint8_t *
545cs_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
2d21ac55
A
554/*
555 * CODESIGNING
556 * End of routines to navigate code signing data structures in the kernel.
557 */
558
6d2010ae
A
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
6d2010ae
A
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 */
39236c6e
A
574
575static uint8_t sha1_zero[SHA1_RESULTLEN] = { 0 };
576
6d2010ae
A
577int
578cs_entitlements_blob_get(proc_t p, void **out_start, size_t *out_length)
579{
39236c6e
A
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;
6d2010ae 608 }
39236c6e 609
6d2010ae 610 SHA1Init(&context);
39236c6e 611 SHA1Update(&context, entitlements, ntohl(entitlements->length));
6d2010ae 612 SHA1Final(computed_hash, &context);
39236c6e
A
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
628const char *
629cs_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
661int
662cs_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
681uint8_t *
682cs_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;
6d2010ae
A
693}
694
695/*
696 * ENTITLEMENTS
697 * End of routines to navigate entitlements in the kernel.
698 */
699
700
2d21ac55 701
1c79356b 702/*
2d21ac55
A
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
1c79356b 713 */
0b4e3aa0 714__private_extern__ void
2d21ac55 715ubc_init(void)
1c79356b
A
716{
717 int i;
718
719 i = (vm_size_t) sizeof (struct ubc_info);
2d21ac55 720
1c79356b 721 ubc_info_zone = zinit (i, 10000*i, 8192, "ubc_info zone");
0b4c1975
A
722
723 zone_change(ubc_info_zone, Z_NOENCRYPT, TRUE);
1c79356b
A
724}
725
2d21ac55 726
1c79356b 727/*
2d21ac55
A
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 *
1c79356b
A
738 */
739int
740ubc_info_init(struct vnode *vp)
91447636
A
741{
742 return(ubc_info_init_internal(vp, 0, 0));
743}
2d21ac55
A
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 */
91447636
A
758int
759ubc_info_init_withsize(struct vnode *vp, off_t filesize)
760{
761 return(ubc_info_init_internal(vp, 1, filesize));
762}
763
2d21ac55
A
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 */
793static int
794ubc_info_init_internal(vnode_t vp, int withfsize, off_t filesize)
1c79356b
A
795{
796 register struct ubc_info *uip;
797 void * pager;
1c79356b
A
798 int error = 0;
799 kern_return_t kret;
0b4e3aa0 800 memory_object_control_t control;
1c79356b 801
91447636 802 uip = vp->v_ubcinfo;
1c79356b 803
2d21ac55
A
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 */
91447636 808 if (uip == UBC_INFO_NULL) {
1c79356b 809
1c79356b 810 uip = (struct ubc_info *) zalloc(ubc_info_zone);
91447636
A
811 bzero((char *)uip, sizeof(struct ubc_info));
812
1c79356b 813 uip->ui_vnode = vp;
91447636 814 uip->ui_flags = UI_INITED;
1c79356b
A
815 uip->ui_ucred = NOCRED;
816 }
1c79356b
A
817 assert(uip->ui_flags != UI_NONE);
818 assert(uip->ui_vnode == vp);
819
1c79356b
A
820 /* now set this ubc_info in the vnode */
821 vp->v_ubcinfo = uip;
91447636 822
2d21ac55
A
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 */
1c79356b
A
830 pager = (void *)vnode_pager_setup(vp, uip->ui_pager);
831 assert(pager);
91447636 832
2d21ac55
A
833 /*
834 * Explicitly set the pager into the ubc_info, after setting the
835 * UI_HASPAGER flag.
836 */
91447636
A
837 SET(uip->ui_flags, UI_HASPAGER);
838 uip->ui_pager = pager;
1c79356b
A
839
840 /*
91447636 841 * Note: We can not use VNOP_GETATTR() to get accurate
2d21ac55
A
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.
1c79356b
A
845 * So use bogus size.
846 */
847
1c79356b 848 /*
0b4e3aa0
A
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.
1c79356b 855 */
0b4e3aa0
A
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);
1c79356b 861
0b4e3aa0
A
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 */
2d21ac55 865
91447636 866 if (withfsize == 0) {
91447636 867 /* initialize the size */
2d21ac55 868 error = vnode_size(vp, &uip->ui_size, vfs_context_current());
91447636
A
869 if (error)
870 uip->ui_size = 0;
871 } else {
872 uip->ui_size = filesize;
873 }
2d21ac55 874 vp->v_lflag |= VNAMED_UBC; /* vnode has a named ubc reference */
1c79356b 875
0b4e3aa0 876 return (error);
1c79356b
A
877}
878
2d21ac55
A
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 */
0b4e3aa0
A
896static void
897ubc_info_free(struct ubc_info *uip)
1c79356b 898{
0c530ab8
A
899 if (IS_VALID_CRED(uip->ui_ucred)) {
900 kauth_cred_unref(&uip->ui_ucred);
1c79356b 901 }
0b4e3aa0
A
902
903 if (uip->ui_control != MEMORY_OBJECT_CONTROL_NULL)
904 memory_object_control_deallocate(uip->ui_control);
91447636
A
905
906 cluster_release(uip);
2d21ac55 907 ubc_cs_free(uip);
0b4e3aa0 908
2d21ac55 909 zfree(ubc_info_zone, uip);
1c79356b
A
910 return;
911}
912
2d21ac55 913
0b4e3aa0
A
914void
915ubc_info_deallocate(struct ubc_info *uip)
916{
91447636 917 ubc_info_free(uip);
0b4e3aa0
A
918}
919
2d21ac55 920
1c79356b 921/*
2d21ac55
A
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.
1c79356b
A
950 */
951int
952ubc_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;
0b4e3aa0 957 memory_object_control_t control;
2d21ac55 958 kern_return_t kret = KERN_SUCCESS;
1c79356b 959
55e303ae
A
960 if (nsize < (off_t)0)
961 return (0);
1c79356b 962
1c79356b 963 if (!UBCINFOEXISTS(vp))
0b4e3aa0 964 return (0);
1c79356b
A
965
966 uip = vp->v_ubcinfo;
2d21ac55
A
967 osize = uip->ui_size;
968 /*
969 * Update the size before flushing the VM
970 */
1c79356b
A
971 uip->ui_size = nsize;
972
b0d623f7 973 if (nsize >= osize) { /* Nothing more to do */
6d2010ae
A
974 if (nsize > osize) {
975 lock_vnode_and_post(vp, NOTE_EXTEND);
976 }
977
0b4e3aa0 978 return (1); /* return success */
b0d623f7 979 }
1c79356b
A
980
981 /*
982 * When the file shrinks, invalidate the pages beyond the
983 * new size. Also get rid of garbage beyond nsize on the
2d21ac55
A
984 * last page. The ui_size already has the nsize, so any
985 * subsequent page-in will zero-fill the tail properly
1c79356b 986 */
1c79356b
A
987 lastpg = trunc_page_64(nsize);
988 olastpgend = round_page_64(osize);
0b4e3aa0
A
989 control = uip->ui_control;
990 assert(control);
1c79356b
A
991 lastoff = (nsize & PAGE_MASK_64);
992
2d21ac55
A
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
1c79356b 1005 if (kret != KERN_SUCCESS)
2d21ac55
A
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);
1c79356b 1012
2d21ac55
A
1013 lastpg += PAGE_SIZE_64;
1014 }
1015 if (olastpgend > lastpg) {
b0d623f7
A
1016 int flags;
1017
1018 if (lastpg == 0)
1019 flags = MEMORY_OBJECT_DATA_FLUSH_ALL;
1020 else
1021 flags = MEMORY_OBJECT_DATA_FLUSH;
2d21ac55
A
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,
b0d623f7 1029 MEMORY_OBJECT_RETURN_NONE, flags, VM_PROT_NO_CHANGE);
2d21ac55
A
1030 if (kret != KERN_SUCCESS)
1031 printf("ubc_setsize: invalidate failed (error = %d)\n", kret);
1032 }
1c79356b
A
1033 return ((kret == KERN_SUCCESS) ? 1 : 0);
1034}
1035
2d21ac55 1036
1c79356b 1037/*
2d21ac55
A
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().
1c79356b
A
1054 */
1055off_t
1056ubc_getsize(struct vnode *vp)
1057{
91447636
A
1058 /* people depend on the side effect of this working this way
1059 * as they call this for directory
1c79356b 1060 */
91447636
A
1061 if (!UBCINFOEXISTS(vp))
1062 return ((off_t)0);
1063 return (vp->v_ubcinfo->ui_size);
1c79356b
A
1064}
1065
2d21ac55 1066
1c79356b 1067/*
2d21ac55
A
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.
1c79356b 1089 */
0b4e3aa0 1090__private_extern__ int
1c79356b
A
1091ubc_umount(struct mount *mp)
1092{
91447636
A
1093 vnode_iterate(mp, 0, ubc_umcallback, 0);
1094 return(0);
1c79356b
A
1095}
1096
2d21ac55
A
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 */
91447636
A
1104static int
1105ubc_umcallback(vnode_t vp, __unused void * args)
1c79356b 1106{
1c79356b 1107
91447636
A
1108 if (UBCINFOEXISTS(vp)) {
1109
91447636 1110 (void) ubc_msync(vp, (off_t)0, ubc_getsize(vp), NULL, UBC_PUSHALL);
1c79356b 1111 }
91447636 1112 return (VNODE_RETURNED);
1c79356b
A
1113}
1114
91447636 1115
2d21ac55
A
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 */
91447636 1131kauth_cred_t
1c79356b
A
1132ubc_getcred(struct vnode *vp)
1133{
91447636
A
1134 if (UBCINFOEXISTS(vp))
1135 return (vp->v_ubcinfo->ui_ucred);
1c79356b 1136
91447636 1137 return (NOCRED);
1c79356b
A
1138}
1139
2d21ac55
A
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 */
13fec989 1198int
2d21ac55 1199ubc_setthreadcred(struct vnode *vp, proc_t p, thread_t thread)
13fec989
A
1200{
1201 struct ubc_info *uip;
1202 kauth_cred_t credp;
2d21ac55 1203 struct uthread *uthread = get_bsdthread_info(thread);
13fec989
A
1204
1205 if (!UBCINFOEXISTS(vp))
2d21ac55 1206 return (1);
13fec989
A
1207
1208 vnode_lock(vp);
1209
1210 uip = vp->v_ubcinfo;
1211 credp = uip->ui_ucred;
1212
0c530ab8 1213 if (!IS_VALID_CRED(credp)) {
13fec989
A
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 }
2d21ac55 1221 }
13fec989
A
1222 vnode_unlock(vp);
1223
1224 return (0);
1225}
1226
2d21ac55 1227
1c79356b 1228/*
2d21ac55
A
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.
1c79356b 1254 */
1c79356b 1255int
2d21ac55 1256ubc_setcred(struct vnode *vp, proc_t p)
1c79356b
A
1257{
1258 struct ubc_info *uip;
91447636 1259 kauth_cred_t credp;
1c79356b 1260
2d21ac55
A
1261 /* If there is no ubc_info, deny the operation */
1262 if ( !UBCINFOEXISTS(vp))
1c79356b 1263 return (0);
1c79356b 1264
2d21ac55
A
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 */
91447636 1269 vnode_lock(vp);
91447636 1270 uip = vp->v_ubcinfo;
1c79356b 1271 credp = uip->ui_ucred;
0c530ab8 1272 if (!IS_VALID_CRED(credp)) {
91447636 1273 uip->ui_ucred = kauth_cred_proc_ref(p);
1c79356b 1274 }
91447636 1275 vnode_unlock(vp);
1c79356b
A
1276
1277 return (1);
1278}
1279
2d21ac55
A
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 */
0b4e3aa0 1295__private_extern__ memory_object_t
1c79356b
A
1296ubc_getpager(struct vnode *vp)
1297{
91447636
A
1298 if (UBCINFOEXISTS(vp))
1299 return (vp->v_ubcinfo->ui_pager);
1c79356b 1300
91447636 1301 return (0);
1c79356b
A
1302}
1303
2d21ac55 1304
1c79356b 1305/*
2d21ac55
A
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.
1c79356b 1327 */
0b4e3aa0 1328memory_object_control_t
91447636 1329ubc_getobject(struct vnode *vp, __unused int flags)
1c79356b 1330{
91447636
A
1331 if (UBCINFOEXISTS(vp))
1332 return((vp->v_ubcinfo->ui_control));
1c79356b 1333
2d21ac55 1334 return (MEMORY_OBJECT_CONTROL_NULL);
1c79356b
A
1335}
1336
6d2010ae
A
1337boolean_t
1338ubc_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}
1c79356b 1347
2d21ac55
A
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 */
1c79356b 1373off_t
91447636 1374ubc_blktooff(vnode_t vp, daddr64_t blkno)
1c79356b 1375{
2d21ac55 1376 off_t file_offset = -1;
1c79356b
A
1377 int error;
1378
2d21ac55
A
1379 if (UBCINFOEXISTS(vp)) {
1380 error = VNOP_BLKTOOFF(vp, blkno, &file_offset);
1381 if (error)
1382 file_offset = -1;
1383 }
1c79356b
A
1384
1385 return (file_offset);
1386}
0b4e3aa0 1387
2d21ac55
A
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 */
91447636
A
1416daddr64_t
1417ubc_offtoblk(vnode_t vp, off_t offset)
1c79356b 1418{
2d21ac55 1419 daddr64_t blkno = -1;
0b4e3aa0 1420 int error = 0;
1c79356b 1421
2d21ac55
A
1422 if (UBCINFOEXISTS(vp)) {
1423 error = VNOP_OFFTOBLK(vp, offset, &blkno);
1424 if (error)
1425 blkno = -1;
1426 }
1c79356b
A
1427
1428 return (blkno);
1429}
1430
2d21ac55
A
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 */
1c79356b 1443int
91447636 1444ubc_pages_resident(vnode_t vp)
1c79356b 1445{
91447636
A
1446 kern_return_t kret;
1447 boolean_t has_pages_resident;
1448
2d21ac55 1449 if (!UBCINFOEXISTS(vp))
0b4e3aa0 1450 return (0);
91447636 1451
2d21ac55
A
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 */
91447636
A
1458 kret = memory_object_pages_resident(vp->v_ubcinfo->ui_control, &has_pages_resident);
1459
1460 if (kret != KERN_SUCCESS)
0b4e3aa0 1461 return (0);
91447636
A
1462
1463 if (has_pages_resident == TRUE)
1464 return (1);
1465
1466 return (0);
1467}
1c79356b 1468
1c79356b 1469
1c79356b 1470/*
2d21ac55
A
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()
91447636 1481 *
2d21ac55
A
1482 * Returns: 1 Success
1483 * 0 Failure
91447636 1484 *
2d21ac55
A
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().
1c79356b
A
1490 */
1491int
91447636 1492ubc_sync_range(vnode_t vp, off_t beg_off, off_t end_off, int flags)
1c79356b 1493{
91447636 1494 return (ubc_msync_internal(vp, beg_off, end_off, NULL, flags, NULL));
0b4e3aa0
A
1495}
1496
91447636 1497
0b4e3aa0 1498/*
2d21ac55
A
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 *
0b4e3aa0 1530 */
91447636
A
1531errno_t
1532ubc_msync(vnode_t vp, off_t beg_off, off_t end_off, off_t *resid_off, int flags)
0b4e3aa0 1533{
91447636
A
1534 int retval;
1535 int io_errno = 0;
1536
1537 if (resid_off)
1538 *resid_off = beg_off;
0b4e3aa0 1539
91447636 1540 retval = ubc_msync_internal(vp, beg_off, end_off, resid_off, flags, &io_errno);
0b4e3aa0 1541
91447636
A
1542 if (retval == 0 && io_errno == 0)
1543 return (EINVAL);
1544 return (io_errno);
1545}
0b4e3aa0 1546
1c79356b 1547
1c79356b 1548/*
2d21ac55
A
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).
1c79356b 1608 */
91447636
A
1609static int
1610ubc_msync_internal(vnode_t vp, off_t beg_off, off_t end_off, off_t *resid_off, int flags, int *io_errno)
1c79356b 1611{
91447636
A
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);
91447636
A
1619 if ((flags & (UBC_INVALIDATE | UBC_PUSHDIRTY | UBC_PUSHALL)) == 0)
1620 return (0);
2d21ac55
A
1621 if (end_off <= beg_off)
1622 return (1);
91447636
A
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);
1c79356b 1629
91447636
A
1630 if (flags & UBC_SYNC)
1631 /*
1632 * wait for all the I/O to complete before returning
55e303ae 1633 */
91447636 1634 request_flags |= MEMORY_OBJECT_IO_SYNC;
55e303ae 1635
91447636
A
1636 if (flags & UBC_PUSHDIRTY)
1637 /*
1638 * we only return the dirty pages in the range
1639 */
1640 flush_flags = MEMORY_OBJECT_RETURN_DIRTY;
0b4e3aa0 1641
91447636
A
1642 if (flags & UBC_PUSHALL)
1643 /*
2d21ac55
A
1644 * then return all the interesting pages in the range (both
1645 * dirty and precious) to the pager
91447636
A
1646 */
1647 flush_flags = MEMORY_OBJECT_RETURN_ALL;
0b4e3aa0 1648
91447636
A
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;
b4c24cb9 1652
91447636
A
1653 /* flush and/or invalidate pages in the range requested */
1654 kret = memory_object_lock_request(vp->v_ubcinfo->ui_control,
2d21ac55
A
1655 beg_off, tsize,
1656 (memory_object_offset_t *)resid_off,
1657 io_errno, flush_flags, request_flags,
1658 VM_PROT_NO_CHANGE);
91447636
A
1659
1660 return ((kret == KERN_SUCCESS) ? 1 : 0);
1c79356b
A
1661}
1662
1c79356b
A
1663
1664/*
2d21ac55
A
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.
1c79356b 1714 */
91447636
A
1715__private_extern__ int
1716ubc_map(vnode_t vp, int flags)
1c79356b
A
1717{
1718 struct ubc_info *uip;
91447636
A
1719 int error = 0;
1720 int need_ref = 0;
2d21ac55 1721 int need_wakeup = 0;
1c79356b 1722
91447636 1723 if (UBCINFOEXISTS(vp)) {
1c79356b 1724
2d21ac55
A
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());
1c79356b 1737
91447636
A
1738 if (error != EPERM)
1739 error = 0;
1c79356b 1740
2d21ac55 1741 vnode_lock_spin(vp);
1c79356b 1742
2d21ac55 1743 if (error == 0) {
91447636
A
1744 if ( !ISSET(uip->ui_flags, UI_ISMAPPED))
1745 need_ref = 1;
1746 SET(uip->ui_flags, (UI_WASMAPPED | UI_ISMAPPED));
2d21ac55
A
1747 }
1748 CLR(uip->ui_flags, UI_MAPBUSY);
55e303ae 1749
2d21ac55
A
1750 if (ISSET(uip->ui_flags, UI_MAPWAITING)) {
1751 CLR(uip->ui_flags, UI_MAPWAITING);
1752 need_wakeup = 1;
55e303ae 1753 }
2d21ac55 1754 vnode_unlock(vp);
b4c24cb9 1755
2d21ac55
A
1756 if (need_wakeup)
1757 wakeup(&uip->ui_flags);
1758
1759 if (need_ref)
1760 vnode_ref(vp);
1761 }
91447636 1762 return (error);
0b4e3aa0
A
1763}
1764
2d21ac55 1765
0b4e3aa0 1766/*
2d21ac55
A
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().
0b4e3aa0 1789 */
2d21ac55
A
1790__private_extern__ void
1791ubc_destroy_named(vnode_t vp)
0b4e3aa0
A
1792{
1793 memory_object_control_t control;
0b4e3aa0
A
1794 struct ubc_info *uip;
1795 kern_return_t kret;
1796
2d21ac55
A
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");
0b4e3aa0
A
1806 }
1807 }
1c79356b
A
1808}
1809
0b4e3aa0 1810
1c79356b 1811/*
2d21ac55
A
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.
1c79356b
A
1834 */
1835int
91447636 1836ubc_isinuse(struct vnode *vp, int busycount)
1c79356b 1837{
91447636 1838 if ( !UBCINFOEXISTS(vp))
0b4e3aa0 1839 return (0);
91447636 1840 return(ubc_isinuse_locked(vp, busycount, 0));
1c79356b
A
1841}
1842
91447636 1843
2d21ac55
A
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 */
1c79356b 1872int
91447636 1873ubc_isinuse_locked(struct vnode *vp, int busycount, int locked)
1c79356b 1874{
91447636 1875 int retval = 0;
1c79356b 1876
9bccf70c 1877
91447636 1878 if (!locked)
b0d623f7 1879 vnode_lock_spin(vp);
1c79356b 1880
91447636
A
1881 if ((vp->v_usecount - vp->v_kusecount) > busycount)
1882 retval = 1;
1883
1884 if (!locked)
1885 vnode_unlock(vp);
1886 return (retval);
1c79356b
A
1887}
1888
91447636 1889
1c79356b 1890/*
2d21ac55
A
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.
1c79356b 1908 */
0b4e3aa0 1909__private_extern__ void
1c79356b
A
1910ubc_unmap(struct vnode *vp)
1911{
1912 struct ubc_info *uip;
91447636 1913 int need_rele = 0;
2d21ac55 1914 int need_wakeup = 0;
b0d623f7 1915
91447636
A
1916 if (vnode_getwithref(vp))
1917 return;
1c79356b 1918
91447636
A
1919 if (UBCINFOEXISTS(vp)) {
1920 vnode_lock(vp);
91447636 1921 uip = vp->v_ubcinfo;
2d21ac55
A
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
91447636 1930 if (ISSET(uip->ui_flags, UI_ISMAPPED)) {
b0d623f7 1931 CLR(uip->ui_flags, UI_ISMAPPED);
91447636
A
1932 need_rele = 1;
1933 }
1934 vnode_unlock(vp);
b0d623f7 1935
91447636 1936 if (need_rele) {
b0d623f7
A
1937 (void)VNOP_MNOMAP(vp, vfs_context_current());
1938 vnode_rele(vp);
91447636 1939 }
2d21ac55
A
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)
b0d623f7 1951 wakeup(&uip->ui_flags);
2d21ac55 1952
91447636
A
1953 }
1954 /*
1955 * the drop of the vnode ref will cleanup
1956 */
1957 vnode_put(vp);
0b4e3aa0
A
1958}
1959
2d21ac55
A
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 */
0b4e3aa0
A
2036kern_return_t
2037ubc_page_op(
2038 struct vnode *vp,
2039 off_t f_offset,
2040 int ops,
55e303ae 2041 ppnum_t *phys_entryp,
0b4e3aa0
A
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}
2d21ac55
A
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 */
55e303ae
A
2116kern_return_t
2117ubc_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}
2d21ac55
A
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 */
0b4e3aa0
A
2176kern_return_t
2177ubc_create_upl(
2178 struct vnode *vp,
2d21ac55 2179 off_t f_offset,
b0d623f7 2180 int bufsize,
2d21ac55 2181 upl_t *uplp,
0b4e3aa0 2182 upl_page_info_t **plp,
2d21ac55 2183 int uplflags)
0b4e3aa0
A
2184{
2185 memory_object_control_t control;
55e303ae 2186 kern_return_t kr;
b0d623f7
A
2187
2188 if (plp != NULL)
2189 *plp = NULL;
2190 *uplp = NULL;
0b4e3aa0
A
2191
2192 if (bufsize & 0xfff)
2193 return KERN_INVALID_ARGUMENT;
2194
6d2010ae
A
2195 if (bufsize > MAX_UPL_SIZE * PAGE_SIZE)
2196 return KERN_INVALID_ARGUMENT;
2197
b0d623f7
A
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 {
316670eb 2215 uplflags |= UPL_RET_ONLY_ABSENT |
b0d623f7
A
2216 UPL_NO_SYNC | UPL_CLEAN_IN_PLACE |
2217 UPL_SET_INTERNAL | UPL_SET_LITE;
316670eb
A
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;
b0d623f7
A
2229 }
2230 } else {
55e303ae 2231 uplflags &= ~UPL_FOR_PAGEOUT;
55e303ae 2232
b0d623f7
A
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);
0b4e3aa0
A
2240 if (control == MEMORY_OBJECT_CONTROL_NULL)
2241 return KERN_INVALID_ARGUMENT;
2242
b0d623f7
A
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);
0b4e3aa0
A
2246 return kr;
2247}
2d21ac55
A
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 */
2259upl_size_t
2260ubc_upl_maxbufsize(
2261 void)
2262{
cf7d32b8 2263 return(MAX_UPL_SIZE * PAGE_SIZE);
2d21ac55 2264}
0b4e3aa0 2265
2d21ac55
A
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 */
0b4e3aa0
A
2283kern_return_t
2284ubc_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
2d21ac55
A
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 */
0b4e3aa0
A
2304kern_return_t
2305ubc_upl_unmap(
2306 upl_t upl)
2307{
2308 return(vm_upl_unmap(kernel_map, upl));
2309}
2310
2d21ac55
A
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 */
0b4e3aa0
A
2335kern_return_t
2336ubc_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);
cf7d32b8 2343 kr = upl_commit(upl, pl, MAX_UPL_SIZE);
0b4e3aa0
A
2344 upl_deallocate(upl);
2345 return kr;
1c79356b
A
2346}
2347
0b4e3aa0 2348
2d21ac55
A
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 */
0b4e3aa0
A
2401kern_return_t
2402ubc_upl_commit_range(
2403 upl_t upl,
b0d623f7
A
2404 upl_offset_t offset,
2405 upl_size_t size,
0b4e3aa0
A
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
593a1d5f
A
2415 if (flags & UPL_COMMIT_KERNEL_ONLY_FLAGS) {
2416 return KERN_INVALID_ARGUMENT;
2417 }
2418
0b4e3aa0
A
2419 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
2420
2421 kr = upl_commit_range(upl, offset, size, flags,
cf7d32b8 2422 pl, MAX_UPL_SIZE, &empty);
0b4e3aa0
A
2423
2424 if((flags & UPL_COMMIT_FREE_ON_EMPTY) && empty)
2425 upl_deallocate(upl);
2426
2427 return kr;
2428}
2d21ac55
A
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 */
0b4e3aa0
A
2476kern_return_t
2477ubc_upl_abort_range(
2478 upl_t upl,
b0d623f7
A
2479 upl_offset_t offset,
2480 upl_size_t size,
0b4e3aa0
A
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
2d21ac55
A
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 */
0b4e3aa0
A
2540kern_return_t
2541ubc_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
2d21ac55
A
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 */
0b4e3aa0
A
2571upl_page_info_t *
2572ubc_upl_pageinfo(
2573 upl_t upl)
2574{
2575 return (UPL_GET_INTERNAL_PAGE_LIST(upl));
2576}
91447636 2577
91447636
A
2578
2579int
2d21ac55 2580UBCINFOEXISTS(struct vnode * vp)
91447636 2581{
2d21ac55 2582 return((vp) && ((vp)->v_type == VREG) && ((vp)->v_ubcinfo != UBC_INFO_NULL));
91447636
A
2583}
2584
2d21ac55 2585
316670eb
A
2586void
2587ubc_upl_range_needed(
2588 upl_t upl,
2589 int index,
2590 int count)
2591{
2592 upl_range_needed(upl, index, count);
2593}
2594
2595
2d21ac55
A
2596/*
2597 * CODE SIGNING
2598 */
593a1d5f 2599#define CS_BLOB_PAGEABLE 0
2d21ac55
A
2600static volatile SInt32 cs_blob_size = 0;
2601static volatile SInt32 cs_blob_count = 0;
2602static SInt32 cs_blob_size_peak = 0;
2603static UInt32 cs_blob_size_max = 0;
2604static SInt32 cs_blob_count_peak = 0;
2d21ac55
A
2605
2606int cs_validation = 1;
2607
316670eb 2608#ifndef SECURE_KERNEL
6d2010ae 2609SYSCTL_INT(_vm, OID_AUTO, cs_validation, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_validation, 0, "Do validate code signatures");
316670eb 2610#endif
6d2010ae
A
2611SYSCTL_INT(_vm, OID_AUTO, cs_blob_count, CTLFLAG_RD | CTLFLAG_LOCKED, (int *)(uintptr_t)&cs_blob_count, 0, "Current number of code signature blobs");
2612SYSCTL_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");
2613SYSCTL_INT(_vm, OID_AUTO, cs_blob_count_peak, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_blob_count_peak, 0, "Peak number of code signature blobs");
2614SYSCTL_INT(_vm, OID_AUTO, cs_blob_size_peak, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_blob_size_peak, 0, "Peak size of code signature blobs");
2615SYSCTL_INT(_vm, OID_AUTO, cs_blob_size_max, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_blob_size_max, 0, "Size of biggest code signature blob");
2d21ac55 2616
39236c6e 2617
593a1d5f
A
2618kern_return_t
2619ubc_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
2639void
2640ubc_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
39236c6e
A
2651int
2652ubc_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;
2749out:
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
2d21ac55
A
2763int
2764ubc_cs_blob_add(
2765 struct vnode *vp,
2766 cpu_type_t cputype,
2767 off_t base_offset,
2768 vm_address_t addr,
39236c6e 2769 off_t blob_offset,
2d21ac55 2770 vm_size_t size)
91447636 2771{
2d21ac55
A
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
2782 blob_handle = IPC_PORT_NULL;
2783
2784 blob = (struct cs_blob *) kalloc(sizeof (struct cs_blob));
2785 if (blob == NULL) {
2786 return ENOMEM;
2787 }
2788
593a1d5f 2789#if CS_BLOB_PAGEABLE
2d21ac55
A
2790 /* get a memory entry on the blob */
2791 blob_size = (memory_object_size_t) size;
2792 kr = mach_make_memory_entry_64(kernel_map,
2793 &blob_size,
2794 addr,
2795 VM_PROT_READ,
2796 &blob_handle,
2797 IPC_PORT_NULL);
2798 if (kr != KERN_SUCCESS) {
2799 error = ENOMEM;
2800 goto out;
2801 }
2802 if (memory_object_round_page(blob_size) !=
2803 (memory_object_size_t) round_page(size)) {
b0d623f7
A
2804 printf("ubc_cs_blob_add: size mismatch 0x%llx 0x%lx !?\n",
2805 blob_size, (size_t)size);
2806 panic("XXX FBDP size mismatch 0x%llx 0x%lx\n", blob_size, (size_t)size);
2d21ac55
A
2807 error = EINVAL;
2808 goto out;
2809 }
593a1d5f
A
2810#else
2811 blob_size = (memory_object_size_t) size;
2812 blob_handle = IPC_PORT_NULL;
2813#endif
2d21ac55
A
2814
2815 /* fill in the new blob */
2816 blob->csb_cpu_type = cputype;
39236c6e 2817 blob->csb_sigpup = 0;
2d21ac55 2818 blob->csb_base_offset = base_offset;
39236c6e 2819 blob->csb_blob_offset = blob_offset;
2d21ac55
A
2820 blob->csb_mem_size = size;
2821 blob->csb_mem_offset = 0;
2822 blob->csb_mem_handle = blob_handle;
2823 blob->csb_mem_kaddr = addr;
39236c6e 2824 blob->csb_flags = 0;
2d21ac55
A
2825
2826 /*
2827 * Validate the blob's contents
2828 */
39236c6e
A
2829
2830 error = cs_validate_csblob((const uint8_t *)addr, size, &cd);
2831 if (error) {
2832 if (cs_debug)
2833 printf("CODESIGNING: csblob invalid: %d\n", error);
2d21ac55
A
2834 blob->csb_flags = 0;
2835 blob->csb_start_offset = 0;
2836 blob->csb_end_offset = 0;
39236c6e
A
2837 memset(blob->csb_sha1, 0, SHA1_RESULTLEN);
2838 /* let the vnode checker determine if the signature is valid or not */
2d21ac55 2839 } else {
6d2010ae 2840 const unsigned char *sha1_base;
2d21ac55
A
2841 int sha1_size;
2842
39236c6e 2843 blob->csb_flags = (ntohl(cd->flags) & CS_ALLOWED_MACHO) | CS_VALID;
2d21ac55 2844 blob->csb_end_offset = round_page(ntohl(cd->codeLimit));
39236c6e
A
2845 if((ntohl(cd->version) >= CS_SUPPORTSSCATTER) && (ntohl(cd->scatterOffset))) {
2846 const SC_Scatter *scatter = (const SC_Scatter*)
b0d623f7
A
2847 ((const char*)cd + ntohl(cd->scatterOffset));
2848 blob->csb_start_offset = ntohl(scatter->base) * PAGE_SIZE;
2849 } else {
2850 blob->csb_start_offset = (blob->csb_end_offset -
2851 (ntohl(cd->nCodeSlots) * PAGE_SIZE));
2852 }
2d21ac55
A
2853 /* compute the blob's SHA1 hash */
2854 sha1_base = (const unsigned char *) cd;
2855 sha1_size = ntohl(cd->length);
2856 SHA1Init(&sha1ctxt);
2857 SHA1Update(&sha1ctxt, sha1_base, sha1_size);
2858 SHA1Final(blob->csb_sha1, &sha1ctxt);
2859 }
2860
593a1d5f
A
2861 /*
2862 * Let policy module check whether the blob's signature is accepted.
2863 */
2864#if CONFIG_MACF
39236c6e 2865 error = mac_vnode_check_signature(vp, base_offset, blob->csb_sha1, (void*)addr, size);
593a1d5f
A
2866 if (error)
2867 goto out;
2868#endif
2869
2d21ac55
A
2870 /*
2871 * Validate the blob's coverage
2872 */
2873 blob_start_offset = blob->csb_base_offset + blob->csb_start_offset;
2874 blob_end_offset = blob->csb_base_offset + blob->csb_end_offset;
2875
cf7d32b8
A
2876 if (blob_start_offset >= blob_end_offset ||
2877 blob_start_offset < 0 ||
2878 blob_end_offset <= 0) {
2d21ac55
A
2879 /* reject empty or backwards blob */
2880 error = EINVAL;
2881 goto out;
2882 }
2883
2884 vnode_lock(vp);
2885 if (! UBCINFOEXISTS(vp)) {
2886 vnode_unlock(vp);
2887 error = ENOENT;
2888 goto out;
2889 }
2890 uip = vp->v_ubcinfo;
2891
2892 /* check if this new blob overlaps with an existing blob */
2893 for (oblob = uip->cs_blobs;
2894 oblob != NULL;
2895 oblob = oblob->csb_next) {
2896 off_t oblob_start_offset, oblob_end_offset;
2897
2898 oblob_start_offset = (oblob->csb_base_offset +
2899 oblob->csb_start_offset);
2900 oblob_end_offset = (oblob->csb_base_offset +
2901 oblob->csb_end_offset);
2902 if (blob_start_offset >= oblob_end_offset ||
2903 blob_end_offset <= oblob_start_offset) {
2904 /* no conflict with this existing blob */
2905 } else {
2906 /* conflict ! */
2907 if (blob_start_offset == oblob_start_offset &&
2908 blob_end_offset == oblob_end_offset &&
2909 blob->csb_mem_size == oblob->csb_mem_size &&
2910 blob->csb_flags == oblob->csb_flags &&
2911 (blob->csb_cpu_type == CPU_TYPE_ANY ||
2912 oblob->csb_cpu_type == CPU_TYPE_ANY ||
2913 blob->csb_cpu_type == oblob->csb_cpu_type) &&
2914 !bcmp(blob->csb_sha1,
2915 oblob->csb_sha1,
2916 SHA1_RESULTLEN)) {
2917 /*
2918 * We already have this blob:
2919 * we'll return success but
2920 * throw away the new blob.
2921 */
2922 if (oblob->csb_cpu_type == CPU_TYPE_ANY) {
2923 /*
2924 * The old blob matches this one
2925 * but doesn't have any CPU type.
2926 * Update it with whatever the caller
2927 * provided this time.
2928 */
2929 oblob->csb_cpu_type = cputype;
2930 }
39236c6e
A
2931 /*
2932 * If the same blob moved around in the Mach-O, we
2933 * want to remember the new blob offset to avoid
2934 * coming back here again and again.
2935 */
2936 oblob->csb_blob_offset = blob_offset;
2937
2d21ac55
A
2938 vnode_unlock(vp);
2939 error = EAGAIN;
2940 goto out;
2941 } else {
2942 /* different blob: reject the new one */
39236c6e
A
2943 char pathbuf[MAXPATHLEN];
2944 char new_sha1_str[2*SHA1_RESULTLEN+1];
2945 char old_sha1_str[2*SHA1_RESULTLEN+1];
2946 char arch_str[20];
2947 const char *pathp = "?unknown";
2948 int pblen = sizeof(pathbuf);
2949 if (vn_getpath(vp, pathbuf, &pblen) == 0) {
2950 /* pblen == strlen(pathbuf) + 1. Assume strlen(pathbuf) > 0 */
2951 for (pathp = pathbuf + pblen - 2; pathp > pathbuf && pathp[-1] != '/'; pathp--) ;
2952 }
2953 snprintf(arch_str, sizeof(arch_str), "%x", cputype);
2954 hex_str(oblob->csb_sha1, SHA1_RESULTLEN, old_sha1_str);
2955 hex_str(blob->csb_sha1, SHA1_RESULTLEN, new_sha1_str);
2956 kern_asl_msg(LOG_NOTICE, "messagetracer",
2957 6,
2958 "com.apple.message.domain", "com.apple.kernel.cs.replace",
2959 "com.apple.message.signature", pathp,
2960 "com.apple.message.signature2", arch_str,
2961 "com.apple.message.signature3", old_sha1_str,
2962 "com.apple.message.result", new_sha1_str,
2963 "com.apple.message.summarize", "YES",
2964 NULL
2965 );
2966 printf("CODESIGNING: rejected new signature for architecture %d of file %s\n",
2967 cputype, pathbuf);
2d21ac55
A
2968 vnode_unlock(vp);
2969 error = EALREADY;
2970 goto out;
2971 }
2972 }
2973
2974 }
2975
2d21ac55
A
2976 /* mark this vnode's VM object as having "signed pages" */
2977 kr = memory_object_signed(uip->ui_control, TRUE);
2978 if (kr != KERN_SUCCESS) {
2979 vnode_unlock(vp);
2980 error = ENOENT;
2981 goto out;
2982 }
2983
2984 /*
2985 * Add this blob to the list of blobs for this vnode.
2986 * We always add at the front of the list and we never remove a
2987 * blob from the list, so ubc_cs_get_blobs() can return whatever
2988 * the top of the list was and that list will remain valid
2989 * while we validate a page, even after we release the vnode's lock.
2990 */
2991 blob->csb_next = uip->cs_blobs;
2992 uip->cs_blobs = blob;
2993
2994 OSAddAtomic(+1, &cs_blob_count);
2995 if (cs_blob_count > cs_blob_count_peak) {
2996 cs_blob_count_peak = cs_blob_count; /* XXX atomic ? */
2997 }
b0d623f7
A
2998 OSAddAtomic((SInt32) +blob->csb_mem_size, &cs_blob_size);
2999 if ((SInt32) cs_blob_size > cs_blob_size_peak) {
3000 cs_blob_size_peak = (SInt32) cs_blob_size; /* XXX atomic ? */
2d21ac55 3001 }
b0d623f7
A
3002 if ((UInt32) blob->csb_mem_size > cs_blob_size_max) {
3003 cs_blob_size_max = (UInt32) blob->csb_mem_size;
2d21ac55
A
3004 }
3005
c331a0be 3006 if (cs_debug > 1) {
2d21ac55 3007 proc_t p;
39236c6e 3008 const char *name = vnode_getname_printable(vp);
2d21ac55
A
3009 p = current_proc();
3010 printf("CODE SIGNING: proc %d(%s) "
3011 "loaded %s signatures for file (%s) "
3012 "range 0x%llx:0x%llx flags 0x%x\n",
3013 p->p_pid, p->p_comm,
3014 blob->csb_cpu_type == -1 ? "detached" : "embedded",
39236c6e 3015 name,
2d21ac55
A
3016 blob->csb_base_offset + blob->csb_start_offset,
3017 blob->csb_base_offset + blob->csb_end_offset,
3018 blob->csb_flags);
39236c6e 3019 vnode_putname_printable(name);
2d21ac55
A
3020 }
3021
2d21ac55
A
3022 vnode_unlock(vp);
3023
3024 error = 0; /* success ! */
3025
3026out:
3027 if (error) {
3028 /* we failed; release what we allocated */
3029 if (blob) {
3030 kfree(blob, sizeof (*blob));
3031 blob = NULL;
3032 }
3033 if (blob_handle != IPC_PORT_NULL) {
3034 mach_memory_entry_port_release(blob_handle);
3035 blob_handle = IPC_PORT_NULL;
3036 }
2d21ac55
A
3037 }
3038
3039 if (error == EAGAIN) {
3040 /*
3041 * See above: error is EAGAIN if we were asked
3042 * to add an existing blob again. We cleaned the new
3043 * blob and we want to return success.
3044 */
3045 error = 0;
3046 /*
3047 * Since we're not failing, consume the data we received.
3048 */
593a1d5f 3049 ubc_cs_blob_deallocate(addr, size);
2d21ac55
A
3050 }
3051
3052 return error;
91447636
A
3053}
3054
3055
2d21ac55
A
3056struct cs_blob *
3057ubc_cs_blob_get(
3058 struct vnode *vp,
3059 cpu_type_t cputype,
3060 off_t offset)
91447636 3061{
2d21ac55
A
3062 struct ubc_info *uip;
3063 struct cs_blob *blob;
3064 off_t offset_in_blob;
3065
3066 vnode_lock_spin(vp);
3067
3068 if (! UBCINFOEXISTS(vp)) {
3069 blob = NULL;
3070 goto out;
3071 }
3072
3073 uip = vp->v_ubcinfo;
3074 for (blob = uip->cs_blobs;
3075 blob != NULL;
3076 blob = blob->csb_next) {
3077 if (cputype != -1 && blob->csb_cpu_type == cputype) {
3078 break;
3079 }
3080 if (offset != -1) {
3081 offset_in_blob = offset - blob->csb_base_offset;
3082 if (offset_in_blob >= blob->csb_start_offset &&
3083 offset_in_blob < blob->csb_end_offset) {
3084 /* our offset is covered by this blob */
3085 break;
3086 }
3087 }
3088 }
3089
39236c6e
A
3090 if (cs_debug && blob != NULL && blob->csb_sigpup)
3091 printf("found sig pup blob\n");
2d21ac55
A
3092out:
3093 vnode_unlock(vp);
3094
3095 return blob;
91447636 3096}
2d21ac55
A
3097
3098static void
3099ubc_cs_free(
3100 struct ubc_info *uip)
91447636 3101{
2d21ac55
A
3102 struct cs_blob *blob, *next_blob;
3103
3104 for (blob = uip->cs_blobs;
3105 blob != NULL;
3106 blob = next_blob) {
3107 next_blob = blob->csb_next;
39236c6e 3108 if (blob->csb_mem_kaddr != 0 && !blob->csb_sigpup) {
593a1d5f
A
3109 ubc_cs_blob_deallocate(blob->csb_mem_kaddr,
3110 blob->csb_mem_size);
2d21ac55
A
3111 blob->csb_mem_kaddr = 0;
3112 }
593a1d5f
A
3113 if (blob->csb_mem_handle != IPC_PORT_NULL) {
3114 mach_memory_entry_port_release(blob->csb_mem_handle);
3115 }
2d21ac55
A
3116 blob->csb_mem_handle = IPC_PORT_NULL;
3117 OSAddAtomic(-1, &cs_blob_count);
b0d623f7 3118 OSAddAtomic((SInt32) -blob->csb_mem_size, &cs_blob_size);
2d21ac55
A
3119 kfree(blob, sizeof (*blob));
3120 }
6d2010ae
A
3121#if CHECK_CS_VALIDATION_BITMAP
3122 ubc_cs_validation_bitmap_deallocate( uip->ui_vnode );
3123#endif
2d21ac55 3124 uip->cs_blobs = NULL;
91447636 3125}
2d21ac55
A
3126
3127struct cs_blob *
3128ubc_get_cs_blobs(
3129 struct vnode *vp)
91447636 3130{
2d21ac55
A
3131 struct ubc_info *uip;
3132 struct cs_blob *blobs;
3133
b0d623f7
A
3134 /*
3135 * No need to take the vnode lock here. The caller must be holding
3136 * a reference on the vnode (via a VM mapping or open file descriptor),
3137 * so the vnode will not go away. The ubc_info stays until the vnode
3138 * goes away. And we only modify "blobs" by adding to the head of the
3139 * list.
3140 * The ubc_info could go away entirely if the vnode gets reclaimed as
3141 * part of a forced unmount. In the case of a code-signature validation
3142 * during a page fault, the "paging_in_progress" reference on the VM
3143 * object guarantess that the vnode pager (and the ubc_info) won't go
3144 * away during the fault.
3145 * Other callers need to protect against vnode reclaim by holding the
3146 * vnode lock, for example.
3147 */
2d21ac55
A
3148
3149 if (! UBCINFOEXISTS(vp)) {
3150 blobs = NULL;
3151 goto out;
3152 }
3153
3154 uip = vp->v_ubcinfo;
3155 blobs = uip->cs_blobs;
3156
3157out:
2d21ac55 3158 return blobs;
91447636 3159}
2d21ac55
A
3160
3161unsigned long cs_validate_page_no_hash = 0;
3162unsigned long cs_validate_page_bad_hash = 0;
3163boolean_t
3164cs_validate_page(
3165 void *_blobs,
316670eb 3166 memory_object_t pager,
2d21ac55
A
3167 memory_object_offset_t page_offset,
3168 const void *data,
3169 boolean_t *tainted)
91447636 3170{
2d21ac55
A
3171 SHA1_CTX sha1ctxt;
3172 unsigned char actual_hash[SHA1_RESULTLEN];
3173 unsigned char expected_hash[SHA1_RESULTLEN];
3174 boolean_t found_hash;
3175 struct cs_blob *blobs, *blob;
3176 const CS_CodeDirectory *cd;
3177 const CS_SuperBlob *embedded;
2d21ac55
A
3178 const unsigned char *hash;
3179 boolean_t validated;
3180 off_t offset; /* page offset in the file */
3181 size_t size;
3182 off_t codeLimit = 0;
3183 char *lower_bound, *upper_bound;
3184 vm_offset_t kaddr, blob_addr;
3185 vm_size_t ksize;
3186 kern_return_t kr;
3187
3188 offset = page_offset;
3189
3190 /* retrieve the expected hash */
3191 found_hash = FALSE;
3192 blobs = (struct cs_blob *) _blobs;
3193
3194 for (blob = blobs;
3195 blob != NULL;
3196 blob = blob->csb_next) {
3197 offset = page_offset - blob->csb_base_offset;
3198 if (offset < blob->csb_start_offset ||
3199 offset >= blob->csb_end_offset) {
3200 /* our page is not covered by this blob */
3201 continue;
3202 }
3203
3204 /* map the blob in the kernel address space */
3205 kaddr = blob->csb_mem_kaddr;
3206 if (kaddr == 0) {
3207 ksize = (vm_size_t) (blob->csb_mem_size +
3208 blob->csb_mem_offset);
3209 kr = vm_map(kernel_map,
3210 &kaddr,
3211 ksize,
3212 0,
3213 VM_FLAGS_ANYWHERE,
3214 blob->csb_mem_handle,
3215 0,
3216 TRUE,
3217 VM_PROT_READ,
3218 VM_PROT_READ,
3219 VM_INHERIT_NONE);
3220 if (kr != KERN_SUCCESS) {
3221 /* XXX FBDP what to do !? */
3222 printf("cs_validate_page: failed to map blob, "
b0d623f7
A
3223 "size=0x%lx kr=0x%x\n",
3224 (size_t)blob->csb_mem_size, kr);
2d21ac55
A
3225 break;
3226 }
3227 }
39236c6e
A
3228 if (blob->csb_sigpup && cs_debug)
3229 printf("checking for a sigpup CD\n");
3230
2d21ac55
A
3231 blob_addr = kaddr + blob->csb_mem_offset;
3232
3233 lower_bound = CAST_DOWN(char *, blob_addr);
3234 upper_bound = lower_bound + blob->csb_mem_size;
3235
3236 embedded = (const CS_SuperBlob *) blob_addr;
3237 cd = findCodeDirectory(embedded, lower_bound, upper_bound);
3238 if (cd != NULL) {
3239 if (cd->pageSize != PAGE_SHIFT ||
39236c6e 3240 cd->hashType != CS_HASHTYPE_SHA1 ||
2d21ac55
A
3241 cd->hashSize != SHA1_RESULTLEN) {
3242 /* bogus blob ? */
39236c6e
A
3243 if (blob->csb_sigpup && cs_debug)
3244 printf("page foo bogus sigpup CD\n");
2d21ac55
A
3245 continue;
3246 }
b0d623f7 3247
2d21ac55 3248 offset = page_offset - blob->csb_base_offset;
b0d623f7
A
3249 if (offset < blob->csb_start_offset ||
3250 offset >= blob->csb_end_offset) {
2d21ac55 3251 /* our page is not covered by this blob */
39236c6e
A
3252 if (blob->csb_sigpup && cs_debug)
3253 printf("OOB sigpup CD\n");
2d21ac55
A
3254 continue;
3255 }
3256
3257 codeLimit = ntohl(cd->codeLimit);
39236c6e
A
3258 if (blob->csb_sigpup && cs_debug)
3259 printf("sigpup codesize %d\n", (int)codeLimit);
3260
3261 hash = hashes(cd, (unsigned)atop(offset),
2d21ac55 3262 lower_bound, upper_bound);
cf7d32b8
A
3263 if (hash != NULL) {
3264 bcopy(hash, expected_hash,
3265 sizeof (expected_hash));
3266 found_hash = TRUE;
39236c6e
A
3267 if (blob->csb_sigpup && cs_debug)
3268 printf("sigpup hash\n");
cf7d32b8 3269 }
2d21ac55 3270
2d21ac55 3271 break;
39236c6e
A
3272 } else {
3273 if (blob->csb_sigpup && cs_debug)
3274 printf("sig pup had no valid CD\n");
3275
2d21ac55
A
3276 }
3277 }
3278
3279 if (found_hash == FALSE) {
3280 /*
3281 * We can't verify this page because there is no signature
3282 * for it (yet). It's possible that this part of the object
3283 * is not signed, or that signatures for that part have not
3284 * been loaded yet.
3285 * Report that the page has not been validated and let the
3286 * caller decide if it wants to accept it or not.
3287 */
3288 cs_validate_page_no_hash++;
3289 if (cs_debug > 1) {
3290 printf("CODE SIGNING: cs_validate_page: "
316670eb
A
3291 "mobj %p off 0x%llx: no hash to validate !?\n",
3292 pager, page_offset);
2d21ac55
A
3293 }
3294 validated = FALSE;
3295 *tainted = FALSE;
3296 } else {
2d21ac55
A
3297
3298 size = PAGE_SIZE;
b0d623f7 3299 if ((off_t)(offset + size) > codeLimit) {
2d21ac55
A
3300 /* partial page at end of segment */
3301 assert(offset < codeLimit);
b0d623f7 3302 size = (size_t) (codeLimit & PAGE_MASK);
2d21ac55
A
3303 }
3304 /* compute the actual page's SHA1 hash */
3305 SHA1Init(&sha1ctxt);
593a1d5f 3306 SHA1UpdateUsePhysicalAddress(&sha1ctxt, data, size);
2d21ac55
A
3307 SHA1Final(actual_hash, &sha1ctxt);
3308
2d21ac55 3309 if (bcmp(expected_hash, actual_hash, SHA1_RESULTLEN) != 0) {
39236c6e
A
3310 char asha1_str[2*SHA1_RESULTLEN+1];
3311 char esha1_str[2*SHA1_RESULTLEN+1];
3312 hex_str(actual_hash, SHA1_RESULTLEN, asha1_str);
3313 hex_str(expected_hash, SHA1_RESULTLEN, esha1_str);
2d21ac55
A
3314 if (cs_debug) {
3315 printf("CODE SIGNING: cs_validate_page: "
39236c6e
A
3316 "mobj %p off 0x%llx size 0x%lx: actual %s expected %s\n",
3317 pager, page_offset, size, asha1_str, esha1_str);
2d21ac55
A
3318 }
3319 cs_validate_page_bad_hash++;
39236c6e
A
3320 if (!*tainted) {
3321 char page_offset_str[20];
3322 snprintf(page_offset_str, sizeof(page_offset_str), "%llx", page_offset);
3323 kern_asl_msg(LOG_NOTICE, "messagetracer",
3324 5,
3325 "com.apple.message.domain", "com.apple.kernel.cs.mismatch",
3326 "com.apple.message.signature", page_offset_str,
3327 "com.apple.message.signature2", asha1_str,
3328 "com.apple.message.signature3", esha1_str,
3329 "com.apple.message.summarize", "YES",
3330 NULL
3331 );
3332 }
2d21ac55
A
3333 *tainted = TRUE;
3334 } else {
39236c6e 3335 if (cs_debug > 10) {
2d21ac55 3336 printf("CODE SIGNING: cs_validate_page: "
316670eb
A
3337 "mobj %p off 0x%llx size 0x%lx: "
3338 "SHA1 OK\n",
3339 pager, page_offset, size);
2d21ac55
A
3340 }
3341 *tainted = FALSE;
3342 }
3343 validated = TRUE;
3344 }
3345
3346 return validated;
91447636
A
3347}
3348
2d21ac55
A
3349int
3350ubc_cs_getcdhash(
3351 vnode_t vp,
3352 off_t offset,
3353 unsigned char *cdhash)
3354{
b0d623f7
A
3355 struct cs_blob *blobs, *blob;
3356 off_t rel_offset;
3357 int ret;
3358
3359 vnode_lock(vp);
2d21ac55
A
3360
3361 blobs = ubc_get_cs_blobs(vp);
3362 for (blob = blobs;
3363 blob != NULL;
3364 blob = blob->csb_next) {
3365 /* compute offset relative to this blob */
3366 rel_offset = offset - blob->csb_base_offset;
3367 if (rel_offset >= blob->csb_start_offset &&
3368 rel_offset < blob->csb_end_offset) {
3369 /* this blob does cover our "offset" ! */
3370 break;
3371 }
3372 }
3373
3374 if (blob == NULL) {
3375 /* we didn't find a blob covering "offset" */
b0d623f7
A
3376 ret = EBADEXEC; /* XXX any better error ? */
3377 } else {
3378 /* get the SHA1 hash of that blob */
3379 bcopy(blob->csb_sha1, cdhash, sizeof (blob->csb_sha1));
3380 ret = 0;
2d21ac55
A
3381 }
3382
b0d623f7 3383 vnode_unlock(vp);
2d21ac55 3384
b0d623f7 3385 return ret;
2d21ac55 3386}
6d2010ae
A
3387
3388#if CHECK_CS_VALIDATION_BITMAP
3389#define stob(s) ((atop_64((s)) + 07) >> 3)
3390extern boolean_t root_fs_upgrade_try;
3391
3392/*
3393 * Should we use the code-sign bitmap to avoid repeated code-sign validation?
3394 * Depends:
3395 * a) Is the target vnode on the root filesystem?
3396 * b) Has someone tried to mount the root filesystem read-write?
3397 * If answers are (a) yes AND (b) no, then we can use the bitmap.
3398 */
3399#define USE_CODE_SIGN_BITMAP(vp) ( (vp != NULL) && (vp->v_mount != NULL) && (vp->v_mount->mnt_flag & MNT_ROOTFS) && !root_fs_upgrade_try)
3400kern_return_t
3401ubc_cs_validation_bitmap_allocate(
3402 vnode_t vp)
3403{
3404 kern_return_t kr = KERN_SUCCESS;
3405 struct ubc_info *uip;
3406 char *target_bitmap;
3407 vm_object_size_t bitmap_size;
3408
3409 if ( ! USE_CODE_SIGN_BITMAP(vp) || (! UBCINFOEXISTS(vp))) {
3410 kr = KERN_INVALID_ARGUMENT;
3411 } else {
3412 uip = vp->v_ubcinfo;
3413
3414 if ( uip->cs_valid_bitmap == NULL ) {
3415 bitmap_size = stob(uip->ui_size);
3416 target_bitmap = (char*) kalloc( (vm_size_t)bitmap_size );
3417 if (target_bitmap == 0) {
3418 kr = KERN_NO_SPACE;
3419 } else {
3420 kr = KERN_SUCCESS;
3421 }
3422 if( kr == KERN_SUCCESS ) {
3423 memset( target_bitmap, 0, (size_t)bitmap_size);
3424 uip->cs_valid_bitmap = (void*)target_bitmap;
3425 uip->cs_valid_bitmap_size = bitmap_size;
3426 }
3427 }
3428 }
3429 return kr;
3430}
3431
3432kern_return_t
3433ubc_cs_check_validation_bitmap (
3434 vnode_t vp,
3435 memory_object_offset_t offset,
3436 int optype)
3437{
3438 kern_return_t kr = KERN_SUCCESS;
3439
3440 if ( ! USE_CODE_SIGN_BITMAP(vp) || ! UBCINFOEXISTS(vp)) {
3441 kr = KERN_INVALID_ARGUMENT;
3442 } else {
3443 struct ubc_info *uip = vp->v_ubcinfo;
3444 char *target_bitmap = uip->cs_valid_bitmap;
3445
3446 if ( target_bitmap == NULL ) {
3447 kr = KERN_INVALID_ARGUMENT;
3448 } else {
3449 uint64_t bit, byte;
3450 bit = atop_64( offset );
3451 byte = bit >> 3;
3452
3453 if ( byte > uip->cs_valid_bitmap_size ) {
3454 kr = KERN_INVALID_ARGUMENT;
3455 } else {
3456
3457 if (optype == CS_BITMAP_SET) {
3458 target_bitmap[byte] |= (1 << (bit & 07));
3459 kr = KERN_SUCCESS;
3460 } else if (optype == CS_BITMAP_CLEAR) {
3461 target_bitmap[byte] &= ~(1 << (bit & 07));
3462 kr = KERN_SUCCESS;
3463 } else if (optype == CS_BITMAP_CHECK) {
3464 if ( target_bitmap[byte] & (1 << (bit & 07))) {
3465 kr = KERN_SUCCESS;
3466 } else {
3467 kr = KERN_FAILURE;
3468 }
3469 }
3470 }
3471 }
3472 }
3473 return kr;
3474}
3475
3476void
3477ubc_cs_validation_bitmap_deallocate(
3478 vnode_t vp)
3479{
3480 struct ubc_info *uip;
3481 void *target_bitmap;
3482 vm_object_size_t bitmap_size;
3483
3484 if ( UBCINFOEXISTS(vp)) {
3485 uip = vp->v_ubcinfo;
3486
3487 if ( (target_bitmap = uip->cs_valid_bitmap) != NULL ) {
3488 bitmap_size = uip->cs_valid_bitmap_size;
3489 kfree( target_bitmap, (vm_size_t) bitmap_size );
3490 uip->cs_valid_bitmap = NULL;
3491 }
3492 }
3493}
3494#else
3495kern_return_t ubc_cs_validation_bitmap_allocate(__unused vnode_t vp){
3496 return KERN_INVALID_ARGUMENT;
3497}
3498
3499kern_return_t ubc_cs_check_validation_bitmap(
3500 __unused struct vnode *vp,
3501 __unused memory_object_offset_t offset,
3502 __unused int optype){
3503
3504 return KERN_INVALID_ARGUMENT;
3505}
3506
3507void ubc_cs_validation_bitmap_deallocate(__unused vnode_t vp){
3508 return;
3509}
3510#endif /* CHECK_CS_VALIDATION_BITMAP */