]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/ubc_subr.c
xnu-3247.10.11.tar.gz
[apple/xnu.git] / bsd / kern / ubc_subr.c
CommitLineData
1c79356b 1/*
fe8ab488 2 * Copyright (c) 1999-2014 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>
fe8ab488
A
53#include <sys/codedir_internal.h>
54#include <sys/fsevents.h>
c18c124e 55#include <sys/fcntl.h>
1c79356b
A
56
57#include <mach/mach_types.h>
58#include <mach/memory_object_types.h>
91447636
A
59#include <mach/memory_object_control.h>
60#include <mach/vm_map.h>
b0d623f7 61#include <mach/mach_vm.h>
91447636 62#include <mach/upl.h>
1c79356b 63
91447636 64#include <kern/kern_types.h>
2d21ac55 65#include <kern/kalloc.h>
1c79356b 66#include <kern/zalloc.h>
13fec989 67#include <kern/thread.h>
91447636
A
68#include <vm/vm_kern.h>
69#include <vm/vm_protos.h> /* last */
1c79356b 70
2d21ac55 71#include <libkern/crypto/sha1.h>
3e170ce0 72#include <libkern/crypto/sha2.h>
39236c6e
A
73#include <libkern/libkern.h>
74
593a1d5f 75#include <security/mac_framework.h>
fe8ab488 76#include <stdbool.h>
593a1d5f 77
2d21ac55
A
78/* XXX These should be in a BSD accessible Mach header, but aren't. */
79extern kern_return_t memory_object_pages_resident(memory_object_control_t,
80 boolean_t *);
81extern kern_return_t memory_object_signed(memory_object_control_t control,
82 boolean_t is_signed);
6d2010ae 83extern boolean_t memory_object_is_slid(memory_object_control_t control);
39236c6e 84extern boolean_t memory_object_is_signed(memory_object_control_t);
6d2010ae 85
2d21ac55
A
86extern void Debugger(const char *message);
87
88
89/* XXX no one uses this interface! */
90kern_return_t ubc_page_op_with_control(
91 memory_object_control_t control,
92 off_t f_offset,
93 int ops,
94 ppnum_t *phys_entryp,
95 int *flagsp);
96
97
1c79356b
A
98#if DIAGNOSTIC
99#if defined(assert)
b0d623f7 100#undef assert
1c79356b
A
101#endif
102#define assert(cond) \
2d21ac55 103 ((void) ((cond) ? 0 : panic("Assert failed: %s", # cond)))
1c79356b
A
104#else
105#include <kern/assert.h>
106#endif /* DIAGNOSTIC */
107
2d21ac55 108static int ubc_info_init_internal(struct vnode *vp, int withfsize, off_t filesize);
0c530ab8 109static int ubc_umcallback(vnode_t, void *);
0c530ab8 110static int ubc_msync_internal(vnode_t, off_t, off_t, off_t *, int, int *);
2d21ac55 111static void ubc_cs_free(struct ubc_info *uip);
b4c24cb9 112
91447636 113struct zone *ubc_info_zone;
fe8ab488 114static uint32_t cs_blob_generation_count = 1;
2d21ac55
A
115
116/*
117 * CODESIGNING
118 * Routines to navigate code signing data structures in the kernel...
119 */
b0d623f7
A
120
121extern int cs_debug;
122
fe8ab488
A
123#define PAGE_SHIFT_4K (12)
124#define PAGE_SIZE_4K ((1<<PAGE_SHIFT_4K))
125#define PAGE_MASK_4K ((PAGE_SIZE_4K-1))
126#define round_page_4K(x) (((vm_offset_t)(x) + PAGE_MASK_4K) & ~((vm_offset_t)PAGE_MASK_4K))
127
2d21ac55
A
128static boolean_t
129cs_valid_range(
130 const void *start,
131 const void *end,
132 const void *lower_bound,
133 const void *upper_bound)
134{
135 if (upper_bound < lower_bound ||
136 end < start) {
137 return FALSE;
138 }
139
140 if (start < lower_bound ||
141 end > upper_bound) {
142 return FALSE;
143 }
144
145 return TRUE;
146}
147
3e170ce0
A
148typedef void (*cs_md_init)(void *ctx);
149typedef void (*cs_md_update)(void *ctx, const void *data, size_t size);
150typedef void (*cs_md_final)(void *hash, void *ctx);
151
152struct cs_hash {
153 uint8_t cs_type;
154 size_t cs_cd_size;
155 size_t cs_size;
156 size_t cs_digest_size;
157 cs_md_init cs_init;
158 cs_md_update cs_update;
159 cs_md_final cs_final;
160};
161
162static struct cs_hash cs_hash_sha1 = {
163 .cs_type = CS_HASHTYPE_SHA1,
164 .cs_cd_size = CS_SHA1_LEN,
165 .cs_size = CS_SHA1_LEN,
166 .cs_digest_size = SHA_DIGEST_LENGTH,
167 .cs_init = (cs_md_init)SHA1Init,
168 .cs_update = (cs_md_update)SHA1Update,
169 .cs_final = (cs_md_final)SHA1Final,
170};
171#if CRYPTO_SHA2
172static struct cs_hash cs_hash_sha256 = {
173 .cs_type = CS_HASHTYPE_SHA256,
174 .cs_cd_size = SHA256_DIGEST_LENGTH,
175 .cs_size = SHA256_DIGEST_LENGTH,
176 .cs_digest_size = SHA256_DIGEST_LENGTH,
177 .cs_init = (cs_md_init)SHA256_Init,
178 .cs_update = (cs_md_update)SHA256_Update,
179 .cs_final = (cs_md_final)SHA256_Final,
180};
181static struct cs_hash cs_hash_sha256_truncate = {
182 .cs_type = CS_HASHTYPE_SHA256_TRUNCATED,
183 .cs_cd_size = CS_SHA256_TRUNCATED_LEN,
184 .cs_size = CS_SHA256_TRUNCATED_LEN,
185 .cs_digest_size = SHA256_DIGEST_LENGTH,
186 .cs_init = (cs_md_init)SHA256_Init,
187 .cs_update = (cs_md_update)SHA256_Update,
188 .cs_final = (cs_md_final)SHA256_Final,
189};
190#endif
191
192static struct cs_hash *
193cs_find_md(uint8_t type)
194{
195 if (type == CS_HASHTYPE_SHA1) {
196 return &cs_hash_sha1;
197#if CRYPTO_SHA2
198 } else if (type == CS_HASHTYPE_SHA256) {
199 return &cs_hash_sha256;
200 } else if (type == CS_HASHTYPE_SHA256_TRUNCATED) {
201 return &cs_hash_sha256_truncate;
202#endif
203 }
204 return NULL;
205}
206
207union cs_hash_union {
208 SHA1_CTX sha1ctxt;
209 SHA256_CTX sha256ctx;
210};
211
212
2d21ac55
A
213/*
214 * Locate the CodeDirectory from an embedded signature blob
215 */
fe8ab488 216const
2d21ac55
A
217CS_CodeDirectory *findCodeDirectory(
218 const CS_SuperBlob *embedded,
3e170ce0
A
219 const char *lower_bound,
220 const char *upper_bound)
2d21ac55
A
221{
222 const CS_CodeDirectory *cd = NULL;
223
224 if (embedded &&
225 cs_valid_range(embedded, embedded + 1, lower_bound, upper_bound) &&
226 ntohl(embedded->magic) == CSMAGIC_EMBEDDED_SIGNATURE) {
227 const CS_BlobIndex *limit;
228 const CS_BlobIndex *p;
229
230 limit = &embedded->index[ntohl(embedded->count)];
231 if (!cs_valid_range(&embedded->index[0], limit,
232 lower_bound, upper_bound)) {
233 return NULL;
234 }
235 for (p = embedded->index; p < limit; ++p) {
236 if (ntohl(p->type) == CSSLOT_CODEDIRECTORY) {
237 const unsigned char *base;
238
239 base = (const unsigned char *)embedded;
240 cd = (const CS_CodeDirectory *)(base + ntohl(p->offset));
241 break;
242 }
243 }
244 } else {
245 /*
246 * Detached signatures come as a bare CS_CodeDirectory,
247 * without a blob.
248 */
249 cd = (const CS_CodeDirectory *) embedded;
250 }
b0d623f7 251
2d21ac55
A
252 if (cd &&
253 cs_valid_range(cd, cd + 1, lower_bound, upper_bound) &&
254 cs_valid_range(cd, (const char *) cd + ntohl(cd->length),
255 lower_bound, upper_bound) &&
cf7d32b8
A
256 cs_valid_range(cd, (const char *) cd + ntohl(cd->hashOffset),
257 lower_bound, upper_bound) &&
258 cs_valid_range(cd, (const char *) cd +
259 ntohl(cd->hashOffset) +
260 (ntohl(cd->nCodeSlots) * SHA1_RESULTLEN),
261 lower_bound, upper_bound) &&
262
2d21ac55
A
263 ntohl(cd->magic) == CSMAGIC_CODEDIRECTORY) {
264 return cd;
265 }
266
267 // not found or not a valid code directory
268 return NULL;
269}
270
271
272/*
273 * Locating a page hash
274 */
275static const unsigned char *
276hashes(
277 const CS_CodeDirectory *cd,
3e170ce0
A
278 uint32_t page,
279 size_t hash_len,
280 const char *lower_bound,
281 const char *upper_bound)
2d21ac55
A
282{
283 const unsigned char *base, *top, *hash;
b0d623f7 284 uint32_t nCodeSlots = ntohl(cd->nCodeSlots);
2d21ac55
A
285
286 assert(cs_valid_range(cd, cd + 1, lower_bound, upper_bound));
287
39236c6e 288 if((ntohl(cd->version) >= CS_SUPPORTSSCATTER) && (ntohl(cd->scatterOffset))) {
b0d623f7 289 /* Get first scatter struct */
39236c6e 290 const SC_Scatter *scatter = (const SC_Scatter*)
b0d623f7
A
291 ((const char*)cd + ntohl(cd->scatterOffset));
292 uint32_t hashindex=0, scount, sbase=0;
293 /* iterate all scatter structs */
294 do {
295 if((const char*)scatter > (const char*)cd + ntohl(cd->length)) {
296 if(cs_debug) {
297 printf("CODE SIGNING: Scatter extends past Code Directory\n");
298 }
299 return NULL;
300 }
301
302 scount = ntohl(scatter->count);
303 uint32_t new_base = ntohl(scatter->base);
304
305 /* last scatter? */
306 if (scount == 0) {
307 return NULL;
308 }
309
310 if((hashindex > 0) && (new_base <= sbase)) {
311 if(cs_debug) {
312 printf("CODE SIGNING: unordered Scatter, prev base %d, cur base %d\n",
313 sbase, new_base);
314 }
315 return NULL; /* unordered scatter array */
316 }
317 sbase = new_base;
318
319 /* this scatter beyond page we're looking for? */
320 if (sbase > page) {
321 return NULL;
322 }
323
324 if (sbase+scount >= page) {
325 /* Found the scatter struct that is
326 * referencing our page */
327
328 /* base = address of first hash covered by scatter */
329 base = (const unsigned char *)cd + ntohl(cd->hashOffset) +
3e170ce0 330 hashindex * hash_len;
b0d623f7 331 /* top = address of first hash after this scatter */
3e170ce0 332 top = base + scount * hash_len;
b0d623f7
A
333 if (!cs_valid_range(base, top, lower_bound,
334 upper_bound) ||
335 hashindex > nCodeSlots) {
336 return NULL;
337 }
338
339 break;
340 }
341
342 /* this scatter struct is before the page we're looking
343 * for. Iterate. */
344 hashindex+=scount;
345 scatter++;
346 } while(1);
347
3e170ce0 348 hash = base + (page - sbase) * hash_len;
b0d623f7
A
349 } else {
350 base = (const unsigned char *)cd + ntohl(cd->hashOffset);
3e170ce0 351 top = base + nCodeSlots * hash_len;
b0d623f7
A
352 if (!cs_valid_range(base, top, lower_bound, upper_bound) ||
353 page > nCodeSlots) {
354 return NULL;
355 }
356 assert(page < nCodeSlots);
2d21ac55 357
3e170ce0 358 hash = base + page * hash_len;
b0d623f7
A
359 }
360
3e170ce0 361 if (!cs_valid_range(hash, hash + hash_len,
2d21ac55
A
362 lower_bound, upper_bound)) {
363 hash = NULL;
364 }
365
366 return hash;
367}
39236c6e
A
368
369/*
370 * cs_validate_codedirectory
371 *
372 * Validate that pointers inside the code directory to make sure that
373 * all offsets and lengths are constrained within the buffer.
374 *
375 * Parameters: cd Pointer to code directory buffer
376 * length Length of buffer
377 *
378 * Returns: 0 Success
379 * EBADEXEC Invalid code signature
380 */
381
382static int
383cs_validate_codedirectory(const CS_CodeDirectory *cd, size_t length)
384{
3e170ce0 385 struct cs_hash *hashtype;
39236c6e
A
386
387 if (length < sizeof(*cd))
388 return EBADEXEC;
389 if (ntohl(cd->magic) != CSMAGIC_CODEDIRECTORY)
390 return EBADEXEC;
fe8ab488 391 if (cd->pageSize != PAGE_SHIFT_4K)
39236c6e 392 return EBADEXEC;
3e170ce0
A
393 hashtype = cs_find_md(cd->hashType);
394 if (hashtype == NULL)
39236c6e
A
395 return EBADEXEC;
396
3e170ce0
A
397 if (cd->hashSize != hashtype->cs_cd_size)
398 return EBADEXEC;
399
400
39236c6e
A
401 if (length < ntohl(cd->hashOffset))
402 return EBADEXEC;
403
404 /* check that nSpecialSlots fits in the buffer in front of hashOffset */
3e170ce0 405 if (ntohl(cd->hashOffset) / hashtype->cs_size < ntohl(cd->nSpecialSlots))
39236c6e
A
406 return EBADEXEC;
407
408 /* check that codeslots fits in the buffer */
3e170ce0 409 if ((length - ntohl(cd->hashOffset)) / hashtype->cs_size < ntohl(cd->nCodeSlots))
39236c6e
A
410 return EBADEXEC;
411
412 if (ntohl(cd->version) >= CS_SUPPORTSSCATTER && cd->scatterOffset) {
413
414 if (length < ntohl(cd->scatterOffset))
415 return EBADEXEC;
416
3e170ce0
A
417 const SC_Scatter *scatter = (const SC_Scatter *)
418 (((const uint8_t *)cd) + ntohl(cd->scatterOffset));
39236c6e
A
419 uint32_t nPages = 0;
420
421 /*
422 * Check each scatter buffer, since we don't know the
423 * length of the scatter buffer array, we have to
424 * check each entry.
425 */
426 while(1) {
427 /* check that the end of each scatter buffer in within the length */
428 if (((const uint8_t *)scatter) + sizeof(scatter[0]) > (const uint8_t *)cd + length)
429 return EBADEXEC;
430 uint32_t scount = ntohl(scatter->count);
431 if (scount == 0)
432 break;
433 if (nPages + scount < nPages)
434 return EBADEXEC;
435 nPages += scount;
436 scatter++;
437
438 /* XXX check that basees doesn't overlap */
439 /* XXX check that targetOffset doesn't overlap */
440 }
441#if 0 /* rdar://12579439 */
442 if (nPages != ntohl(cd->nCodeSlots))
443 return EBADEXEC;
444#endif
445 }
446
447 if (length < ntohl(cd->identOffset))
448 return EBADEXEC;
449
450 /* identifier is NUL terminated string */
451 if (cd->identOffset) {
3e170ce0 452 const uint8_t *ptr = (const uint8_t *)cd + ntohl(cd->identOffset);
39236c6e
A
453 if (memchr(ptr, 0, length - ntohl(cd->identOffset)) == NULL)
454 return EBADEXEC;
455 }
456
fe8ab488
A
457 /* team identifier is NULL terminated string */
458 if (ntohl(cd->version) >= CS_SUPPORTSTEAMID && ntohl(cd->teamOffset)) {
459 if (length < ntohl(cd->teamOffset))
460 return EBADEXEC;
461
3e170ce0 462 const uint8_t *ptr = (const uint8_t *)cd + ntohl(cd->teamOffset);
fe8ab488
A
463 if (memchr(ptr, 0, length - ntohl(cd->teamOffset)) == NULL)
464 return EBADEXEC;
465 }
466
39236c6e
A
467 return 0;
468}
469
470/*
471 *
472 */
473
474static int
475cs_validate_blob(const CS_GenericBlob *blob, size_t length)
476{
477 if (length < sizeof(CS_GenericBlob) || length < ntohl(blob->length))
478 return EBADEXEC;
479 return 0;
480}
481
482/*
483 * cs_validate_csblob
484 *
485 * Validate that superblob/embedded code directory to make sure that
486 * all internal pointers are valid.
487 *
488 * Will validate both a superblob csblob and a "raw" code directory.
489 *
490 *
491 * Parameters: buffer Pointer to code signature
492 * length Length of buffer
493 * rcd returns pointer to code directory
494 *
495 * Returns: 0 Success
496 * EBADEXEC Invalid code signature
497 */
498
499static int
500cs_validate_csblob(const uint8_t *addr, size_t length,
501 const CS_CodeDirectory **rcd)
502{
3e170ce0 503 const CS_GenericBlob *blob = (const CS_GenericBlob *)(const void *)addr;
39236c6e
A
504 int error;
505
506 *rcd = NULL;
507
508 error = cs_validate_blob(blob, length);
509 if (error)
510 return error;
511
512 length = ntohl(blob->length);
513
514 if (ntohl(blob->magic) == CSMAGIC_EMBEDDED_SIGNATURE) {
515 const CS_SuperBlob *sb = (const CS_SuperBlob *)blob;
516 uint32_t n, count = ntohl(sb->count);
517
518 if (length < sizeof(CS_SuperBlob))
519 return EBADEXEC;
520
521 /* check that the array of BlobIndex fits in the rest of the data */
522 if ((length - sizeof(CS_SuperBlob)) / sizeof(CS_BlobIndex) < count)
523 return EBADEXEC;
524
525 /* now check each BlobIndex */
526 for (n = 0; n < count; n++) {
527 const CS_BlobIndex *blobIndex = &sb->index[n];
528 if (length < ntohl(blobIndex->offset))
529 return EBADEXEC;
530
531 const CS_GenericBlob *subBlob =
3e170ce0 532 (const CS_GenericBlob *)(const void *)(addr + ntohl(blobIndex->offset));
39236c6e
A
533
534 size_t subLength = length - ntohl(blobIndex->offset);
535
536 if ((error = cs_validate_blob(subBlob, subLength)) != 0)
537 return error;
538 subLength = ntohl(subBlob->length);
539
540 /* extra validation for CDs, that is also returned */
541 if (ntohl(blobIndex->type) == CSSLOT_CODEDIRECTORY) {
542 const CS_CodeDirectory *cd = (const CS_CodeDirectory *)subBlob;
543 if ((error = cs_validate_codedirectory(cd, subLength)) != 0)
544 return error;
545 *rcd = cd;
546 }
547 }
548
549 } else if (ntohl(blob->magic) == CSMAGIC_CODEDIRECTORY) {
550
3e170ce0 551 if ((error = cs_validate_codedirectory((const CS_CodeDirectory *)(const void *)addr, length)) != 0)
39236c6e
A
552 return error;
553 *rcd = (const CS_CodeDirectory *)blob;
554 } else {
555 return EBADEXEC;
556 }
557
558 if (*rcd == NULL)
559 return EBADEXEC;
560
561 return 0;
562}
563
564/*
565 * cs_find_blob_bytes
566 *
567 * Find an blob from the superblob/code directory. The blob must have
568 * been been validated by cs_validate_csblob() before calling
3e170ce0 569 * this. Use csblob_find_blob() instead.
39236c6e
A
570 *
571 * Will also find a "raw" code directory if its stored as well as
572 * searching the superblob.
573 *
574 * Parameters: buffer Pointer to code signature
575 * length Length of buffer
576 * type type of blob to find
577 * magic the magic number for that blob
578 *
579 * Returns: pointer Success
580 * NULL Buffer not found
581 */
582
3e170ce0
A
583const CS_GenericBlob *
584csblob_find_blob_bytes(const uint8_t *addr, size_t length, uint32_t type, uint32_t magic)
39236c6e 585{
3e170ce0 586 const CS_GenericBlob *blob = (const CS_GenericBlob *)(const void *)addr;
39236c6e
A
587
588 if (ntohl(blob->magic) == CSMAGIC_EMBEDDED_SIGNATURE) {
589 const CS_SuperBlob *sb = (const CS_SuperBlob *)blob;
590 size_t n, count = ntohl(sb->count);
591
592 for (n = 0; n < count; n++) {
593 if (ntohl(sb->index[n].type) != type)
594 continue;
595 uint32_t offset = ntohl(sb->index[n].offset);
596 if (length - sizeof(const CS_GenericBlob) < offset)
597 return NULL;
3e170ce0 598 blob = (const CS_GenericBlob *)(const void *)(addr + offset);
39236c6e
A
599 if (ntohl(blob->magic) != magic)
600 continue;
601 return blob;
602 }
603 } else if (type == CSSLOT_CODEDIRECTORY
604 && ntohl(blob->magic) == CSMAGIC_CODEDIRECTORY
605 && magic == CSMAGIC_CODEDIRECTORY)
606 return blob;
607 return NULL;
608}
609
610
fe8ab488 611const CS_GenericBlob *
3e170ce0 612csblob_find_blob(struct cs_blob *csblob, uint32_t type, uint32_t magic)
39236c6e
A
613{
614 if ((csblob->csb_flags & CS_VALID) == 0)
615 return NULL;
3e170ce0 616 return csblob_find_blob_bytes((const uint8_t *)csblob->csb_mem_kaddr, csblob->csb_mem_size, type, magic);
39236c6e
A
617}
618
619static const uint8_t *
3e170ce0 620find_special_slot(const CS_CodeDirectory *cd, size_t slotsize, uint32_t slot)
39236c6e
A
621{
622 /* there is no zero special slot since that is the first code slot */
623 if (ntohl(cd->nSpecialSlots) < slot || slot == 0)
624 return NULL;
625
3e170ce0 626 return ((const uint8_t *)cd + ntohl(cd->hashOffset) - (slotsize * slot));
39236c6e
A
627}
628
3e170ce0 629static uint8_t cshash_zero[CS_HASH_MAX_SIZE] = { 0 };
39236c6e 630
6d2010ae 631int
3e170ce0 632csblob_get_entitlements(struct cs_blob *csblob, void **out_start, size_t *out_length)
6d2010ae 633{
3e170ce0 634 uint8_t computed_hash[CS_HASH_MAX_SIZE];
39236c6e
A
635 const CS_GenericBlob *entitlements;
636 const CS_CodeDirectory *code_dir;
39236c6e 637 const uint8_t *embedded_hash;
3e170ce0 638 union cs_hash_union context;
39236c6e
A
639
640 *out_start = NULL;
641 *out_length = 0;
642
3e170ce0
A
643 if (csblob->csb_hashtype == NULL || csblob->csb_hashtype->cs_digest_size > sizeof(computed_hash))
644 return EBADEXEC;
39236c6e 645
3e170ce0 646 if ((code_dir = (const CS_CodeDirectory *)csblob_find_blob(csblob, CSSLOT_CODEDIRECTORY, CSMAGIC_CODEDIRECTORY)) == NULL)
39236c6e
A
647 return 0;
648
3e170ce0
A
649 entitlements = csblob_find_blob(csblob, CSSLOT_ENTITLEMENTS, CSMAGIC_EMBEDDED_ENTITLEMENTS);
650 embedded_hash = find_special_slot(code_dir, csblob->csb_hashtype->cs_size, CSSLOT_ENTITLEMENTS);
39236c6e
A
651
652 if (embedded_hash == NULL) {
653 if (entitlements)
654 return EBADEXEC;
655 return 0;
3e170ce0 656 } else if (entitlements == NULL && memcmp(embedded_hash, cshash_zero, csblob->csb_hashtype->cs_size) != 0) {
39236c6e 657 return EBADEXEC;
6d2010ae 658 }
39236c6e 659
3e170ce0
A
660 csblob->csb_hashtype->cs_init(&context);
661 csblob->csb_hashtype->cs_update(&context, entitlements, ntohl(entitlements->length));
662 csblob->csb_hashtype->cs_final(computed_hash, &context);
663
664 if (memcmp(computed_hash, embedded_hash, csblob->csb_hashtype->cs_size) != 0)
39236c6e
A
665 return EBADEXEC;
666
3e170ce0 667 *out_start = __DECONST(void *, entitlements);
39236c6e
A
668 *out_length = ntohl(entitlements->length);
669
670 return 0;
671}
672
6d2010ae 673/*
3e170ce0
A
674 * CODESIGNING
675 * End of routines to navigate code signing data structures in the kernel.
6d2010ae
A
676 */
677
678
2d21ac55 679
1c79356b 680/*
2d21ac55
A
681 * ubc_init
682 *
683 * Initialization of the zone for Unified Buffer Cache.
684 *
685 * Parameters: (void)
686 *
687 * Returns: (void)
688 *
689 * Implicit returns:
690 * ubc_info_zone(global) initialized for subsequent allocations
1c79356b 691 */
0b4e3aa0 692__private_extern__ void
2d21ac55 693ubc_init(void)
1c79356b
A
694{
695 int i;
696
697 i = (vm_size_t) sizeof (struct ubc_info);
2d21ac55 698
1c79356b 699 ubc_info_zone = zinit (i, 10000*i, 8192, "ubc_info zone");
0b4c1975
A
700
701 zone_change(ubc_info_zone, Z_NOENCRYPT, TRUE);
1c79356b
A
702}
703
2d21ac55 704
1c79356b 705/*
2d21ac55
A
706 * ubc_info_init
707 *
708 * Allocate and attach an empty ubc_info structure to a vnode
709 *
710 * Parameters: vp Pointer to the vnode
711 *
712 * Returns: 0 Success
713 * vnode_size:ENOMEM Not enough space
714 * vnode_size:??? Other error from vnode_getattr
715 *
1c79356b
A
716 */
717int
718ubc_info_init(struct vnode *vp)
91447636
A
719{
720 return(ubc_info_init_internal(vp, 0, 0));
721}
2d21ac55
A
722
723
724/*
725 * ubc_info_init_withsize
726 *
727 * Allocate and attach a sized ubc_info structure to a vnode
728 *
729 * Parameters: vp Pointer to the vnode
730 * filesize The size of the file
731 *
732 * Returns: 0 Success
733 * vnode_size:ENOMEM Not enough space
734 * vnode_size:??? Other error from vnode_getattr
735 */
91447636
A
736int
737ubc_info_init_withsize(struct vnode *vp, off_t filesize)
738{
739 return(ubc_info_init_internal(vp, 1, filesize));
740}
741
2d21ac55
A
742
743/*
744 * ubc_info_init_internal
745 *
746 * Allocate and attach a ubc_info structure to a vnode
747 *
748 * Parameters: vp Pointer to the vnode
749 * withfsize{0,1} Zero if the size should be obtained
750 * from the vnode; otherwise, use filesize
751 * filesize The size of the file, if withfsize == 1
752 *
753 * Returns: 0 Success
754 * vnode_size:ENOMEM Not enough space
755 * vnode_size:??? Other error from vnode_getattr
756 *
757 * Notes: We call a blocking zalloc(), and the zone was created as an
758 * expandable and collectable zone, so if no memory is available,
759 * it is possible for zalloc() to block indefinitely. zalloc()
760 * may also panic if the zone of zones is exhausted, since it's
761 * NOT expandable.
762 *
763 * We unconditionally call vnode_pager_setup(), even if this is
764 * a reuse of a ubc_info; in that case, we should probably assert
765 * that it does not already have a pager association, but do not.
766 *
767 * Since memory_object_create_named() can only fail from receiving
768 * an invalid pager argument, the explicit check and panic is
769 * merely precautionary.
770 */
771static int
772ubc_info_init_internal(vnode_t vp, int withfsize, off_t filesize)
1c79356b
A
773{
774 register struct ubc_info *uip;
775 void * pager;
1c79356b
A
776 int error = 0;
777 kern_return_t kret;
0b4e3aa0 778 memory_object_control_t control;
1c79356b 779
91447636 780 uip = vp->v_ubcinfo;
1c79356b 781
2d21ac55
A
782 /*
783 * If there is not already a ubc_info attached to the vnode, we
784 * attach one; otherwise, we will reuse the one that's there.
785 */
91447636 786 if (uip == UBC_INFO_NULL) {
1c79356b 787
1c79356b 788 uip = (struct ubc_info *) zalloc(ubc_info_zone);
91447636
A
789 bzero((char *)uip, sizeof(struct ubc_info));
790
1c79356b 791 uip->ui_vnode = vp;
91447636 792 uip->ui_flags = UI_INITED;
1c79356b
A
793 uip->ui_ucred = NOCRED;
794 }
1c79356b
A
795 assert(uip->ui_flags != UI_NONE);
796 assert(uip->ui_vnode == vp);
797
1c79356b
A
798 /* now set this ubc_info in the vnode */
799 vp->v_ubcinfo = uip;
91447636 800
2d21ac55
A
801 /*
802 * Allocate a pager object for this vnode
803 *
804 * XXX The value of the pager parameter is currently ignored.
805 * XXX Presumably, this API changed to avoid the race between
806 * XXX setting the pager and the UI_HASPAGER flag.
807 */
1c79356b
A
808 pager = (void *)vnode_pager_setup(vp, uip->ui_pager);
809 assert(pager);
91447636 810
2d21ac55
A
811 /*
812 * Explicitly set the pager into the ubc_info, after setting the
813 * UI_HASPAGER flag.
814 */
91447636
A
815 SET(uip->ui_flags, UI_HASPAGER);
816 uip->ui_pager = pager;
1c79356b
A
817
818 /*
91447636 819 * Note: We can not use VNOP_GETATTR() to get accurate
2d21ac55
A
820 * value of ui_size because this may be an NFS vnode, and
821 * nfs_getattr() can call vinvalbuf(); if this happens,
822 * ubc_info is not set up to deal with that event.
1c79356b
A
823 * So use bogus size.
824 */
825
1c79356b 826 /*
0b4e3aa0
A
827 * create a vnode - vm_object association
828 * memory_object_create_named() creates a "named" reference on the
829 * memory object we hold this reference as long as the vnode is
830 * "alive." Since memory_object_create_named() took its own reference
831 * on the vnode pager we passed it, we can drop the reference
832 * vnode_pager_setup() returned here.
1c79356b 833 */
0b4e3aa0
A
834 kret = memory_object_create_named(pager,
835 (memory_object_size_t)uip->ui_size, &control);
836 vnode_pager_deallocate(pager);
837 if (kret != KERN_SUCCESS)
838 panic("ubc_info_init: memory_object_create_named returned %d", kret);
1c79356b 839
0b4e3aa0
A
840 assert(control);
841 uip->ui_control = control; /* cache the value of the mo control */
842 SET(uip->ui_flags, UI_HASOBJREF); /* with a named reference */
2d21ac55 843
91447636 844 if (withfsize == 0) {
91447636 845 /* initialize the size */
2d21ac55 846 error = vnode_size(vp, &uip->ui_size, vfs_context_current());
91447636
A
847 if (error)
848 uip->ui_size = 0;
849 } else {
850 uip->ui_size = filesize;
851 }
2d21ac55 852 vp->v_lflag |= VNAMED_UBC; /* vnode has a named ubc reference */
1c79356b 853
0b4e3aa0 854 return (error);
1c79356b
A
855}
856
2d21ac55
A
857
858/*
859 * ubc_info_free
860 *
861 * Free a ubc_info structure
862 *
863 * Parameters: uip A pointer to the ubc_info to free
864 *
865 * Returns: (void)
866 *
867 * Notes: If there is a credential that has subsequently been associated
868 * with the ubc_info via a call to ubc_setcred(), the reference
869 * to the credential is dropped.
870 *
871 * It's actually impossible for a ubc_info.ui_control to take the
872 * value MEMORY_OBJECT_CONTROL_NULL.
873 */
0b4e3aa0
A
874static void
875ubc_info_free(struct ubc_info *uip)
1c79356b 876{
0c530ab8
A
877 if (IS_VALID_CRED(uip->ui_ucred)) {
878 kauth_cred_unref(&uip->ui_ucred);
1c79356b 879 }
0b4e3aa0
A
880
881 if (uip->ui_control != MEMORY_OBJECT_CONTROL_NULL)
882 memory_object_control_deallocate(uip->ui_control);
91447636
A
883
884 cluster_release(uip);
2d21ac55 885 ubc_cs_free(uip);
0b4e3aa0 886
2d21ac55 887 zfree(ubc_info_zone, uip);
1c79356b
A
888 return;
889}
890
2d21ac55 891
0b4e3aa0
A
892void
893ubc_info_deallocate(struct ubc_info *uip)
894{
91447636 895 ubc_info_free(uip);
0b4e3aa0
A
896}
897
3e170ce0 898errno_t mach_to_bsd_errno(kern_return_t mach_err)
fe8ab488
A
899{
900 switch (mach_err) {
901 case KERN_SUCCESS:
902 return 0;
903
904 case KERN_INVALID_ADDRESS:
905 case KERN_INVALID_ARGUMENT:
906 case KERN_NOT_IN_SET:
907 case KERN_INVALID_NAME:
908 case KERN_INVALID_TASK:
909 case KERN_INVALID_RIGHT:
910 case KERN_INVALID_VALUE:
911 case KERN_INVALID_CAPABILITY:
912 case KERN_INVALID_HOST:
913 case KERN_MEMORY_PRESENT:
914 case KERN_INVALID_PROCESSOR_SET:
915 case KERN_INVALID_POLICY:
916 case KERN_ALREADY_WAITING:
917 case KERN_DEFAULT_SET:
918 case KERN_EXCEPTION_PROTECTED:
919 case KERN_INVALID_LEDGER:
920 case KERN_INVALID_MEMORY_CONTROL:
921 case KERN_INVALID_SECURITY:
922 case KERN_NOT_DEPRESSED:
923 case KERN_LOCK_OWNED:
924 case KERN_LOCK_OWNED_SELF:
925 return EINVAL;
926
927 case KERN_PROTECTION_FAILURE:
928 case KERN_NOT_RECEIVER:
929 case KERN_NO_ACCESS:
930 case KERN_POLICY_STATIC:
931 return EACCES;
932
933 case KERN_NO_SPACE:
934 case KERN_RESOURCE_SHORTAGE:
935 case KERN_UREFS_OVERFLOW:
936 case KERN_INVALID_OBJECT:
937 return ENOMEM;
938
939 case KERN_FAILURE:
940 return EIO;
941
942 case KERN_MEMORY_FAILURE:
943 case KERN_POLICY_LIMIT:
944 case KERN_CODESIGN_ERROR:
945 return EPERM;
946
947 case KERN_MEMORY_ERROR:
948 return EBUSY;
949
950 case KERN_ALREADY_IN_SET:
951 case KERN_NAME_EXISTS:
952 case KERN_RIGHT_EXISTS:
953 return EEXIST;
954
955 case KERN_ABORTED:
956 return EINTR;
957
958 case KERN_TERMINATED:
959 case KERN_LOCK_SET_DESTROYED:
960 case KERN_LOCK_UNSTABLE:
961 case KERN_SEMAPHORE_DESTROYED:
962 return ENOENT;
963
964 case KERN_RPC_SERVER_TERMINATED:
965 return ECONNRESET;
966
967 case KERN_NOT_SUPPORTED:
968 return ENOTSUP;
969
970 case KERN_NODE_DOWN:
971 return ENETDOWN;
972
973 case KERN_NOT_WAITING:
974 return ENOENT;
975
976 case KERN_OPERATION_TIMED_OUT:
977 return ETIMEDOUT;
978
979 default:
980 return EIO;
981 }
982}
2d21ac55 983
1c79356b 984/*
fe8ab488 985 * ubc_setsize_ex
2d21ac55 986 *
fe8ab488 987 * Tell the VM that the the size of the file represented by the vnode has
2d21ac55
A
988 * changed
989 *
fe8ab488
A
990 * Parameters: vp The vp whose backing file size is
991 * being changed
992 * nsize The new size of the backing file
993 * opts Options
994 *
995 * Returns: EINVAL for new size < 0
996 * ENOENT if no UBC info exists
997 * EAGAIN if UBC_SETSIZE_NO_FS_REENTRY option is set and new_size < old size
998 * Other errors (mapped to errno_t) returned by VM functions
999 *
1000 * Notes: This function will indicate success if the new size is the
1001 * same or larger than the old size (in this case, the
1002 * remainder of the file will require modification or use of
1003 * an existing upl to access successfully).
1004 *
1005 * This function will fail if the new file size is smaller,
1006 * and the memory region being invalidated was unable to
1007 * actually be invalidated and/or the last page could not be
1008 * flushed, if the new size is not aligned to a page
1009 * boundary. This is usually indicative of an I/O error.
1c79356b 1010 */
fe8ab488 1011errno_t ubc_setsize_ex(struct vnode *vp, off_t nsize, ubc_setsize_opts_t opts)
1c79356b
A
1012{
1013 off_t osize; /* ui_size before change */
1014 off_t lastpg, olastpgend, lastoff;
1015 struct ubc_info *uip;
0b4e3aa0 1016 memory_object_control_t control;
2d21ac55 1017 kern_return_t kret = KERN_SUCCESS;
1c79356b 1018
55e303ae 1019 if (nsize < (off_t)0)
fe8ab488 1020 return EINVAL;
1c79356b 1021
1c79356b 1022 if (!UBCINFOEXISTS(vp))
fe8ab488 1023 return ENOENT;
1c79356b
A
1024
1025 uip = vp->v_ubcinfo;
2d21ac55 1026 osize = uip->ui_size;
fe8ab488
A
1027
1028 if (ISSET(opts, UBC_SETSIZE_NO_FS_REENTRY) && nsize < osize)
1029 return EAGAIN;
1030
2d21ac55
A
1031 /*
1032 * Update the size before flushing the VM
1033 */
1c79356b
A
1034 uip->ui_size = nsize;
1035
b0d623f7 1036 if (nsize >= osize) { /* Nothing more to do */
6d2010ae
A
1037 if (nsize > osize) {
1038 lock_vnode_and_post(vp, NOTE_EXTEND);
1039 }
1040
fe8ab488 1041 return 0;
b0d623f7 1042 }
1c79356b
A
1043
1044 /*
1045 * When the file shrinks, invalidate the pages beyond the
1046 * new size. Also get rid of garbage beyond nsize on the
2d21ac55
A
1047 * last page. The ui_size already has the nsize, so any
1048 * subsequent page-in will zero-fill the tail properly
1c79356b 1049 */
1c79356b
A
1050 lastpg = trunc_page_64(nsize);
1051 olastpgend = round_page_64(osize);
0b4e3aa0
A
1052 control = uip->ui_control;
1053 assert(control);
1c79356b
A
1054 lastoff = (nsize & PAGE_MASK_64);
1055
2d21ac55 1056 if (lastoff) {
fe8ab488 1057 upl_t upl;
2d21ac55
A
1058 upl_page_info_t *pl;
1059
fe8ab488 1060 /*
2d21ac55 1061 * new EOF ends up in the middle of a page
fe8ab488 1062 * zero the tail of this page if it's currently
2d21ac55
A
1063 * present in the cache
1064 */
fe8ab488
A
1065 kret = ubc_create_upl(vp, lastpg, PAGE_SIZE, &upl, &pl, UPL_SET_LITE);
1066
1c79356b 1067 if (kret != KERN_SUCCESS)
2d21ac55
A
1068 panic("ubc_setsize: ubc_create_upl (error = %d)\n", kret);
1069
1070 if (upl_valid_page(pl, 0))
1071 cluster_zero(upl, (uint32_t)lastoff, PAGE_SIZE - (uint32_t)lastoff, NULL);
1072
1073 ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
1c79356b 1074
2d21ac55
A
1075 lastpg += PAGE_SIZE_64;
1076 }
1077 if (olastpgend > lastpg) {
b0d623f7
A
1078 int flags;
1079
1080 if (lastpg == 0)
1081 flags = MEMORY_OBJECT_DATA_FLUSH_ALL;
1082 else
1083 flags = MEMORY_OBJECT_DATA_FLUSH;
fe8ab488 1084 /*
2d21ac55
A
1085 * invalidate the pages beyond the new EOF page
1086 *
1087 */
fe8ab488
A
1088 kret = memory_object_lock_request(control,
1089 (memory_object_offset_t)lastpg,
1090 (memory_object_size_t)(olastpgend - lastpg), NULL, NULL,
1091 MEMORY_OBJECT_RETURN_NONE, flags, VM_PROT_NO_CHANGE);
2d21ac55
A
1092 if (kret != KERN_SUCCESS)
1093 printf("ubc_setsize: invalidate failed (error = %d)\n", kret);
1094 }
fe8ab488 1095 return mach_to_bsd_errno(kret);
1c79356b
A
1096}
1097
fe8ab488
A
1098// Returns true for success
1099int ubc_setsize(vnode_t vp, off_t nsize)
1100{
1101 return ubc_setsize_ex(vp, nsize, 0) == 0;
1102}
2d21ac55 1103
1c79356b 1104/*
2d21ac55
A
1105 * ubc_getsize
1106 *
1107 * Get the size of the file assocated with the specified vnode
1108 *
1109 * Parameters: vp The vnode whose size is of interest
1110 *
1111 * Returns: 0 There is no ubc_info associated with
1112 * this vnode, or the size is zero
1113 * !0 The size of the file
1114 *
1115 * Notes: Using this routine, it is not possible for a caller to
1116 * successfully distinguish between a vnode associate with a zero
1117 * length file, and a vnode with no associated ubc_info. The
1118 * caller therefore needs to not care, or needs to ensure that
1119 * they have previously successfully called ubc_info_init() or
1120 * ubc_info_init_withsize().
1c79356b
A
1121 */
1122off_t
1123ubc_getsize(struct vnode *vp)
1124{
91447636
A
1125 /* people depend on the side effect of this working this way
1126 * as they call this for directory
1c79356b 1127 */
91447636
A
1128 if (!UBCINFOEXISTS(vp))
1129 return ((off_t)0);
1130 return (vp->v_ubcinfo->ui_size);
1c79356b
A
1131}
1132
2d21ac55 1133
1c79356b 1134/*
2d21ac55
A
1135 * ubc_umount
1136 *
fe8ab488 1137 * Call ubc_msync(vp, 0, EOF, NULL, UBC_PUSHALL) on all the vnodes for this
2d21ac55
A
1138 * mount point
1139 *
1140 * Parameters: mp The mount point
1141 *
1142 * Returns: 0 Success
1143 *
1144 * Notes: There is no failure indication for this function.
1145 *
1146 * This function is used in the unmount path; since it may block
1147 * I/O indefinitely, it should not be used in the forced unmount
1148 * path, since a device unavailability could also block that
1149 * indefinitely.
1150 *
1151 * Because there is no device ejection interlock on USB, FireWire,
1152 * or similar devices, it's possible that an ejection that begins
1153 * subsequent to the vnode_iterate() completing, either on one of
1154 * those devices, or a network mount for which the server quits
1155 * responding, etc., may cause the caller to block indefinitely.
1c79356b 1156 */
0b4e3aa0 1157__private_extern__ int
1c79356b
A
1158ubc_umount(struct mount *mp)
1159{
91447636
A
1160 vnode_iterate(mp, 0, ubc_umcallback, 0);
1161 return(0);
1c79356b
A
1162}
1163
2d21ac55
A
1164
1165/*
1166 * ubc_umcallback
1167 *
1168 * Used by ubc_umount() as an internal implementation detail; see ubc_umount()
1169 * and vnode_iterate() for details of implementation.
1170 */
91447636
A
1171static int
1172ubc_umcallback(vnode_t vp, __unused void * args)
1c79356b 1173{
1c79356b 1174
91447636
A
1175 if (UBCINFOEXISTS(vp)) {
1176
91447636 1177 (void) ubc_msync(vp, (off_t)0, ubc_getsize(vp), NULL, UBC_PUSHALL);
1c79356b 1178 }
91447636 1179 return (VNODE_RETURNED);
1c79356b
A
1180}
1181
91447636 1182
2d21ac55
A
1183/*
1184 * ubc_getcred
1185 *
1186 * Get the credentials currently active for the ubc_info associated with the
1187 * vnode.
1188 *
1189 * Parameters: vp The vnode whose ubc_info credentials
1190 * are to be retrieved
1191 *
1192 * Returns: !NOCRED The credentials
1193 * NOCRED If there is no ubc_info for the vnode,
1194 * or if there is one, but it has not had
1195 * any credentials associated with it via
1196 * a call to ubc_setcred()
1197 */
91447636 1198kauth_cred_t
1c79356b
A
1199ubc_getcred(struct vnode *vp)
1200{
91447636
A
1201 if (UBCINFOEXISTS(vp))
1202 return (vp->v_ubcinfo->ui_ucred);
1c79356b 1203
91447636 1204 return (NOCRED);
1c79356b
A
1205}
1206
2d21ac55
A
1207
1208/*
1209 * ubc_setthreadcred
1210 *
1211 * If they are not already set, set the credentials of the ubc_info structure
1212 * associated with the vnode to those of the supplied thread; otherwise leave
1213 * them alone.
1214 *
1215 * Parameters: vp The vnode whose ubc_info creds are to
1216 * be set
1217 * p The process whose credentials are to
1218 * be used, if not running on an assumed
1219 * credential
1220 * thread The thread whose credentials are to
1221 * be used
1222 *
1223 * Returns: 1 This vnode has no associated ubc_info
1224 * 0 Success
1225 *
1226 * Notes: This function takes a proc parameter to account for bootstrap
1227 * issues where a task or thread may call this routine, either
1228 * before credentials have been initialized by bsd_init(), or if
1229 * there is no BSD info asscoiate with a mach thread yet. This
1230 * is known to happen in both the initial swap and memory mapping
1231 * calls.
1232 *
1233 * This function is generally used only in the following cases:
1234 *
1235 * o a memory mapped file via the mmap() system call
2d21ac55
A
1236 * o a swap store backing file
1237 * o subsequent to a successful write via vn_write()
1238 *
1239 * The information is then used by the NFS client in order to
1240 * cons up a wire message in either the page-in or page-out path.
1241 *
1242 * There are two potential problems with the use of this API:
1243 *
1244 * o Because the write path only set it on a successful
1245 * write, there is a race window between setting the
1246 * credential and its use to evict the pages to the
1247 * remote file server
1248 *
1249 * o Because a page-in may occur prior to a write, the
1250 * credential may not be set at this time, if the page-in
fe8ab488 1251 * is not the result of a mapping established via mmap().
2d21ac55
A
1252 *
1253 * In both these cases, this will be triggered from the paging
1254 * path, which will instead use the credential of the current
1255 * process, which in this case is either the dynamic_pager or
1256 * the kernel task, both of which utilize "root" credentials.
1257 *
1258 * This may potentially permit operations to occur which should
1259 * be denied, or it may cause to be denied operations which
1260 * should be permitted, depending on the configuration of the NFS
1261 * server.
1262 */
13fec989 1263int
2d21ac55 1264ubc_setthreadcred(struct vnode *vp, proc_t p, thread_t thread)
13fec989
A
1265{
1266 struct ubc_info *uip;
1267 kauth_cred_t credp;
2d21ac55 1268 struct uthread *uthread = get_bsdthread_info(thread);
13fec989
A
1269
1270 if (!UBCINFOEXISTS(vp))
2d21ac55 1271 return (1);
13fec989
A
1272
1273 vnode_lock(vp);
1274
1275 uip = vp->v_ubcinfo;
1276 credp = uip->ui_ucred;
1277
0c530ab8 1278 if (!IS_VALID_CRED(credp)) {
13fec989
A
1279 /* use per-thread cred, if assumed identity, else proc cred */
1280 if (uthread == NULL || (uthread->uu_flag & UT_SETUID) == 0) {
1281 uip->ui_ucred = kauth_cred_proc_ref(p);
1282 } else {
1283 uip->ui_ucred = uthread->uu_ucred;
1284 kauth_cred_ref(uip->ui_ucred);
1285 }
2d21ac55 1286 }
13fec989
A
1287 vnode_unlock(vp);
1288
1289 return (0);
1290}
1291
2d21ac55 1292
1c79356b 1293/*
2d21ac55
A
1294 * ubc_setcred
1295 *
1296 * If they are not already set, set the credentials of the ubc_info structure
1297 * associated with the vnode to those of the process; otherwise leave them
1298 * alone.
1299 *
1300 * Parameters: vp The vnode whose ubc_info creds are to
1301 * be set
1302 * p The process whose credentials are to
1303 * be used
1304 *
1305 * Returns: 0 This vnode has no associated ubc_info
1306 * 1 Success
1307 *
1308 * Notes: The return values for this function are inverted from nearly
1309 * all other uses in the kernel.
1310 *
1311 * See also ubc_setthreadcred(), above.
1312 *
1313 * This function is considered deprecated, and generally should
1314 * not be used, as it is incompatible with per-thread credentials;
1315 * it exists for legacy KPI reasons.
1316 *
1317 * DEPRECATION: ubc_setcred() is being deprecated. Please use
1318 * ubc_setthreadcred() instead.
1c79356b 1319 */
1c79356b 1320int
2d21ac55 1321ubc_setcred(struct vnode *vp, proc_t p)
1c79356b
A
1322{
1323 struct ubc_info *uip;
91447636 1324 kauth_cred_t credp;
1c79356b 1325
2d21ac55
A
1326 /* If there is no ubc_info, deny the operation */
1327 if ( !UBCINFOEXISTS(vp))
1c79356b 1328 return (0);
1c79356b 1329
2d21ac55
A
1330 /*
1331 * Check to see if there is already a credential reference in the
1332 * ubc_info; if there is not, take one on the supplied credential.
1333 */
91447636 1334 vnode_lock(vp);
91447636 1335 uip = vp->v_ubcinfo;
1c79356b 1336 credp = uip->ui_ucred;
0c530ab8 1337 if (!IS_VALID_CRED(credp)) {
91447636 1338 uip->ui_ucred = kauth_cred_proc_ref(p);
1c79356b 1339 }
91447636 1340 vnode_unlock(vp);
1c79356b
A
1341
1342 return (1);
1343}
1344
2d21ac55
A
1345/*
1346 * ubc_getpager
1347 *
1348 * Get the pager associated with the ubc_info associated with the vnode.
1349 *
1350 * Parameters: vp The vnode to obtain the pager from
1351 *
1352 * Returns: !VNODE_PAGER_NULL The memory_object_t for the pager
1353 * VNODE_PAGER_NULL There is no ubc_info for this vnode
1354 *
1355 * Notes: For each vnode that has a ubc_info associated with it, that
1356 * ubc_info SHALL have a pager associated with it, so in the
1357 * normal case, it's impossible to return VNODE_PAGER_NULL for
1358 * a vnode with an associated ubc_info.
1359 */
0b4e3aa0 1360__private_extern__ memory_object_t
1c79356b
A
1361ubc_getpager(struct vnode *vp)
1362{
91447636
A
1363 if (UBCINFOEXISTS(vp))
1364 return (vp->v_ubcinfo->ui_pager);
1c79356b 1365
91447636 1366 return (0);
1c79356b
A
1367}
1368
2d21ac55 1369
1c79356b 1370/*
2d21ac55
A
1371 * ubc_getobject
1372 *
1373 * Get the memory object control associated with the ubc_info associated with
1374 * the vnode
1375 *
1376 * Parameters: vp The vnode to obtain the memory object
1377 * from
1378 * flags DEPRECATED
1379 *
1380 * Returns: !MEMORY_OBJECT_CONTROL_NULL
1381 * MEMORY_OBJECT_CONTROL_NULL
1382 *
1383 * Notes: Historically, if the flags were not "do not reactivate", this
1384 * function would look up the memory object using the pager if
1385 * it did not exist (this could be the case if the vnode had
1386 * been previously reactivated). The flags would also permit a
1387 * hold to be requested, which would have created an object
1388 * reference, if one had not already existed. This usage is
1389 * deprecated, as it would permit a race between finding and
1390 * taking the reference vs. a single reference being dropped in
1391 * another thread.
1c79356b 1392 */
0b4e3aa0 1393memory_object_control_t
91447636 1394ubc_getobject(struct vnode *vp, __unused int flags)
1c79356b 1395{
91447636
A
1396 if (UBCINFOEXISTS(vp))
1397 return((vp->v_ubcinfo->ui_control));
1c79356b 1398
2d21ac55 1399 return (MEMORY_OBJECT_CONTROL_NULL);
1c79356b
A
1400}
1401
6d2010ae
A
1402boolean_t
1403ubc_strict_uncached_IO(struct vnode *vp)
1404{
1405 boolean_t result = FALSE;
1406
1407 if (UBCINFOEXISTS(vp)) {
1408 result = memory_object_is_slid(vp->v_ubcinfo->ui_control);
1409 }
1410 return result;
1411}
1c79356b 1412
2d21ac55
A
1413/*
1414 * ubc_blktooff
1415 *
1416 * Convert a given block number to a memory backing object (file) offset for a
1417 * given vnode
1418 *
1419 * Parameters: vp The vnode in which the block is located
1420 * blkno The block number to convert
1421 *
1422 * Returns: !-1 The offset into the backing object
1423 * -1 There is no ubc_info associated with
1424 * the vnode
1425 * -1 An error occurred in the underlying VFS
1426 * while translating the block to an
1427 * offset; the most likely cause is that
1428 * the caller specified a block past the
1429 * end of the file, but this could also be
1430 * any other error from VNOP_BLKTOOFF().
1431 *
1432 * Note: Representing the error in band loses some information, but does
1433 * not occlude a valid offset, since an off_t of -1 is normally
1434 * used to represent EOF. If we had a more reliable constant in
1435 * our header files for it (i.e. explicitly cast to an off_t), we
1436 * would use it here instead.
1437 */
1c79356b 1438off_t
91447636 1439ubc_blktooff(vnode_t vp, daddr64_t blkno)
1c79356b 1440{
2d21ac55 1441 off_t file_offset = -1;
1c79356b
A
1442 int error;
1443
2d21ac55
A
1444 if (UBCINFOEXISTS(vp)) {
1445 error = VNOP_BLKTOOFF(vp, blkno, &file_offset);
1446 if (error)
1447 file_offset = -1;
1448 }
1c79356b
A
1449
1450 return (file_offset);
1451}
0b4e3aa0 1452
2d21ac55
A
1453
1454/*
1455 * ubc_offtoblk
1456 *
1457 * Convert a given offset in a memory backing object into a block number for a
1458 * given vnode
1459 *
1460 * Parameters: vp The vnode in which the offset is
1461 * located
1462 * offset The offset into the backing object
1463 *
1464 * Returns: !-1 The returned block number
1465 * -1 There is no ubc_info associated with
1466 * the vnode
1467 * -1 An error occurred in the underlying VFS
1468 * while translating the block to an
1469 * offset; the most likely cause is that
1470 * the caller specified a block past the
1471 * end of the file, but this could also be
1472 * any other error from VNOP_OFFTOBLK().
1473 *
1474 * Note: Representing the error in band loses some information, but does
1475 * not occlude a valid block number, since block numbers exceed
1476 * the valid range for offsets, due to their relative sizes. If
1477 * we had a more reliable constant than -1 in our header files
1478 * for it (i.e. explicitly cast to an daddr64_t), we would use it
1479 * here instead.
1480 */
91447636
A
1481daddr64_t
1482ubc_offtoblk(vnode_t vp, off_t offset)
1c79356b 1483{
2d21ac55 1484 daddr64_t blkno = -1;
0b4e3aa0 1485 int error = 0;
1c79356b 1486
2d21ac55
A
1487 if (UBCINFOEXISTS(vp)) {
1488 error = VNOP_OFFTOBLK(vp, offset, &blkno);
1489 if (error)
1490 blkno = -1;
1491 }
1c79356b
A
1492
1493 return (blkno);
1494}
1495
2d21ac55
A
1496
1497/*
1498 * ubc_pages_resident
1499 *
1500 * Determine whether or not a given vnode has pages resident via the memory
1501 * object control associated with the ubc_info associated with the vnode
1502 *
1503 * Parameters: vp The vnode we want to know about
1504 *
1505 * Returns: 1 Yes
1506 * 0 No
1507 */
1c79356b 1508int
91447636 1509ubc_pages_resident(vnode_t vp)
1c79356b 1510{
91447636
A
1511 kern_return_t kret;
1512 boolean_t has_pages_resident;
1513
2d21ac55 1514 if (!UBCINFOEXISTS(vp))
0b4e3aa0 1515 return (0);
91447636 1516
2d21ac55
A
1517 /*
1518 * The following call may fail if an invalid ui_control is specified,
1519 * or if there is no VM object associated with the control object. In
1520 * either case, reacting to it as if there were no pages resident will
1521 * result in correct behavior.
1522 */
91447636
A
1523 kret = memory_object_pages_resident(vp->v_ubcinfo->ui_control, &has_pages_resident);
1524
1525 if (kret != KERN_SUCCESS)
0b4e3aa0 1526 return (0);
91447636
A
1527
1528 if (has_pages_resident == TRUE)
1529 return (1);
1530
1531 return (0);
1532}
1c79356b 1533
0b4e3aa0 1534/*
2d21ac55
A
1535 * ubc_msync
1536 *
1537 * Clean and/or invalidate a range in the memory object that backs this vnode
1538 *
1539 * Parameters: vp The vnode whose associated ubc_info's
1540 * associated memory object is to have a
1541 * range invalidated within it
1542 * beg_off The start of the range, as an offset
1543 * end_off The end of the range, as an offset
1544 * resid_off The address of an off_t supplied by the
1545 * caller; may be set to NULL to ignore
1546 * flags See ubc_msync_internal()
1547 *
1548 * Returns: 0 Success
1549 * !0 Failure; an errno is returned
1550 *
1551 * Implicit Returns:
1552 * *resid_off, modified If non-NULL, the contents are ALWAYS
1553 * modified; they are initialized to the
1554 * beg_off, and in case of an I/O error,
1555 * the difference between beg_off and the
1556 * current value will reflect what was
1557 * able to be written before the error
1558 * occurred. If no error is returned, the
1559 * value of the resid_off is undefined; do
1560 * NOT use it in place of end_off if you
1561 * intend to increment from the end of the
1562 * last call and call iteratively.
1563 *
1564 * Notes: see ubc_msync_internal() for more detailed information.
1565 *
0b4e3aa0 1566 */
91447636
A
1567errno_t
1568ubc_msync(vnode_t vp, off_t beg_off, off_t end_off, off_t *resid_off, int flags)
0b4e3aa0 1569{
91447636
A
1570 int retval;
1571 int io_errno = 0;
1572
1573 if (resid_off)
1574 *resid_off = beg_off;
0b4e3aa0 1575
91447636 1576 retval = ubc_msync_internal(vp, beg_off, end_off, resid_off, flags, &io_errno);
0b4e3aa0 1577
91447636
A
1578 if (retval == 0 && io_errno == 0)
1579 return (EINVAL);
1580 return (io_errno);
1581}
0b4e3aa0 1582
1c79356b 1583
1c79356b 1584/*
fe8ab488
A
1585 * ubc_msync_internal
1586 *
2d21ac55
A
1587 * Clean and/or invalidate a range in the memory object that backs this vnode
1588 *
1589 * Parameters: vp The vnode whose associated ubc_info's
1590 * associated memory object is to have a
1591 * range invalidated within it
1592 * beg_off The start of the range, as an offset
1593 * end_off The end of the range, as an offset
1594 * resid_off The address of an off_t supplied by the
1595 * caller; may be set to NULL to ignore
1596 * flags MUST contain at least one of the flags
1597 * UBC_INVALIDATE, UBC_PUSHDIRTY, or
1598 * UBC_PUSHALL; if UBC_PUSHDIRTY is used,
1599 * UBC_SYNC may also be specified to cause
1600 * this function to block until the
1601 * operation is complete. The behavior
1602 * of UBC_SYNC is otherwise undefined.
1603 * io_errno The address of an int to contain the
1604 * errno from a failed I/O operation, if
1605 * one occurs; may be set to NULL to
1606 * ignore
1607 *
1608 * Returns: 1 Success
1609 * 0 Failure
1610 *
1611 * Implicit Returns:
1612 * *resid_off, modified The contents of this offset MAY be
1613 * modified; in case of an I/O error, the
1614 * difference between beg_off and the
1615 * current value will reflect what was
1616 * able to be written before the error
1617 * occurred.
1618 * *io_errno, modified The contents of this offset are set to
1619 * an errno, if an error occurs; if the
1620 * caller supplies an io_errno parameter,
1621 * they should be careful to initialize it
1622 * to 0 before calling this function to
1623 * enable them to distinguish an error
1624 * with a valid *resid_off from an invalid
1625 * one, and to avoid potentially falsely
1626 * reporting an error, depending on use.
1627 *
1628 * Notes: If there is no ubc_info associated with the vnode supplied,
1629 * this function immediately returns success.
1630 *
1631 * If the value of end_off is less than or equal to beg_off, this
1632 * function immediately returns success; that is, end_off is NOT
1633 * inclusive.
1634 *
1635 * IMPORTANT: one of the flags UBC_INVALIDATE, UBC_PUSHDIRTY, or
1636 * UBC_PUSHALL MUST be specified; that is, it is NOT possible to
1637 * attempt to block on in-progress I/O by calling this function
1638 * with UBC_PUSHDIRTY, and then later call it with just UBC_SYNC
1639 * in order to block pending on the I/O already in progress.
1640 *
1641 * The start offset is truncated to the page boundary and the
1642 * size is adjusted to include the last page in the range; that
1643 * is, end_off on exactly a page boundary will not change if it
1644 * is rounded, and the range of bytes written will be from the
1645 * truncate beg_off to the rounded (end_off - 1).
1c79356b 1646 */
91447636
A
1647static int
1648ubc_msync_internal(vnode_t vp, off_t beg_off, off_t end_off, off_t *resid_off, int flags, int *io_errno)
1c79356b 1649{
91447636
A
1650 memory_object_size_t tsize;
1651 kern_return_t kret;
1652 int request_flags = 0;
1653 int flush_flags = MEMORY_OBJECT_RETURN_NONE;
1654
1655 if ( !UBCINFOEXISTS(vp))
1656 return (0);
91447636
A
1657 if ((flags & (UBC_INVALIDATE | UBC_PUSHDIRTY | UBC_PUSHALL)) == 0)
1658 return (0);
2d21ac55
A
1659 if (end_off <= beg_off)
1660 return (1);
91447636
A
1661
1662 if (flags & UBC_INVALIDATE)
1663 /*
1664 * discard the resident pages
1665 */
1666 request_flags = (MEMORY_OBJECT_DATA_FLUSH | MEMORY_OBJECT_DATA_NO_CHANGE);
1c79356b 1667
91447636
A
1668 if (flags & UBC_SYNC)
1669 /*
1670 * wait for all the I/O to complete before returning
55e303ae 1671 */
91447636 1672 request_flags |= MEMORY_OBJECT_IO_SYNC;
55e303ae 1673
91447636
A
1674 if (flags & UBC_PUSHDIRTY)
1675 /*
1676 * we only return the dirty pages in the range
1677 */
1678 flush_flags = MEMORY_OBJECT_RETURN_DIRTY;
0b4e3aa0 1679
91447636
A
1680 if (flags & UBC_PUSHALL)
1681 /*
2d21ac55
A
1682 * then return all the interesting pages in the range (both
1683 * dirty and precious) to the pager
91447636
A
1684 */
1685 flush_flags = MEMORY_OBJECT_RETURN_ALL;
0b4e3aa0 1686
91447636
A
1687 beg_off = trunc_page_64(beg_off);
1688 end_off = round_page_64(end_off);
1689 tsize = (memory_object_size_t)end_off - beg_off;
b4c24cb9 1690
91447636
A
1691 /* flush and/or invalidate pages in the range requested */
1692 kret = memory_object_lock_request(vp->v_ubcinfo->ui_control,
2d21ac55
A
1693 beg_off, tsize,
1694 (memory_object_offset_t *)resid_off,
1695 io_errno, flush_flags, request_flags,
1696 VM_PROT_NO_CHANGE);
91447636
A
1697
1698 return ((kret == KERN_SUCCESS) ? 1 : 0);
1c79356b
A
1699}
1700
1c79356b
A
1701
1702/*
fe8ab488 1703 * ubc_map
2d21ac55
A
1704 *
1705 * Explicitly map a vnode that has an associate ubc_info, and add a reference
1706 * to it for the ubc system, if there isn't one already, so it will not be
1707 * recycled while it's in use, and set flags on the ubc_info to indicate that
1708 * we have done this
1709 *
1710 * Parameters: vp The vnode to map
1711 * flags The mapping flags for the vnode; this
1712 * will be a combination of one or more of
1713 * PROT_READ, PROT_WRITE, and PROT_EXEC
1714 *
1715 * Returns: 0 Success
1716 * EPERM Permission was denied
1717 *
1718 * Notes: An I/O reference on the vnode must already be held on entry
1719 *
1720 * If there is no ubc_info associated with the vnode, this function
1721 * will return success.
1722 *
1723 * If a permission error occurs, this function will return
1724 * failure; all other failures will cause this function to return
1725 * success.
1726 *
1727 * IMPORTANT: This is an internal use function, and its symbols
1728 * are not exported, hence its error checking is not very robust.
1729 * It is primarily used by:
1730 *
1731 * o mmap(), when mapping a file
2d21ac55
A
1732 * o When mapping a shared file (a shared library in the
1733 * shared segment region)
1734 * o When loading a program image during the exec process
1735 *
1736 * ...all of these uses ignore the return code, and any fault that
1737 * results later because of a failure is handled in the fix-up path
1738 * of the fault handler. The interface exists primarily as a
1739 * performance hint.
1740 *
1741 * Given that third party implementation of the type of interfaces
1742 * that would use this function, such as alternative executable
1743 * formats, etc., are unsupported, this function is not exported
1744 * for general use.
1745 *
1746 * The extra reference is held until the VM system unmaps the
1747 * vnode from its own context to maintain a vnode reference in
1748 * cases like open()/mmap()/close(), which leave the backing
1749 * object referenced by a mapped memory region in a process
1750 * address space.
1c79356b 1751 */
91447636
A
1752__private_extern__ int
1753ubc_map(vnode_t vp, int flags)
1c79356b
A
1754{
1755 struct ubc_info *uip;
91447636
A
1756 int error = 0;
1757 int need_ref = 0;
2d21ac55 1758 int need_wakeup = 0;
1c79356b 1759
91447636 1760 if (UBCINFOEXISTS(vp)) {
1c79356b 1761
2d21ac55
A
1762 vnode_lock(vp);
1763 uip = vp->v_ubcinfo;
1764
1765 while (ISSET(uip->ui_flags, UI_MAPBUSY)) {
1766 SET(uip->ui_flags, UI_MAPWAITING);
1767 (void) msleep(&uip->ui_flags, &vp->v_lock,
1768 PRIBIO, "ubc_map", NULL);
1769 }
1770 SET(uip->ui_flags, UI_MAPBUSY);
1771 vnode_unlock(vp);
1772
1773 error = VNOP_MMAP(vp, flags, vfs_context_current());
1c79356b 1774
91447636
A
1775 if (error != EPERM)
1776 error = 0;
1c79356b 1777
2d21ac55 1778 vnode_lock_spin(vp);
1c79356b 1779
2d21ac55 1780 if (error == 0) {
91447636
A
1781 if ( !ISSET(uip->ui_flags, UI_ISMAPPED))
1782 need_ref = 1;
1783 SET(uip->ui_flags, (UI_WASMAPPED | UI_ISMAPPED));
22ba694c
A
1784 if (flags & PROT_WRITE) {
1785 SET(uip->ui_flags, UI_MAPPEDWRITE);
1786 }
2d21ac55
A
1787 }
1788 CLR(uip->ui_flags, UI_MAPBUSY);
55e303ae 1789
2d21ac55
A
1790 if (ISSET(uip->ui_flags, UI_MAPWAITING)) {
1791 CLR(uip->ui_flags, UI_MAPWAITING);
1792 need_wakeup = 1;
55e303ae 1793 }
2d21ac55 1794 vnode_unlock(vp);
b4c24cb9 1795
2d21ac55
A
1796 if (need_wakeup)
1797 wakeup(&uip->ui_flags);
1798
1799 if (need_ref)
1800 vnode_ref(vp);
1801 }
91447636 1802 return (error);
0b4e3aa0
A
1803}
1804
2d21ac55 1805
0b4e3aa0 1806/*
2d21ac55
A
1807 * ubc_destroy_named
1808 *
1809 * Destroy the named memory object associated with the ubc_info control object
1810 * associated with the designated vnode, if there is a ubc_info associated
1811 * with the vnode, and a control object is associated with it
1812 *
1813 * Parameters: vp The designated vnode
1814 *
1815 * Returns: (void)
1816 *
1817 * Notes: This function is called on vnode termination for all vnodes,
1818 * and must therefore not assume that there is a ubc_info that is
1819 * associated with the vnode, nor that there is a control object
1820 * associated with the ubc_info.
1821 *
1822 * If all the conditions necessary are present, this function
1823 * calls memory_object_destory(), which will in turn end up
1824 * calling ubc_unmap() to release any vnode references that were
1825 * established via ubc_map().
1826 *
1827 * IMPORTANT: This is an internal use function that is used
1828 * exclusively by the internal use function vclean().
0b4e3aa0 1829 */
2d21ac55
A
1830__private_extern__ void
1831ubc_destroy_named(vnode_t vp)
0b4e3aa0
A
1832{
1833 memory_object_control_t control;
0b4e3aa0
A
1834 struct ubc_info *uip;
1835 kern_return_t kret;
1836
2d21ac55
A
1837 if (UBCINFOEXISTS(vp)) {
1838 uip = vp->v_ubcinfo;
1839
1840 /* Terminate the memory object */
1841 control = ubc_getobject(vp, UBC_HOLDOBJECT);
1842 if (control != MEMORY_OBJECT_CONTROL_NULL) {
1843 kret = memory_object_destroy(control, 0);
1844 if (kret != KERN_SUCCESS)
1845 panic("ubc_destroy_named: memory_object_destroy failed");
0b4e3aa0
A
1846 }
1847 }
1c79356b
A
1848}
1849
0b4e3aa0 1850
1c79356b 1851/*
2d21ac55
A
1852 * ubc_isinuse
1853 *
1854 * Determine whether or not a vnode is currently in use by ubc at a level in
1855 * excess of the requested busycount
1856 *
1857 * Parameters: vp The vnode to check
1858 * busycount The threshold busy count, used to bias
1859 * the count usually already held by the
1860 * caller to avoid races
1861 *
1862 * Returns: 1 The vnode is in use over the threshold
1863 * 0 The vnode is not in use over the
1864 * threshold
1865 *
1866 * Notes: Because the vnode is only held locked while actually asking
1867 * the use count, this function only represents a snapshot of the
1868 * current state of the vnode. If more accurate information is
1869 * required, an additional busycount should be held by the caller
1870 * and a non-zero busycount used.
1871 *
1872 * If there is no ubc_info associated with the vnode, this
1873 * function will report that the vnode is not in use by ubc.
1c79356b
A
1874 */
1875int
91447636 1876ubc_isinuse(struct vnode *vp, int busycount)
1c79356b 1877{
91447636 1878 if ( !UBCINFOEXISTS(vp))
0b4e3aa0 1879 return (0);
91447636 1880 return(ubc_isinuse_locked(vp, busycount, 0));
1c79356b
A
1881}
1882
91447636 1883
2d21ac55
A
1884/*
1885 * ubc_isinuse_locked
1886 *
1887 * Determine whether or not a vnode is currently in use by ubc at a level in
1888 * excess of the requested busycount
1889 *
1890 * Parameters: vp The vnode to check
1891 * busycount The threshold busy count, used to bias
1892 * the count usually already held by the
1893 * caller to avoid races
1894 * locked True if the vnode is already locked by
1895 * the caller
1896 *
1897 * Returns: 1 The vnode is in use over the threshold
1898 * 0 The vnode is not in use over the
1899 * threshold
1900 *
1901 * Notes: If the vnode is not locked on entry, it is locked while
1902 * actually asking the use count. If this is the case, this
1903 * function only represents a snapshot of the current state of
1904 * the vnode. If more accurate information is required, the
1905 * vnode lock should be held by the caller, otherwise an
1906 * additional busycount should be held by the caller and a
1907 * non-zero busycount used.
1908 *
1909 * If there is no ubc_info associated with the vnode, this
1910 * function will report that the vnode is not in use by ubc.
1911 */
1c79356b 1912int
91447636 1913ubc_isinuse_locked(struct vnode *vp, int busycount, int locked)
1c79356b 1914{
91447636 1915 int retval = 0;
1c79356b 1916
9bccf70c 1917
91447636 1918 if (!locked)
b0d623f7 1919 vnode_lock_spin(vp);
1c79356b 1920
91447636
A
1921 if ((vp->v_usecount - vp->v_kusecount) > busycount)
1922 retval = 1;
1923
1924 if (!locked)
1925 vnode_unlock(vp);
1926 return (retval);
1c79356b
A
1927}
1928
91447636 1929
1c79356b 1930/*
2d21ac55
A
1931 * ubc_unmap
1932 *
1933 * Reverse the effects of a ubc_map() call for a given vnode
1934 *
1935 * Parameters: vp vnode to unmap from ubc
1936 *
1937 * Returns: (void)
1938 *
1939 * Notes: This is an internal use function used by vnode_pager_unmap().
1940 * It will attempt to obtain a reference on the supplied vnode,
1941 * and if it can do so, and there is an associated ubc_info, and
1942 * the flags indicate that it was mapped via ubc_map(), then the
1943 * flag is cleared, the mapping removed, and the reference taken
1944 * by ubc_map() is released.
1945 *
1946 * IMPORTANT: This MUST only be called by the VM
1947 * to prevent race conditions.
1c79356b 1948 */
0b4e3aa0 1949__private_extern__ void
1c79356b
A
1950ubc_unmap(struct vnode *vp)
1951{
1952 struct ubc_info *uip;
91447636 1953 int need_rele = 0;
2d21ac55 1954 int need_wakeup = 0;
b0d623f7 1955
91447636
A
1956 if (vnode_getwithref(vp))
1957 return;
1c79356b 1958
91447636 1959 if (UBCINFOEXISTS(vp)) {
fe8ab488
A
1960 bool want_fsevent = false;
1961
91447636 1962 vnode_lock(vp);
91447636 1963 uip = vp->v_ubcinfo;
2d21ac55
A
1964
1965 while (ISSET(uip->ui_flags, UI_MAPBUSY)) {
1966 SET(uip->ui_flags, UI_MAPWAITING);
1967 (void) msleep(&uip->ui_flags, &vp->v_lock,
1968 PRIBIO, "ubc_unmap", NULL);
1969 }
1970 SET(uip->ui_flags, UI_MAPBUSY);
1971
91447636 1972 if (ISSET(uip->ui_flags, UI_ISMAPPED)) {
fe8ab488
A
1973 if (ISSET(uip->ui_flags, UI_MAPPEDWRITE))
1974 want_fsevent = true;
1975
91447636 1976 need_rele = 1;
fe8ab488
A
1977
1978 /*
1979 * We want to clear the mapped flags after we've called
1980 * VNOP_MNOMAP to avoid certain races and allow
1981 * VNOP_MNOMAP to call ubc_is_mapped_writable.
1982 */
91447636
A
1983 }
1984 vnode_unlock(vp);
fe8ab488 1985
91447636 1986 if (need_rele) {
fe8ab488
A
1987 vfs_context_t ctx = vfs_context_current();
1988
1989 (void)VNOP_MNOMAP(vp, ctx);
1990
1991#if CONFIG_FSE
1992 /*
1993 * Why do we want an fsevent here? Normally the
1994 * content modified fsevent is posted when a file is
1995 * closed and only if it's written to via conventional
1996 * means. It's perfectly legal to close a file and
1997 * keep your mappings and we don't currently track
1998 * whether it was written to via a mapping.
1999 * Therefore, we need to post an fsevent here if the
2000 * file was mapped writable. This may result in false
2001 * events, i.e. we post a notification when nothing
2002 * has really changed.
2003 */
2004 if (want_fsevent && need_fsevent(FSE_CONTENT_MODIFIED, vp)) {
2005 add_fsevent(FSE_CONTENT_MODIFIED, ctx,
2006 FSE_ARG_VNODE, vp,
2007 FSE_ARG_DONE);
2008 }
2009#endif
2010
b0d623f7 2011 vnode_rele(vp);
91447636 2012 }
2d21ac55
A
2013
2014 vnode_lock_spin(vp);
2015
fe8ab488
A
2016 if (need_rele)
2017 CLR(uip->ui_flags, UI_ISMAPPED | UI_MAPPEDWRITE);
2018
2d21ac55 2019 CLR(uip->ui_flags, UI_MAPBUSY);
fe8ab488 2020
2d21ac55
A
2021 if (ISSET(uip->ui_flags, UI_MAPWAITING)) {
2022 CLR(uip->ui_flags, UI_MAPWAITING);
2023 need_wakeup = 1;
2024 }
2025 vnode_unlock(vp);
2026
2027 if (need_wakeup)
b0d623f7 2028 wakeup(&uip->ui_flags);
2d21ac55 2029
91447636
A
2030 }
2031 /*
2032 * the drop of the vnode ref will cleanup
2033 */
2034 vnode_put(vp);
0b4e3aa0
A
2035}
2036
2d21ac55
A
2037
2038/*
2039 * ubc_page_op
2040 *
2041 * Manipulate individual page state for a vnode with an associated ubc_info
2042 * with an associated memory object control.
2043 *
2044 * Parameters: vp The vnode backing the page
2045 * f_offset A file offset interior to the page
2046 * ops The operations to perform, as a bitmap
2047 * (see below for more information)
2048 * phys_entryp The address of a ppnum_t; may be NULL
2049 * to ignore
2050 * flagsp A pointer to an int to contain flags;
2051 * may be NULL to ignore
2052 *
2053 * Returns: KERN_SUCCESS Success
2054 * KERN_INVALID_ARGUMENT If the memory object control has no VM
2055 * object associated
2056 * KERN_INVALID_OBJECT If UPL_POP_PHYSICAL and the object is
2057 * not physically contiguous
2058 * KERN_INVALID_OBJECT If !UPL_POP_PHYSICAL and the object is
2059 * physically contiguous
2060 * KERN_FAILURE If the page cannot be looked up
2061 *
2062 * Implicit Returns:
2063 * *phys_entryp (modified) If phys_entryp is non-NULL and
2064 * UPL_POP_PHYSICAL
2065 * *flagsp (modified) If flagsp is non-NULL and there was
2066 * !UPL_POP_PHYSICAL and a KERN_SUCCESS
2067 *
2068 * Notes: For object boundaries, it is considerably more efficient to
2069 * ensure that f_offset is in fact on a page boundary, as this
2070 * will avoid internal use of the hash table to identify the
2071 * page, and would therefore skip a number of early optimizations.
2072 * Since this is a page operation anyway, the caller should try
2073 * to pass only a page aligned offset because of this.
2074 *
2075 * *flagsp may be modified even if this function fails. If it is
2076 * modified, it will contain the condition of the page before the
2077 * requested operation was attempted; these will only include the
2078 * bitmap flags, and not the PL_POP_PHYSICAL, UPL_POP_DUMP,
2079 * UPL_POP_SET, or UPL_POP_CLR bits.
2080 *
2081 * The flags field may contain a specific operation, such as
2082 * UPL_POP_PHYSICAL or UPL_POP_DUMP:
2083 *
2084 * o UPL_POP_PHYSICAL Fail if not contiguous; if
2085 * *phys_entryp and successful, set
2086 * *phys_entryp
2087 * o UPL_POP_DUMP Dump the specified page
2088 *
2089 * Otherwise, it is treated as a bitmap of one or more page
2090 * operations to perform on the final memory object; allowable
2091 * bit values are:
2092 *
2093 * o UPL_POP_DIRTY The page is dirty
2094 * o UPL_POP_PAGEOUT The page is paged out
2095 * o UPL_POP_PRECIOUS The page is precious
2096 * o UPL_POP_ABSENT The page is absent
2097 * o UPL_POP_BUSY The page is busy
2098 *
2099 * If the page status is only being queried and not modified, then
2100 * not other bits should be specified. However, if it is being
2101 * modified, exactly ONE of the following bits should be set:
2102 *
2103 * o UPL_POP_SET Set the current bitmap bits
2104 * o UPL_POP_CLR Clear the current bitmap bits
2105 *
2106 * Thus to effect a combination of setting an clearing, it may be
2107 * necessary to call this function twice. If this is done, the
2108 * set should be used before the clear, since clearing may trigger
2109 * a wakeup on the destination page, and if the page is backed by
2110 * an encrypted swap file, setting will trigger the decryption
2111 * needed before the wakeup occurs.
2112 */
0b4e3aa0
A
2113kern_return_t
2114ubc_page_op(
2115 struct vnode *vp,
2116 off_t f_offset,
2117 int ops,
55e303ae 2118 ppnum_t *phys_entryp,
0b4e3aa0
A
2119 int *flagsp)
2120{
2121 memory_object_control_t control;
2122
2123 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2124 if (control == MEMORY_OBJECT_CONTROL_NULL)
2125 return KERN_INVALID_ARGUMENT;
2126
2127 return (memory_object_page_op(control,
2128 (memory_object_offset_t)f_offset,
2129 ops,
2130 phys_entryp,
2131 flagsp));
2132}
2d21ac55
A
2133
2134
2135/*
2136 * ubc_range_op
2137 *
2138 * Manipulate page state for a range of memory for a vnode with an associated
2139 * ubc_info with an associated memory object control, when page level state is
2140 * not required to be returned from the call (i.e. there are no phys_entryp or
2141 * flagsp parameters to this call, and it takes a range which may contain
2142 * multiple pages, rather than an offset interior to a single page).
2143 *
2144 * Parameters: vp The vnode backing the page
2145 * f_offset_beg A file offset interior to the start page
2146 * f_offset_end A file offset interior to the end page
2147 * ops The operations to perform, as a bitmap
2148 * (see below for more information)
2149 * range The address of an int; may be NULL to
2150 * ignore
2151 *
2152 * Returns: KERN_SUCCESS Success
2153 * KERN_INVALID_ARGUMENT If the memory object control has no VM
2154 * object associated
2155 * KERN_INVALID_OBJECT If the object is physically contiguous
2156 *
2157 * Implicit Returns:
2158 * *range (modified) If range is non-NULL, its contents will
2159 * be modified to contain the number of
2160 * bytes successfully operated upon.
2161 *
2162 * Notes: IMPORTANT: This function cannot be used on a range that
2163 * consists of physically contiguous pages.
2164 *
2165 * For object boundaries, it is considerably more efficient to
2166 * ensure that f_offset_beg and f_offset_end are in fact on page
2167 * boundaries, as this will avoid internal use of the hash table
2168 * to identify the page, and would therefore skip a number of
2169 * early optimizations. Since this is an operation on a set of
2170 * pages anyway, the caller should try to pass only a page aligned
2171 * offsets because of this.
2172 *
2173 * *range will be modified only if this function succeeds.
2174 *
2175 * The flags field MUST contain a specific operation; allowable
2176 * values are:
2177 *
2178 * o UPL_ROP_ABSENT Returns the extent of the range
2179 * presented which is absent, starting
2180 * with the start address presented
2181 *
2182 * o UPL_ROP_PRESENT Returns the extent of the range
2183 * presented which is present (resident),
2184 * starting with the start address
2185 * presented
2186 * o UPL_ROP_DUMP Dump the pages which are found in the
2187 * target object for the target range.
2188 *
2189 * IMPORTANT: For UPL_ROP_ABSENT and UPL_ROP_PRESENT; if there are
2190 * multiple regions in the range, only the first matching region
2191 * is returned.
2192 */
55e303ae
A
2193kern_return_t
2194ubc_range_op(
2195 struct vnode *vp,
2196 off_t f_offset_beg,
2197 off_t f_offset_end,
2198 int ops,
2199 int *range)
2200{
2201 memory_object_control_t control;
2202
2203 control = ubc_getobject(vp, UBC_FLAGS_NONE);
2204 if (control == MEMORY_OBJECT_CONTROL_NULL)
2205 return KERN_INVALID_ARGUMENT;
2206
2207 return (memory_object_range_op(control,
2208 (memory_object_offset_t)f_offset_beg,
2209 (memory_object_offset_t)f_offset_end,
2210 ops,
2211 range));
2212}
2d21ac55
A
2213
2214
2215/*
2216 * ubc_create_upl
2217 *
2218 * Given a vnode, cause the population of a portion of the vm_object; based on
2219 * the nature of the request, the pages returned may contain valid data, or
2220 * they may be uninitialized.
2221 *
2222 * Parameters: vp The vnode from which to create the upl
2223 * f_offset The start offset into the backing store
2224 * represented by the vnode
2225 * bufsize The size of the upl to create
2226 * uplp Pointer to the upl_t to receive the
2227 * created upl; MUST NOT be NULL
2228 * plp Pointer to receive the internal page
2229 * list for the created upl; MAY be NULL
2230 * to ignore
2231 *
2232 * Returns: KERN_SUCCESS The requested upl has been created
2233 * KERN_INVALID_ARGUMENT The bufsize argument is not an even
2234 * multiple of the page size
2235 * KERN_INVALID_ARGUMENT There is no ubc_info associated with
2236 * the vnode, or there is no memory object
2237 * control associated with the ubc_info
2238 * memory_object_upl_request:KERN_INVALID_VALUE
2239 * The supplied upl_flags argument is
2240 * invalid
2241 * Implicit Returns:
2242 * *uplp (modified)
2243 * *plp (modified) If non-NULL, the value of *plp will be
2244 * modified to point to the internal page
2245 * list; this modification may occur even
2246 * if this function is unsuccessful, in
2247 * which case the contents may be invalid
2248 *
2249 * Note: If successful, the returned *uplp MUST subsequently be freed
2250 * via a call to ubc_upl_commit(), ubc_upl_commit_range(),
2251 * ubc_upl_abort(), or ubc_upl_abort_range().
2252 */
0b4e3aa0
A
2253kern_return_t
2254ubc_create_upl(
2255 struct vnode *vp,
2d21ac55 2256 off_t f_offset,
b0d623f7 2257 int bufsize,
2d21ac55 2258 upl_t *uplp,
0b4e3aa0 2259 upl_page_info_t **plp,
2d21ac55 2260 int uplflags)
0b4e3aa0
A
2261{
2262 memory_object_control_t control;
55e303ae 2263 kern_return_t kr;
b0d623f7
A
2264
2265 if (plp != NULL)
2266 *plp = NULL;
2267 *uplp = NULL;
0b4e3aa0
A
2268
2269 if (bufsize & 0xfff)
2270 return KERN_INVALID_ARGUMENT;
2271
fe8ab488 2272 if (bufsize > MAX_UPL_SIZE_BYTES)
6d2010ae
A
2273 return KERN_INVALID_ARGUMENT;
2274
b0d623f7
A
2275 if (uplflags & (UPL_UBC_MSYNC | UPL_UBC_PAGEOUT | UPL_UBC_PAGEIN)) {
2276
2277 if (uplflags & UPL_UBC_MSYNC) {
2278 uplflags &= UPL_RET_ONLY_DIRTY;
2279
2280 uplflags |= UPL_COPYOUT_FROM | UPL_CLEAN_IN_PLACE |
2281 UPL_SET_INTERNAL | UPL_SET_LITE;
2282
2283 } else if (uplflags & UPL_UBC_PAGEOUT) {
2284 uplflags &= UPL_RET_ONLY_DIRTY;
2285
2286 if (uplflags & UPL_RET_ONLY_DIRTY)
2287 uplflags |= UPL_NOBLOCK;
2288
2289 uplflags |= UPL_FOR_PAGEOUT | UPL_CLEAN_IN_PLACE |
2290 UPL_COPYOUT_FROM | UPL_SET_INTERNAL | UPL_SET_LITE;
2291 } else {
316670eb 2292 uplflags |= UPL_RET_ONLY_ABSENT |
b0d623f7
A
2293 UPL_NO_SYNC | UPL_CLEAN_IN_PLACE |
2294 UPL_SET_INTERNAL | UPL_SET_LITE;
316670eb
A
2295
2296 /*
2297 * if the requested size == PAGE_SIZE, we don't want to set
2298 * the UPL_NOBLOCK since we may be trying to recover from a
2299 * previous partial pagein I/O that occurred because we were low
2300 * on memory and bailed early in order to honor the UPL_NOBLOCK...
2301 * since we're only asking for a single page, we can block w/o fear
2302 * of tying up pages while waiting for more to become available
2303 */
2304 if (bufsize > PAGE_SIZE)
2305 uplflags |= UPL_NOBLOCK;
b0d623f7
A
2306 }
2307 } else {
55e303ae 2308 uplflags &= ~UPL_FOR_PAGEOUT;
55e303ae 2309
b0d623f7
A
2310 if (uplflags & UPL_WILL_BE_DUMPED) {
2311 uplflags &= ~UPL_WILL_BE_DUMPED;
2312 uplflags |= (UPL_NO_SYNC|UPL_SET_INTERNAL);
2313 } else
2314 uplflags |= (UPL_NO_SYNC|UPL_CLEAN_IN_PLACE|UPL_SET_INTERNAL);
2315 }
2316 control = ubc_getobject(vp, UBC_FLAGS_NONE);
0b4e3aa0
A
2317 if (control == MEMORY_OBJECT_CONTROL_NULL)
2318 return KERN_INVALID_ARGUMENT;
2319
b0d623f7
A
2320 kr = memory_object_upl_request(control, f_offset, bufsize, uplp, NULL, NULL, uplflags);
2321 if (kr == KERN_SUCCESS && plp != NULL)
2322 *plp = UPL_GET_INTERNAL_PAGE_LIST(*uplp);
0b4e3aa0
A
2323 return kr;
2324}
2d21ac55
A
2325
2326
2327/*
2328 * ubc_upl_maxbufsize
2329 *
2330 * Return the maximum bufsize ubc_create_upl( ) will take.
2331 *
2332 * Parameters: none
2333 *
2334 * Returns: maximum size buffer (in bytes) ubc_create_upl( ) will take.
2335 */
2336upl_size_t
2337ubc_upl_maxbufsize(
2338 void)
2339{
fe8ab488 2340 return(MAX_UPL_SIZE_BYTES);
2d21ac55 2341}
0b4e3aa0 2342
2d21ac55
A
2343/*
2344 * ubc_upl_map
2345 *
2346 * Map the page list assocated with the supplied upl into the kernel virtual
2347 * address space at the virtual address indicated by the dst_addr argument;
2348 * the entire upl is mapped
2349 *
2350 * Parameters: upl The upl to map
2351 * dst_addr The address at which to map the upl
2352 *
2353 * Returns: KERN_SUCCESS The upl has been mapped
2354 * KERN_INVALID_ARGUMENT The upl is UPL_NULL
2355 * KERN_FAILURE The upl is already mapped
2356 * vm_map_enter:KERN_INVALID_ARGUMENT
2357 * A failure code from vm_map_enter() due
2358 * to an invalid argument
2359 */
0b4e3aa0
A
2360kern_return_t
2361ubc_upl_map(
2362 upl_t upl,
2363 vm_offset_t *dst_addr)
2364{
2365 return (vm_upl_map(kernel_map, upl, dst_addr));
2366}
2367
2368
2d21ac55
A
2369/*
2370 * ubc_upl_unmap
2371 *
2372 * Unmap the page list assocated with the supplied upl from the kernel virtual
2373 * address space; the entire upl is unmapped.
2374 *
2375 * Parameters: upl The upl to unmap
2376 *
2377 * Returns: KERN_SUCCESS The upl has been unmapped
2378 * KERN_FAILURE The upl is not currently mapped
2379 * KERN_INVALID_ARGUMENT If the upl is UPL_NULL
2380 */
0b4e3aa0
A
2381kern_return_t
2382ubc_upl_unmap(
2383 upl_t upl)
2384{
2385 return(vm_upl_unmap(kernel_map, upl));
2386}
2387
2d21ac55
A
2388
2389/*
2390 * ubc_upl_commit
2391 *
2392 * Commit the contents of the upl to the backing store
2393 *
2394 * Parameters: upl The upl to commit
2395 *
2396 * Returns: KERN_SUCCESS The upl has been committed
2397 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2398 * KERN_FAILURE The supplied upl does not represent
2399 * device memory, and the offset plus the
2400 * size would exceed the actual size of
2401 * the upl
2402 *
2403 * Notes: In practice, the only return value for this function should be
2404 * KERN_SUCCESS, unless there has been data structure corruption;
2405 * since the upl is deallocated regardless of success or failure,
2406 * there's really nothing to do about this other than panic.
2407 *
2408 * IMPORTANT: Use of this function should not be mixed with use of
2409 * ubc_upl_commit_range(), due to the unconditional deallocation
2410 * by this function.
2411 */
0b4e3aa0
A
2412kern_return_t
2413ubc_upl_commit(
2414 upl_t upl)
2415{
2416 upl_page_info_t *pl;
2417 kern_return_t kr;
2418
2419 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
fe8ab488 2420 kr = upl_commit(upl, pl, MAX_UPL_SIZE_BYTES >> PAGE_SHIFT);
0b4e3aa0
A
2421 upl_deallocate(upl);
2422 return kr;
1c79356b
A
2423}
2424
0b4e3aa0 2425
2d21ac55
A
2426/*
2427 * ubc_upl_commit
2428 *
2429 * Commit the contents of the specified range of the upl to the backing store
2430 *
2431 * Parameters: upl The upl to commit
2432 * offset The offset into the upl
2433 * size The size of the region to be committed,
2434 * starting at the specified offset
2435 * flags commit type (see below)
2436 *
2437 * Returns: KERN_SUCCESS The range has been committed
2438 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2439 * KERN_FAILURE The supplied upl does not represent
2440 * device memory, and the offset plus the
2441 * size would exceed the actual size of
2442 * the upl
2443 *
2444 * Notes: IMPORTANT: If the commit is successful, and the object is now
2445 * empty, the upl will be deallocated. Since the caller cannot
2446 * check that this is the case, the UPL_COMMIT_FREE_ON_EMPTY flag
2447 * should generally only be used when the offset is 0 and the size
2448 * is equal to the upl size.
2449 *
2450 * The flags argument is a bitmap of flags on the rage of pages in
2451 * the upl to be committed; allowable flags are:
2452 *
2453 * o UPL_COMMIT_FREE_ON_EMPTY Free the upl when it is
2454 * both empty and has been
2455 * successfully committed
2456 * o UPL_COMMIT_CLEAR_DIRTY Clear each pages dirty
2457 * bit; will prevent a
2458 * later pageout
2459 * o UPL_COMMIT_SET_DIRTY Set each pages dirty
2460 * bit; will cause a later
2461 * pageout
2462 * o UPL_COMMIT_INACTIVATE Clear each pages
2463 * reference bit; the page
2464 * will not be accessed
2465 * o UPL_COMMIT_ALLOW_ACCESS Unbusy each page; pages
2466 * become busy when an
2467 * IOMemoryDescriptor is
2468 * mapped or redirected,
2469 * and we have to wait for
2470 * an IOKit driver
2471 *
2472 * The flag UPL_COMMIT_NOTIFY_EMPTY is used internally, and should
2473 * not be specified by the caller.
2474 *
2475 * The UPL_COMMIT_CLEAR_DIRTY and UPL_COMMIT_SET_DIRTY flags are
2476 * mutually exclusive, and should not be combined.
2477 */
0b4e3aa0
A
2478kern_return_t
2479ubc_upl_commit_range(
2480 upl_t upl,
b0d623f7
A
2481 upl_offset_t offset,
2482 upl_size_t size,
0b4e3aa0
A
2483 int flags)
2484{
2485 upl_page_info_t *pl;
2486 boolean_t empty;
2487 kern_return_t kr;
2488
2489 if (flags & UPL_COMMIT_FREE_ON_EMPTY)
2490 flags |= UPL_COMMIT_NOTIFY_EMPTY;
2491
593a1d5f
A
2492 if (flags & UPL_COMMIT_KERNEL_ONLY_FLAGS) {
2493 return KERN_INVALID_ARGUMENT;
2494 }
2495
0b4e3aa0
A
2496 pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
2497
2498 kr = upl_commit_range(upl, offset, size, flags,
fe8ab488 2499 pl, MAX_UPL_SIZE_BYTES >> PAGE_SHIFT, &empty);
0b4e3aa0
A
2500
2501 if((flags & UPL_COMMIT_FREE_ON_EMPTY) && empty)
2502 upl_deallocate(upl);
2503
2504 return kr;
2505}
2d21ac55
A
2506
2507
2508/*
2509 * ubc_upl_abort_range
2510 *
2511 * Abort the contents of the specified range of the specified upl
2512 *
2513 * Parameters: upl The upl to abort
2514 * offset The offset into the upl
2515 * size The size of the region to be aborted,
2516 * starting at the specified offset
2517 * abort_flags abort type (see below)
2518 *
2519 * Returns: KERN_SUCCESS The range has been aborted
2520 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2521 * KERN_FAILURE The supplied upl does not represent
2522 * device memory, and the offset plus the
2523 * size would exceed the actual size of
2524 * the upl
2525 *
2526 * Notes: IMPORTANT: If the abort is successful, and the object is now
2527 * empty, the upl will be deallocated. Since the caller cannot
2528 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2529 * should generally only be used when the offset is 0 and the size
2530 * is equal to the upl size.
2531 *
2532 * The abort_flags argument is a bitmap of flags on the range of
2533 * pages in the upl to be aborted; allowable flags are:
2534 *
2535 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2536 * empty and has been successfully
2537 * aborted
2538 * o UPL_ABORT_RESTART The operation must be restarted
2539 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2540 * o UPL_ABORT_ERROR An I/O error occurred
2541 * o UPL_ABORT_DUMP_PAGES Just free the pages
2542 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2543 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2544 *
2545 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2546 * not be specified by the caller. It is intended to fulfill the
2547 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2548 * ubc_upl_commit_range(), but is never referenced internally.
2549 *
2550 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2551 * referenced; do not use it.
2552 */
0b4e3aa0
A
2553kern_return_t
2554ubc_upl_abort_range(
2555 upl_t upl,
b0d623f7
A
2556 upl_offset_t offset,
2557 upl_size_t size,
0b4e3aa0
A
2558 int abort_flags)
2559{
2560 kern_return_t kr;
2561 boolean_t empty = FALSE;
2562
2563 if (abort_flags & UPL_ABORT_FREE_ON_EMPTY)
2564 abort_flags |= UPL_ABORT_NOTIFY_EMPTY;
2565
2566 kr = upl_abort_range(upl, offset, size, abort_flags, &empty);
2567
2568 if((abort_flags & UPL_ABORT_FREE_ON_EMPTY) && empty)
2569 upl_deallocate(upl);
2570
2571 return kr;
2572}
2573
2d21ac55
A
2574
2575/*
2576 * ubc_upl_abort
2577 *
2578 * Abort the contents of the specified upl
2579 *
2580 * Parameters: upl The upl to abort
2581 * abort_type abort type (see below)
2582 *
2583 * Returns: KERN_SUCCESS The range has been aborted
2584 * KERN_INVALID_ARGUMENT The supplied upl was UPL_NULL
2585 * KERN_FAILURE The supplied upl does not represent
2586 * device memory, and the offset plus the
2587 * size would exceed the actual size of
2588 * the upl
2589 *
2590 * Notes: IMPORTANT: If the abort is successful, and the object is now
2591 * empty, the upl will be deallocated. Since the caller cannot
2592 * check that this is the case, the UPL_ABORT_FREE_ON_EMPTY flag
2593 * should generally only be used when the offset is 0 and the size
2594 * is equal to the upl size.
2595 *
2596 * The abort_type is a bitmap of flags on the range of
2597 * pages in the upl to be aborted; allowable flags are:
2598 *
2599 * o UPL_ABORT_FREE_ON_EMPTY Free the upl when it is both
2600 * empty and has been successfully
2601 * aborted
2602 * o UPL_ABORT_RESTART The operation must be restarted
2603 * o UPL_ABORT_UNAVAILABLE The pages are unavailable
2604 * o UPL_ABORT_ERROR An I/O error occurred
2605 * o UPL_ABORT_DUMP_PAGES Just free the pages
2606 * o UPL_ABORT_NOTIFY_EMPTY RESERVED
2607 * o UPL_ABORT_ALLOW_ACCESS RESERVED
2608 *
2609 * The UPL_ABORT_NOTIFY_EMPTY is an internal use flag and should
2610 * not be specified by the caller. It is intended to fulfill the
2611 * same role as UPL_COMMIT_NOTIFY_EMPTY does in the function
2612 * ubc_upl_commit_range(), but is never referenced internally.
2613 *
2614 * The UPL_ABORT_ALLOW_ACCESS is defined, but neither set nor
2615 * referenced; do not use it.
2616 */
0b4e3aa0
A
2617kern_return_t
2618ubc_upl_abort(
2619 upl_t upl,
2620 int abort_type)
2621{
2622 kern_return_t kr;
2623
2624 kr = upl_abort(upl, abort_type);
2625 upl_deallocate(upl);
2626 return kr;
2627}
2628
2d21ac55
A
2629
2630/*
2631 * ubc_upl_pageinfo
2632 *
2633 * Retrieve the internal page list for the specified upl
2634 *
2635 * Parameters: upl The upl to obtain the page list from
2636 *
2637 * Returns: !NULL The (upl_page_info_t *) for the page
2638 * list internal to the upl
2639 * NULL Error/no page list associated
2640 *
2641 * Notes: IMPORTANT: The function is only valid on internal objects
2642 * where the list request was made with the UPL_INTERNAL flag.
2643 *
2644 * This function is a utility helper function, since some callers
2645 * may not have direct access to the header defining the macro,
2646 * due to abstraction layering constraints.
2647 */
0b4e3aa0
A
2648upl_page_info_t *
2649ubc_upl_pageinfo(
2650 upl_t upl)
2651{
2652 return (UPL_GET_INTERNAL_PAGE_LIST(upl));
2653}
91447636 2654
91447636
A
2655
2656int
fe8ab488 2657UBCINFOEXISTS(const struct vnode * vp)
91447636 2658{
2d21ac55 2659 return((vp) && ((vp)->v_type == VREG) && ((vp)->v_ubcinfo != UBC_INFO_NULL));
91447636
A
2660}
2661
2d21ac55 2662
316670eb
A
2663void
2664ubc_upl_range_needed(
2665 upl_t upl,
2666 int index,
2667 int count)
2668{
2669 upl_range_needed(upl, index, count);
2670}
2671
fe8ab488
A
2672boolean_t ubc_is_mapped(const struct vnode *vp, boolean_t *writable)
2673{
2674 if (!UBCINFOEXISTS(vp) || !ISSET(vp->v_ubcinfo->ui_flags, UI_ISMAPPED))
2675 return FALSE;
2676 if (writable)
2677 *writable = ISSET(vp->v_ubcinfo->ui_flags, UI_MAPPEDWRITE);
2678 return TRUE;
2679}
2680
2681boolean_t ubc_is_mapped_writable(const struct vnode *vp)
2682{
2683 boolean_t writable;
2684 return ubc_is_mapped(vp, &writable) && writable;
2685}
2686
316670eb 2687
2d21ac55
A
2688/*
2689 * CODE SIGNING
2690 */
593a1d5f 2691#define CS_BLOB_PAGEABLE 0
2d21ac55
A
2692static volatile SInt32 cs_blob_size = 0;
2693static volatile SInt32 cs_blob_count = 0;
2694static SInt32 cs_blob_size_peak = 0;
2695static UInt32 cs_blob_size_max = 0;
2696static SInt32 cs_blob_count_peak = 0;
2d21ac55 2697
6d2010ae
A
2698SYSCTL_INT(_vm, OID_AUTO, cs_blob_count, CTLFLAG_RD | CTLFLAG_LOCKED, (int *)(uintptr_t)&cs_blob_count, 0, "Current number of code signature blobs");
2699SYSCTL_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");
2700SYSCTL_INT(_vm, OID_AUTO, cs_blob_count_peak, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_blob_count_peak, 0, "Peak number of code signature blobs");
2701SYSCTL_INT(_vm, OID_AUTO, cs_blob_size_peak, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_blob_size_peak, 0, "Peak size of code signature blobs");
2702SYSCTL_INT(_vm, OID_AUTO, cs_blob_size_max, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_blob_size_max, 0, "Size of biggest code signature blob");
2d21ac55 2703
3e170ce0
A
2704/*
2705 * Function: csblob_parse_teamid
2706 *
2707 * Description: This function returns a pointer to the team id
2708 stored within the codedirectory of the csblob.
2709 If the codedirectory predates team-ids, it returns
2710 NULL.
2711 This does not copy the name but returns a pointer to
2712 it within the CD. Subsequently, the CD must be
2713 available when this is used.
2714*/
2715
2716static const char *
2717csblob_parse_teamid(struct cs_blob *csblob)
2718{
2719 const CS_CodeDirectory *cd;
2720
2721 if ((cd = (const CS_CodeDirectory *)csblob_find_blob(
2722 csblob, CSSLOT_CODEDIRECTORY, CSMAGIC_CODEDIRECTORY)) == NULL)
2723 return NULL;
2724
2725 if (ntohl(cd->version) < CS_SUPPORTSTEAMID)
2726 return NULL;
2727
2728 if (cd->teamOffset == 0)
2729 return NULL;
2730
2731 const char *name = ((const char *)cd) + ntohl(cd->teamOffset);
2732 if (cs_debug > 1)
2733 printf("found team-id %s in cdblob\n", name);
2734
2735 return name;
2736}
2737
39236c6e 2738
593a1d5f
A
2739kern_return_t
2740ubc_cs_blob_allocate(
2741 vm_offset_t *blob_addr_p,
2742 vm_size_t *blob_size_p)
2743{
2744 kern_return_t kr;
2745
2746#if CS_BLOB_PAGEABLE
2747 *blob_size_p = round_page(*blob_size_p);
3e170ce0 2748 kr = kmem_alloc(kernel_map, blob_addr_p, *blob_size_p, VM_KERN_MEMORY_SECURITY);
593a1d5f 2749#else /* CS_BLOB_PAGEABLE */
3e170ce0 2750 *blob_addr_p = (vm_offset_t) kalloc_tag(*blob_size_p, VM_KERN_MEMORY_SECURITY);
593a1d5f
A
2751 if (*blob_addr_p == 0) {
2752 kr = KERN_NO_SPACE;
2753 } else {
2754 kr = KERN_SUCCESS;
2755 }
2756#endif /* CS_BLOB_PAGEABLE */
2757 return kr;
2758}
2759
2760void
2761ubc_cs_blob_deallocate(
2762 vm_offset_t blob_addr,
2763 vm_size_t blob_size)
2764{
2765#if CS_BLOB_PAGEABLE
2766 kmem_free(kernel_map, blob_addr, blob_size);
2767#else /* CS_BLOB_PAGEABLE */
2768 kfree((void *) blob_addr, blob_size);
2769#endif /* CS_BLOB_PAGEABLE */
2770}
39236c6e 2771
2d21ac55
A
2772int
2773ubc_cs_blob_add(
2774 struct vnode *vp,
2775 cpu_type_t cputype,
2776 off_t base_offset,
2777 vm_address_t addr,
c18c124e 2778 vm_size_t size,
3e170ce0
A
2779 __unused int flags,
2780 struct cs_blob **ret_blob)
91447636 2781{
2d21ac55
A
2782 kern_return_t kr;
2783 struct ubc_info *uip;
2784 struct cs_blob *blob, *oblob;
2785 int error;
2786 ipc_port_t blob_handle;
2787 memory_object_size_t blob_size;
2788 const CS_CodeDirectory *cd;
2789 off_t blob_start_offset, blob_end_offset;
3e170ce0 2790 union cs_hash_union mdctx;
15129b1c 2791 boolean_t record_mtime;
3e170ce0 2792 int cs_flags;
15129b1c
A
2793
2794 record_mtime = FALSE;
3e170ce0
A
2795 cs_flags = 0;
2796 if (ret_blob)
2797 *ret_blob = NULL;
2d21ac55
A
2798
2799 blob_handle = IPC_PORT_NULL;
2800
2801 blob = (struct cs_blob *) kalloc(sizeof (struct cs_blob));
2802 if (blob == NULL) {
2803 return ENOMEM;
2804 }
2805
593a1d5f 2806#if CS_BLOB_PAGEABLE
2d21ac55
A
2807 /* get a memory entry on the blob */
2808 blob_size = (memory_object_size_t) size;
2809 kr = mach_make_memory_entry_64(kernel_map,
2810 &blob_size,
2811 addr,
2812 VM_PROT_READ,
2813 &blob_handle,
2814 IPC_PORT_NULL);
2815 if (kr != KERN_SUCCESS) {
2816 error = ENOMEM;
2817 goto out;
2818 }
2819 if (memory_object_round_page(blob_size) !=
2820 (memory_object_size_t) round_page(size)) {
b0d623f7
A
2821 printf("ubc_cs_blob_add: size mismatch 0x%llx 0x%lx !?\n",
2822 blob_size, (size_t)size);
2823 panic("XXX FBDP size mismatch 0x%llx 0x%lx\n", blob_size, (size_t)size);
2d21ac55
A
2824 error = EINVAL;
2825 goto out;
2826 }
593a1d5f
A
2827#else
2828 blob_size = (memory_object_size_t) size;
2829 blob_handle = IPC_PORT_NULL;
2830#endif
2d21ac55
A
2831
2832 /* fill in the new blob */
2833 blob->csb_cpu_type = cputype;
2834 blob->csb_base_offset = base_offset;
2835 blob->csb_mem_size = size;
2836 blob->csb_mem_offset = 0;
2837 blob->csb_mem_handle = blob_handle;
2838 blob->csb_mem_kaddr = addr;
39236c6e 2839 blob->csb_flags = 0;
fe8ab488 2840 blob->csb_platform_binary = 0;
3e170ce0 2841 blob->csb_platform_path = 0;
fe8ab488 2842 blob->csb_teamid = NULL;
2d21ac55
A
2843
2844 /*
2845 * Validate the blob's contents
2846 */
39236c6e
A
2847
2848 error = cs_validate_csblob((const uint8_t *)addr, size, &cd);
2849 if (error) {
2850 if (cs_debug)
2851 printf("CODESIGNING: csblob invalid: %d\n", error);
2d21ac55
A
2852 blob->csb_flags = 0;
2853 blob->csb_start_offset = 0;
2854 blob->csb_end_offset = 0;
3e170ce0 2855 memset(blob->csb_cdhash, 0, sizeof(blob->csb_cdhash));
39236c6e 2856 /* let the vnode checker determine if the signature is valid or not */
2d21ac55 2857 } else {
3e170ce0
A
2858 const unsigned char *md_base;
2859 uint8_t hash[CS_HASH_MAX_SIZE];
2860 int md_size;
2861
2862 blob->csb_hashtype = cs_find_md(cd->hashType);
2863 if (blob->csb_hashtype == NULL || blob->csb_hashtype->cs_digest_size > sizeof(hash))
2864 panic("validated CodeDirectory but unsupported type");
2865 if (blob->csb_hashtype->cs_cd_size < CS_CDHASH_LEN) {
2866 if (cs_debug)
2867 printf("cs_cd_size is too small for a cdhash\n");
2868 error = EINVAL;
2869 goto out;
2870 }
2871
39236c6e 2872 blob->csb_flags = (ntohl(cd->flags) & CS_ALLOWED_MACHO) | CS_VALID;
fe8ab488 2873 blob->csb_end_offset = round_page_4K(ntohl(cd->codeLimit));
39236c6e
A
2874 if((ntohl(cd->version) >= CS_SUPPORTSSCATTER) && (ntohl(cd->scatterOffset))) {
2875 const SC_Scatter *scatter = (const SC_Scatter*)
b0d623f7 2876 ((const char*)cd + ntohl(cd->scatterOffset));
fe8ab488 2877 blob->csb_start_offset = ntohl(scatter->base) * PAGE_SIZE_4K;
b0d623f7 2878 } else {
3e170ce0 2879 blob->csb_start_offset = 0;
b0d623f7 2880 }
3e170ce0
A
2881 /* compute the blob's cdhash */
2882 md_base = (const unsigned char *) cd;
2883 md_size = ntohl(cd->length);
2884
2885 blob->csb_hashtype->cs_init(&mdctx);
2886 blob->csb_hashtype->cs_update(&mdctx, md_base, md_size);
2887 blob->csb_hashtype->cs_final(hash, &mdctx);
2888
2889 memcpy(blob->csb_cdhash, hash, CS_CDHASH_LEN);
2d21ac55
A
2890 }
2891
593a1d5f
A
2892 /*
2893 * Let policy module check whether the blob's signature is accepted.
2894 */
2895#if CONFIG_MACF
c18c124e
A
2896 error = mac_vnode_check_signature(vp,
2897 base_offset,
3e170ce0
A
2898 blob->csb_cdhash,
2899 (const void*)addr, size,
2900 flags, &cs_flags);
fe8ab488
A
2901 if (error) {
2902 if (cs_debug)
2903 printf("check_signature[pid: %d], error = %d\n", current_proc()->p_pid, error);
593a1d5f 2904 goto out;
fe8ab488 2905 }
3e170ce0 2906 if ((flags & MAC_VNODE_CHECK_DYLD_SIM) && !(cs_flags & CS_PLATFORM_BINARY)) {
c18c124e
A
2907 if (cs_debug)
2908 printf("check_signature[pid: %d], is not apple signed\n", current_proc()->p_pid);
2909 error = EPERM;
2910 goto out;
2911 }
593a1d5f
A
2912#endif
2913
3e170ce0 2914 if (cs_flags & CS_PLATFORM_BINARY) {
fe8ab488
A
2915 if (cs_debug > 1)
2916 printf("check_signature[pid: %d]: platform binary\n", current_proc()->p_pid);
2917 blob->csb_platform_binary = 1;
3e170ce0 2918 blob->csb_platform_path = !!(cs_flags & CS_PLATFORM_PATH);
fe8ab488
A
2919 } else {
2920 blob->csb_platform_binary = 0;
3e170ce0
A
2921 blob->csb_platform_path = 0;
2922 blob->csb_teamid = csblob_parse_teamid(blob);
fe8ab488
A
2923 if (cs_debug > 1) {
2924 if (blob->csb_teamid)
2925 printf("check_signature[pid: %d]: team-id is %s\n", current_proc()->p_pid, blob->csb_teamid);
2926 else
2927 printf("check_signature[pid: %d]: no team-id\n", current_proc()->p_pid);
2928 }
2929 }
2930
2d21ac55
A
2931 /*
2932 * Validate the blob's coverage
2933 */
2934 blob_start_offset = blob->csb_base_offset + blob->csb_start_offset;
2935 blob_end_offset = blob->csb_base_offset + blob->csb_end_offset;
2936
cf7d32b8
A
2937 if (blob_start_offset >= blob_end_offset ||
2938 blob_start_offset < 0 ||
2939 blob_end_offset <= 0) {
2d21ac55
A
2940 /* reject empty or backwards blob */
2941 error = EINVAL;
2942 goto out;
2943 }
2944
2945 vnode_lock(vp);
2946 if (! UBCINFOEXISTS(vp)) {
2947 vnode_unlock(vp);
2948 error = ENOENT;
2949 goto out;
2950 }
2951 uip = vp->v_ubcinfo;
2952
2953 /* check if this new blob overlaps with an existing blob */
2954 for (oblob = uip->cs_blobs;
2955 oblob != NULL;
2956 oblob = oblob->csb_next) {
2957 off_t oblob_start_offset, oblob_end_offset;
2958
fe8ab488
A
2959 /* check for conflicting teamid */
2960 if (blob->csb_platform_binary) { //platform binary needs to be the same for app slices
2961 if (!oblob->csb_platform_binary) {
2962 vnode_unlock(vp);
2963 error = EALREADY;
2964 goto out;
2965 }
2966 } else if (blob->csb_teamid) { //teamid binary needs to be the same for app slices
2967 if (oblob->csb_platform_binary ||
2968 oblob->csb_teamid == NULL ||
2969 strcmp(oblob->csb_teamid, blob->csb_teamid) != 0) {
2970 vnode_unlock(vp);
2971 error = EALREADY;
2972 goto out;
2973 }
2974 } else { // non teamid binary needs to be the same for app slices
2975 if (oblob->csb_platform_binary ||
2976 oblob->csb_teamid != NULL) {
2977 vnode_unlock(vp);
2978 error = EALREADY;
2979 goto out;
2980 }
2981 }
2982
2d21ac55
A
2983 oblob_start_offset = (oblob->csb_base_offset +
2984 oblob->csb_start_offset);
2985 oblob_end_offset = (oblob->csb_base_offset +
2986 oblob->csb_end_offset);
2987 if (blob_start_offset >= oblob_end_offset ||
2988 blob_end_offset <= oblob_start_offset) {
2989 /* no conflict with this existing blob */
2990 } else {
2991 /* conflict ! */
2992 if (blob_start_offset == oblob_start_offset &&
2993 blob_end_offset == oblob_end_offset &&
2994 blob->csb_mem_size == oblob->csb_mem_size &&
2995 blob->csb_flags == oblob->csb_flags &&
2996 (blob->csb_cpu_type == CPU_TYPE_ANY ||
2997 oblob->csb_cpu_type == CPU_TYPE_ANY ||
2998 blob->csb_cpu_type == oblob->csb_cpu_type) &&
3e170ce0
A
2999 !bcmp(blob->csb_cdhash,
3000 oblob->csb_cdhash,
3001 CS_CDHASH_LEN)) {
2d21ac55
A
3002 /*
3003 * We already have this blob:
3004 * we'll return success but
3005 * throw away the new blob.
3006 */
3007 if (oblob->csb_cpu_type == CPU_TYPE_ANY) {
3008 /*
3009 * The old blob matches this one
3010 * but doesn't have any CPU type.
3011 * Update it with whatever the caller
3012 * provided this time.
3013 */
3014 oblob->csb_cpu_type = cputype;
3015 }
3016 vnode_unlock(vp);
3e170ce0
A
3017 if (ret_blob)
3018 *ret_blob = oblob;
2d21ac55
A
3019 error = EAGAIN;
3020 goto out;
3021 } else {
3022 /* different blob: reject the new one */
3023 vnode_unlock(vp);
3024 error = EALREADY;
3025 goto out;
3026 }
3027 }
3028
3029 }
3030
fe8ab488 3031
2d21ac55
A
3032 /* mark this vnode's VM object as having "signed pages" */
3033 kr = memory_object_signed(uip->ui_control, TRUE);
3034 if (kr != KERN_SUCCESS) {
3035 vnode_unlock(vp);
3036 error = ENOENT;
3037 goto out;
3038 }
3039
15129b1c
A
3040 if (uip->cs_blobs == NULL) {
3041 /* loading 1st blob: record the file's current "modify time" */
3042 record_mtime = TRUE;
3043 }
3044
fe8ab488
A
3045 /* set the generation count for cs_blobs */
3046 uip->cs_add_gen = cs_blob_generation_count;
3047
2d21ac55
A
3048 /*
3049 * Add this blob to the list of blobs for this vnode.
3050 * We always add at the front of the list and we never remove a
3051 * blob from the list, so ubc_cs_get_blobs() can return whatever
3052 * the top of the list was and that list will remain valid
3053 * while we validate a page, even after we release the vnode's lock.
3054 */
3055 blob->csb_next = uip->cs_blobs;
3056 uip->cs_blobs = blob;
3057
3058 OSAddAtomic(+1, &cs_blob_count);
3059 if (cs_blob_count > cs_blob_count_peak) {
3060 cs_blob_count_peak = cs_blob_count; /* XXX atomic ? */
3061 }
b0d623f7
A
3062 OSAddAtomic((SInt32) +blob->csb_mem_size, &cs_blob_size);
3063 if ((SInt32) cs_blob_size > cs_blob_size_peak) {
3064 cs_blob_size_peak = (SInt32) cs_blob_size; /* XXX atomic ? */
2d21ac55 3065 }
b0d623f7
A
3066 if ((UInt32) blob->csb_mem_size > cs_blob_size_max) {
3067 cs_blob_size_max = (UInt32) blob->csb_mem_size;
2d21ac55
A
3068 }
3069
c331a0be 3070 if (cs_debug > 1) {
2d21ac55 3071 proc_t p;
39236c6e 3072 const char *name = vnode_getname_printable(vp);
2d21ac55
A
3073 p = current_proc();
3074 printf("CODE SIGNING: proc %d(%s) "
3075 "loaded %s signatures for file (%s) "
3076 "range 0x%llx:0x%llx flags 0x%x\n",
3077 p->p_pid, p->p_comm,
3078 blob->csb_cpu_type == -1 ? "detached" : "embedded",
39236c6e 3079 name,
2d21ac55
A
3080 blob->csb_base_offset + blob->csb_start_offset,
3081 blob->csb_base_offset + blob->csb_end_offset,
3082 blob->csb_flags);
39236c6e 3083 vnode_putname_printable(name);
2d21ac55
A
3084 }
3085
2d21ac55
A
3086 vnode_unlock(vp);
3087
15129b1c
A
3088 if (record_mtime) {
3089 vnode_mtime(vp, &uip->cs_mtime, vfs_context_current());
3090 }
3091
3e170ce0
A
3092 if (ret_blob)
3093 *ret_blob = blob;
3094
2d21ac55
A
3095 error = 0; /* success ! */
3096
3097out:
3098 if (error) {
fe8ab488
A
3099 if (cs_debug)
3100 printf("check_signature[pid: %d]: error = %d\n", current_proc()->p_pid, error);
3101
2d21ac55
A
3102 /* we failed; release what we allocated */
3103 if (blob) {
3104 kfree(blob, sizeof (*blob));
3105 blob = NULL;
3106 }
3107 if (blob_handle != IPC_PORT_NULL) {
3108 mach_memory_entry_port_release(blob_handle);
3109 blob_handle = IPC_PORT_NULL;
3110 }
2d21ac55
A
3111 }
3112
3113 if (error == EAGAIN) {
3114 /*
3115 * See above: error is EAGAIN if we were asked
3116 * to add an existing blob again. We cleaned the new
3117 * blob and we want to return success.
3118 */
3119 error = 0;
3120 /*
3121 * Since we're not failing, consume the data we received.
3122 */
593a1d5f 3123 ubc_cs_blob_deallocate(addr, size);
2d21ac55
A
3124 }
3125
3126 return error;
91447636
A
3127}
3128
3e170ce0
A
3129void
3130csvnode_print_debug(struct vnode *vp)
3131{
3132 const char *name = NULL;
3133 struct ubc_info *uip;
3134 struct cs_blob *blob;
3135
3136 name = vnode_getname_printable(vp);
3137 if (name) {
3138 printf("csvnode: name: %s\n", name);
3139 vnode_putname_printable(name);
3140 }
3141
3142 vnode_lock_spin(vp);
3143
3144 if (! UBCINFOEXISTS(vp)) {
3145 blob = NULL;
3146 goto out;
3147 }
3148
3149 uip = vp->v_ubcinfo;
3150 for (blob = uip->cs_blobs; blob != NULL; blob = blob->csb_next) {
3151 printf("csvnode: range: %lu -> %lu flags: 0x%08x platform: %s path: %s team: %s\n",
3152 (unsigned long)blob->csb_start_offset,
3153 (unsigned long)blob->csb_end_offset,
3154 blob->csb_flags,
3155 blob->csb_platform_binary ? "yes" : "no",
3156 blob->csb_platform_path ? "yes" : "no",
3157 blob->csb_teamid ? blob->csb_teamid : "<NO-TEAM>");
3158 }
3159
3160out:
3161 vnode_unlock(vp);
3162
3163}
3164
2d21ac55
A
3165struct cs_blob *
3166ubc_cs_blob_get(
3167 struct vnode *vp,
3168 cpu_type_t cputype,
3169 off_t offset)
91447636 3170{
2d21ac55
A
3171 struct ubc_info *uip;
3172 struct cs_blob *blob;
3173 off_t offset_in_blob;
3174
3175 vnode_lock_spin(vp);
3176
3177 if (! UBCINFOEXISTS(vp)) {
3178 blob = NULL;
3179 goto out;
3180 }
3181
3182 uip = vp->v_ubcinfo;
3183 for (blob = uip->cs_blobs;
3184 blob != NULL;
3185 blob = blob->csb_next) {
3186 if (cputype != -1 && blob->csb_cpu_type == cputype) {
3187 break;
3188 }
3189 if (offset != -1) {
3190 offset_in_blob = offset - blob->csb_base_offset;
3191 if (offset_in_blob >= blob->csb_start_offset &&
3192 offset_in_blob < blob->csb_end_offset) {
3193 /* our offset is covered by this blob */
3194 break;
3195 }
3196 }
3197 }
3198
3199out:
3200 vnode_unlock(vp);
3201
3202 return blob;
91447636 3203}
2d21ac55
A
3204
3205static void
3206ubc_cs_free(
3207 struct ubc_info *uip)
91447636 3208{
2d21ac55
A
3209 struct cs_blob *blob, *next_blob;
3210
3211 for (blob = uip->cs_blobs;
3212 blob != NULL;
3213 blob = next_blob) {
3214 next_blob = blob->csb_next;
3e170ce0 3215 if (blob->csb_mem_kaddr != 0) {
593a1d5f
A
3216 ubc_cs_blob_deallocate(blob->csb_mem_kaddr,
3217 blob->csb_mem_size);
2d21ac55
A
3218 blob->csb_mem_kaddr = 0;
3219 }
593a1d5f
A
3220 if (blob->csb_mem_handle != IPC_PORT_NULL) {
3221 mach_memory_entry_port_release(blob->csb_mem_handle);
3222 }
2d21ac55
A
3223 blob->csb_mem_handle = IPC_PORT_NULL;
3224 OSAddAtomic(-1, &cs_blob_count);
b0d623f7 3225 OSAddAtomic((SInt32) -blob->csb_mem_size, &cs_blob_size);
2d21ac55
A
3226 kfree(blob, sizeof (*blob));
3227 }
6d2010ae
A
3228#if CHECK_CS_VALIDATION_BITMAP
3229 ubc_cs_validation_bitmap_deallocate( uip->ui_vnode );
3230#endif
2d21ac55 3231 uip->cs_blobs = NULL;
91447636 3232}
2d21ac55 3233
fe8ab488
A
3234/* check cs blob generation on vnode
3235 * returns:
3236 * 0 : Success, the cs_blob attached is current
3237 * ENEEDAUTH : Generation count mismatch. Needs authentication again.
3238 */
3239int
3240ubc_cs_generation_check(
3241 struct vnode *vp)
3242{
3243 int retval = ENEEDAUTH;
3244
3245 vnode_lock_spin(vp);
3246
3247 if (UBCINFOEXISTS(vp) && vp->v_ubcinfo->cs_add_gen == cs_blob_generation_count) {
3248 retval = 0;
3249 }
3250
3251 vnode_unlock(vp);
3252 return retval;
3253}
3254
3255int
3256ubc_cs_blob_revalidate(
3257 struct vnode *vp,
c18c124e
A
3258 struct cs_blob *blob,
3259 __unused int flags
fe8ab488
A
3260 )
3261{
3262 int error = 0;
3263#if CONFIG_MACF
3e170ce0 3264 int cs_flags = 0;
fe8ab488
A
3265#endif
3266 const CS_CodeDirectory *cd = NULL;
3267
3268 assert(vp != NULL);
3269 assert(blob != NULL);
3270
3271 error = cs_validate_csblob((const uint8_t *)blob->csb_mem_kaddr, blob->csb_mem_size, &cd);
3272 if (error) {
3273 if (cs_debug) {
3274 printf("CODESIGNING: csblob invalid: %d\n", error);
3275 }
3276 goto out;
3277 }
3278
3279 /* callout to mac_vnode_check_signature */
3280#if CONFIG_MACF
3e170ce0
A
3281 error = mac_vnode_check_signature(vp, blob->csb_base_offset, blob->csb_cdhash,
3282 (const void*)blob->csb_mem_kaddr, (int)blob->csb_mem_size,
3283 flags, &cs_flags);
fe8ab488
A
3284 if (cs_debug && error) {
3285 printf("revalidate: check_signature[pid: %d], error = %d\n", current_proc()->p_pid, error);
3286 }
3287#endif
3288
3289 /* update generation number if success */
3290 vnode_lock_spin(vp);
3291 if (UBCINFOEXISTS(vp)) {
3292 if (error == 0)
3293 vp->v_ubcinfo->cs_add_gen = cs_blob_generation_count;
3294 else
3295 vp->v_ubcinfo->cs_add_gen = 0;
3296 }
3297
3298 vnode_unlock(vp);
3299
3300out:
3301 return error;
3302}
3303
3304void
3305cs_blob_reset_cache()
3306{
3307 /* incrementing odd no by 2 makes sure '0' is never reached. */
3308 OSAddAtomic(+2, &cs_blob_generation_count);
3309 printf("Reseting cs_blob cache from all vnodes. \n");
3310}
3311
2d21ac55
A
3312struct cs_blob *
3313ubc_get_cs_blobs(
3314 struct vnode *vp)
91447636 3315{
2d21ac55
A
3316 struct ubc_info *uip;
3317 struct cs_blob *blobs;
3318
b0d623f7
A
3319 /*
3320 * No need to take the vnode lock here. The caller must be holding
3321 * a reference on the vnode (via a VM mapping or open file descriptor),
3322 * so the vnode will not go away. The ubc_info stays until the vnode
3323 * goes away. And we only modify "blobs" by adding to the head of the
3324 * list.
3325 * The ubc_info could go away entirely if the vnode gets reclaimed as
3326 * part of a forced unmount. In the case of a code-signature validation
3327 * during a page fault, the "paging_in_progress" reference on the VM
3328 * object guarantess that the vnode pager (and the ubc_info) won't go
3329 * away during the fault.
3330 * Other callers need to protect against vnode reclaim by holding the
3331 * vnode lock, for example.
3332 */
2d21ac55
A
3333
3334 if (! UBCINFOEXISTS(vp)) {
3335 blobs = NULL;
3336 goto out;
3337 }
3338
3339 uip = vp->v_ubcinfo;
3340 blobs = uip->cs_blobs;
3341
3342out:
2d21ac55 3343 return blobs;
91447636 3344}
2d21ac55 3345
15129b1c
A
3346void
3347ubc_get_cs_mtime(
3348 struct vnode *vp,
3349 struct timespec *cs_mtime)
3350{
3351 struct ubc_info *uip;
3352
3353 if (! UBCINFOEXISTS(vp)) {
3354 cs_mtime->tv_sec = 0;
3355 cs_mtime->tv_nsec = 0;
3356 return;
3357 }
3358
3359 uip = vp->v_ubcinfo;
3360 cs_mtime->tv_sec = uip->cs_mtime.tv_sec;
3361 cs_mtime->tv_nsec = uip->cs_mtime.tv_nsec;
3362}
3363
2d21ac55
A
3364unsigned long cs_validate_page_no_hash = 0;
3365unsigned long cs_validate_page_bad_hash = 0;
3366boolean_t
3367cs_validate_page(
3368 void *_blobs,
316670eb 3369 memory_object_t pager,
2d21ac55
A
3370 memory_object_offset_t page_offset,
3371 const void *data,
c18c124e 3372 unsigned *tainted)
91447636 3373{
3e170ce0
A
3374 union cs_hash_union mdctx;
3375 struct cs_hash *hashtype = NULL;
3376 unsigned char actual_hash[CS_HASH_MAX_SIZE];
2d21ac55
A
3377 unsigned char expected_hash[SHA1_RESULTLEN];
3378 boolean_t found_hash;
3379 struct cs_blob *blobs, *blob;
3380 const CS_CodeDirectory *cd;
3381 const CS_SuperBlob *embedded;
2d21ac55
A
3382 const unsigned char *hash;
3383 boolean_t validated;
3384 off_t offset; /* page offset in the file */
3385 size_t size;
3386 off_t codeLimit = 0;
3e170ce0 3387 const char *lower_bound, *upper_bound;
2d21ac55
A
3388 vm_offset_t kaddr, blob_addr;
3389 vm_size_t ksize;
3390 kern_return_t kr;
3391
3392 offset = page_offset;
3393
3394 /* retrieve the expected hash */
3395 found_hash = FALSE;
3396 blobs = (struct cs_blob *) _blobs;
3397
3398 for (blob = blobs;
3399 blob != NULL;
3400 blob = blob->csb_next) {
3401 offset = page_offset - blob->csb_base_offset;
3402 if (offset < blob->csb_start_offset ||
3403 offset >= blob->csb_end_offset) {
3404 /* our page is not covered by this blob */
3405 continue;
3406 }
3407
3408 /* map the blob in the kernel address space */
3409 kaddr = blob->csb_mem_kaddr;
3410 if (kaddr == 0) {
3411 ksize = (vm_size_t) (blob->csb_mem_size +
3412 blob->csb_mem_offset);
3413 kr = vm_map(kernel_map,
3414 &kaddr,
3415 ksize,
3416 0,
3417 VM_FLAGS_ANYWHERE,
3418 blob->csb_mem_handle,
3419 0,
3420 TRUE,
3421 VM_PROT_READ,
3422 VM_PROT_READ,
3423 VM_INHERIT_NONE);
3424 if (kr != KERN_SUCCESS) {
3425 /* XXX FBDP what to do !? */
3426 printf("cs_validate_page: failed to map blob, "
b0d623f7
A
3427 "size=0x%lx kr=0x%x\n",
3428 (size_t)blob->csb_mem_size, kr);
2d21ac55
A
3429 break;
3430 }
3431 }
39236c6e 3432
2d21ac55
A
3433 blob_addr = kaddr + blob->csb_mem_offset;
3434
3435 lower_bound = CAST_DOWN(char *, blob_addr);
3436 upper_bound = lower_bound + blob->csb_mem_size;
3437
3438 embedded = (const CS_SuperBlob *) blob_addr;
3439 cd = findCodeDirectory(embedded, lower_bound, upper_bound);
3440 if (cd != NULL) {
3e170ce0 3441 /* all CD's that have been injected is already validated */
b0d623f7 3442
2d21ac55 3443 offset = page_offset - blob->csb_base_offset;
b0d623f7
A
3444 if (offset < blob->csb_start_offset ||
3445 offset >= blob->csb_end_offset) {
2d21ac55 3446 /* our page is not covered by this blob */
2d21ac55
A
3447 continue;
3448 }
3449
3e170ce0
A
3450 hashtype = blob->csb_hashtype;
3451 if (hashtype == NULL)
3452 panic("unknown hash type ?");
3453 if (hashtype->cs_digest_size > sizeof(actual_hash))
3454 panic("hash size too large");
3455
2d21ac55 3456 codeLimit = ntohl(cd->codeLimit);
39236c6e 3457
3e170ce0
A
3458 hash = hashes(cd, (uint32_t)(offset>>PAGE_SHIFT_4K),
3459 hashtype->cs_size,
2d21ac55 3460 lower_bound, upper_bound);
cf7d32b8 3461 if (hash != NULL) {
3e170ce0 3462 bcopy(hash, expected_hash, sizeof(expected_hash));
cf7d32b8
A
3463 found_hash = TRUE;
3464 }
2d21ac55 3465
2d21ac55
A
3466 break;
3467 }
3468 }
3469
3470 if (found_hash == FALSE) {
3471 /*
3472 * We can't verify this page because there is no signature
3473 * for it (yet). It's possible that this part of the object
3474 * is not signed, or that signatures for that part have not
3475 * been loaded yet.
3476 * Report that the page has not been validated and let the
3477 * caller decide if it wants to accept it or not.
3478 */
3479 cs_validate_page_no_hash++;
3480 if (cs_debug > 1) {
3481 printf("CODE SIGNING: cs_validate_page: "
316670eb
A
3482 "mobj %p off 0x%llx: no hash to validate !?\n",
3483 pager, page_offset);
2d21ac55
A
3484 }
3485 validated = FALSE;
c18c124e 3486 *tainted = 0;
2d21ac55 3487 } else {
2d21ac55 3488
c18c124e
A
3489 *tainted = 0;
3490
fe8ab488
A
3491 size = PAGE_SIZE_4K;
3492 const uint32_t *asha1, *esha1;
b0d623f7 3493 if ((off_t)(offset + size) > codeLimit) {
2d21ac55
A
3494 /* partial page at end of segment */
3495 assert(offset < codeLimit);
fe8ab488 3496 size = (size_t) (codeLimit & PAGE_MASK_4K);
c18c124e 3497 *tainted |= CS_VALIDATE_NX;
2d21ac55 3498 }
3e170ce0
A
3499
3500 hashtype->cs_init(&mdctx);
3501 hashtype->cs_update(&mdctx, data, size);
3502 hashtype->cs_final(actual_hash, &mdctx);
2d21ac55 3503
fe8ab488
A
3504 asha1 = (const uint32_t *) actual_hash;
3505 esha1 = (const uint32_t *) expected_hash;
3506
3e170ce0 3507 if (bcmp(expected_hash, actual_hash, hashtype->cs_cd_size) != 0) {
2d21ac55
A
3508 if (cs_debug) {
3509 printf("CODE SIGNING: cs_validate_page: "
fe8ab488
A
3510 "mobj %p off 0x%llx size 0x%lx: "
3511 "actual [0x%x 0x%x 0x%x 0x%x 0x%x] != "
3512 "expected [0x%x 0x%x 0x%x 0x%x 0x%x]\n",
3513 pager, page_offset, size,
3514 asha1[0], asha1[1], asha1[2],
3515 asha1[3], asha1[4],
3516 esha1[0], esha1[1], esha1[2],
3517 esha1[3], esha1[4]);
2d21ac55
A
3518 }
3519 cs_validate_page_bad_hash++;
c18c124e 3520 *tainted |= CS_VALIDATE_TAINTED;
2d21ac55 3521 } else {
39236c6e 3522 if (cs_debug > 10) {
2d21ac55 3523 printf("CODE SIGNING: cs_validate_page: "
316670eb
A
3524 "mobj %p off 0x%llx size 0x%lx: "
3525 "SHA1 OK\n",
3526 pager, page_offset, size);
2d21ac55 3527 }
2d21ac55
A
3528 }
3529 validated = TRUE;
3530 }
3531
3532 return validated;
91447636
A
3533}
3534
2d21ac55
A
3535int
3536ubc_cs_getcdhash(
3537 vnode_t vp,
3538 off_t offset,
3539 unsigned char *cdhash)
3540{
b0d623f7
A
3541 struct cs_blob *blobs, *blob;
3542 off_t rel_offset;
3543 int ret;
3544
3545 vnode_lock(vp);
2d21ac55
A
3546
3547 blobs = ubc_get_cs_blobs(vp);
3548 for (blob = blobs;
3549 blob != NULL;
3550 blob = blob->csb_next) {
3551 /* compute offset relative to this blob */
3552 rel_offset = offset - blob->csb_base_offset;
3553 if (rel_offset >= blob->csb_start_offset &&
3554 rel_offset < blob->csb_end_offset) {
3555 /* this blob does cover our "offset" ! */
3556 break;
3557 }
3558 }
3559
3560 if (blob == NULL) {
3561 /* we didn't find a blob covering "offset" */
b0d623f7
A
3562 ret = EBADEXEC; /* XXX any better error ? */
3563 } else {
3564 /* get the SHA1 hash of that blob */
3e170ce0 3565 bcopy(blob->csb_cdhash, cdhash, sizeof (blob->csb_cdhash));
b0d623f7 3566 ret = 0;
2d21ac55
A
3567 }
3568
b0d623f7 3569 vnode_unlock(vp);
2d21ac55 3570
b0d623f7 3571 return ret;
2d21ac55 3572}
6d2010ae
A
3573
3574#if CHECK_CS_VALIDATION_BITMAP
3575#define stob(s) ((atop_64((s)) + 07) >> 3)
3576extern boolean_t root_fs_upgrade_try;
3577
3578/*
3579 * Should we use the code-sign bitmap to avoid repeated code-sign validation?
3580 * Depends:
3581 * a) Is the target vnode on the root filesystem?
3582 * b) Has someone tried to mount the root filesystem read-write?
3583 * If answers are (a) yes AND (b) no, then we can use the bitmap.
3584 */
3585#define USE_CODE_SIGN_BITMAP(vp) ( (vp != NULL) && (vp->v_mount != NULL) && (vp->v_mount->mnt_flag & MNT_ROOTFS) && !root_fs_upgrade_try)
3586kern_return_t
3587ubc_cs_validation_bitmap_allocate(
3588 vnode_t vp)
3589{
3590 kern_return_t kr = KERN_SUCCESS;
3591 struct ubc_info *uip;
3592 char *target_bitmap;
3593 vm_object_size_t bitmap_size;
3594
3595 if ( ! USE_CODE_SIGN_BITMAP(vp) || (! UBCINFOEXISTS(vp))) {
3596 kr = KERN_INVALID_ARGUMENT;
3597 } else {
3598 uip = vp->v_ubcinfo;
3599
3600 if ( uip->cs_valid_bitmap == NULL ) {
3601 bitmap_size = stob(uip->ui_size);
3602 target_bitmap = (char*) kalloc( (vm_size_t)bitmap_size );
3603 if (target_bitmap == 0) {
3604 kr = KERN_NO_SPACE;
3605 } else {
3606 kr = KERN_SUCCESS;
3607 }
3608 if( kr == KERN_SUCCESS ) {
3609 memset( target_bitmap, 0, (size_t)bitmap_size);
3610 uip->cs_valid_bitmap = (void*)target_bitmap;
3611 uip->cs_valid_bitmap_size = bitmap_size;
3612 }
3613 }
3614 }
3615 return kr;
3616}
3617
3618kern_return_t
3619ubc_cs_check_validation_bitmap (
3620 vnode_t vp,
3621 memory_object_offset_t offset,
3622 int optype)
3623{
3624 kern_return_t kr = KERN_SUCCESS;
3625
3626 if ( ! USE_CODE_SIGN_BITMAP(vp) || ! UBCINFOEXISTS(vp)) {
3627 kr = KERN_INVALID_ARGUMENT;
3628 } else {
3629 struct ubc_info *uip = vp->v_ubcinfo;
3630 char *target_bitmap = uip->cs_valid_bitmap;
3631
3632 if ( target_bitmap == NULL ) {
3633 kr = KERN_INVALID_ARGUMENT;
3634 } else {
3635 uint64_t bit, byte;
3636 bit = atop_64( offset );
3637 byte = bit >> 3;
3638
3639 if ( byte > uip->cs_valid_bitmap_size ) {
3640 kr = KERN_INVALID_ARGUMENT;
3641 } else {
3642
3643 if (optype == CS_BITMAP_SET) {
3644 target_bitmap[byte] |= (1 << (bit & 07));
3645 kr = KERN_SUCCESS;
3646 } else if (optype == CS_BITMAP_CLEAR) {
3647 target_bitmap[byte] &= ~(1 << (bit & 07));
3648 kr = KERN_SUCCESS;
3649 } else if (optype == CS_BITMAP_CHECK) {
3650 if ( target_bitmap[byte] & (1 << (bit & 07))) {
3651 kr = KERN_SUCCESS;
3652 } else {
3653 kr = KERN_FAILURE;
3654 }
3655 }
3656 }
3657 }
3658 }
3659 return kr;
3660}
3661
3662void
3663ubc_cs_validation_bitmap_deallocate(
3664 vnode_t vp)
3665{
3666 struct ubc_info *uip;
3667 void *target_bitmap;
3668 vm_object_size_t bitmap_size;
3669
3670 if ( UBCINFOEXISTS(vp)) {
3671 uip = vp->v_ubcinfo;
3672
3673 if ( (target_bitmap = uip->cs_valid_bitmap) != NULL ) {
3674 bitmap_size = uip->cs_valid_bitmap_size;
3675 kfree( target_bitmap, (vm_size_t) bitmap_size );
3676 uip->cs_valid_bitmap = NULL;
3677 }
3678 }
3679}
3680#else
3681kern_return_t ubc_cs_validation_bitmap_allocate(__unused vnode_t vp){
3682 return KERN_INVALID_ARGUMENT;
3683}
3684
3685kern_return_t ubc_cs_check_validation_bitmap(
3686 __unused struct vnode *vp,
3687 __unused memory_object_offset_t offset,
3688 __unused int optype){
3689
3690 return KERN_INVALID_ARGUMENT;
3691}
3692
3693void ubc_cs_validation_bitmap_deallocate(__unused vnode_t vp){
3694 return;
3695}
3696#endif /* CHECK_CS_VALIDATION_BITMAP */