1 /*
2 * Copyright (c) 2022 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <os/overflow.h>
24 #include <machine/atomic.h>
25 #include <mach/vm_param.h>
26 #include <vm/vm_kern.h>
27 #include <kern/zalloc.h>
28 #include <kern/kalloc.h>
29 #include <kern/assert.h>
30 #include <kern/locks.h>
31 #include <kern/lock_rw.h>
32 #include <libkern/libkern.h>
33 #include <libkern/section_keywords.h>
34 #include <libkern/coretrust/coretrust.h>
35 #include <pexpert/pexpert.h>
36 #include <sys/vm.h>
37 #include <sys/proc.h>
38 #include <sys/codesign.h>
39 #include <sys/code_signing.h>
40 #include <uuid/uuid.h>
41 #include <IOKit/IOBSD.h>
42
43 #if PMAP_CS_PPL_MONITOR
44 /*
45 * The Page Protection Layer layer implements the PMAP_CS monitor environment which
46 * provides code signing and memory isolation enforcements for data structures which
47 * are critical to ensuring that all code executed on the system is authorized to do
48 * so.
49 *
50 * Unless the data is managed by the PPL itself, XNU needs to page-align everything,
51 * and then reference the memory as read-only.
52 */
53
54 typedef uint64_t pmap_paddr_t __kernel_ptr_semantics;
55 extern vm_map_address_t phystokv(pmap_paddr_t pa);
56 extern pmap_paddr_t kvtophys_nofail(vm_offset_t va);
57
58 #pragma mark Initialization
59
60 void
code_signing_init()61 code_signing_init()
62 {
63 /* Does nothing */
64 }
65
66 #pragma mark Developer Mode
67
68 SECURITY_READ_ONLY_LATE(bool*) developer_mode_enabled = &ppl_developer_mode_storage;
69
70 void
ppl_toggle_developer_mode(bool state)71 ppl_toggle_developer_mode(
72 bool state)
73 {
74 pmap_toggle_developer_mode(state);
75 }
76
77 #pragma mark Code Signing and Provisioning Profiles
78
79 bool
ppl_code_signing_enabled(void)80 ppl_code_signing_enabled(void)
81 {
82 return pmap_cs_enabled();
83 }
84
85 kern_return_t
ppl_register_provisioning_profile(const void * profile_blob,const size_t profile_blob_size,void ** profile_obj)86 ppl_register_provisioning_profile(
87 const void *profile_blob,
88 const size_t profile_blob_size,
89 void **profile_obj)
90 {
91 pmap_profile_payload_t *pmap_payload = NULL;
92 vm_address_t payload_addr = 0;
93 vm_size_t payload_size = 0;
94 vm_size_t payload_size_aligned = 0;
95 kern_return_t ret = KERN_DENIED;
96
97 if (os_add_overflow(sizeof(*pmap_payload), profile_blob_size, &payload_size)) {
98 panic("attempted to load a too-large profile: %lu bytes", profile_blob_size);
99 }
100 payload_size_aligned = round_page(payload_size);
101
102 ret = kmem_alloc(kernel_map, &payload_addr, payload_size_aligned,
103 KMA_KOBJECT | KMA_DATA | KMA_ZERO, VM_KERN_MEMORY_SECURITY);
104 if (ret != KERN_SUCCESS) {
105 printf("unable to allocate memory for pmap profile payload: %d\n", ret);
106 goto exit;
107 }
108
109 /* We need to setup the payload before we send it to the PPL */
110 pmap_payload = (pmap_profile_payload_t*)payload_addr;
111
112 pmap_payload->profile_blob_size = profile_blob_size;
113 memcpy(pmap_payload->profile_blob, profile_blob, profile_blob_size);
114
115 ret = pmap_register_provisioning_profile(payload_addr, payload_size_aligned);
116 if (ret == KERN_SUCCESS) {
117 *profile_obj = &pmap_payload->profile_obj_storage;
118 *profile_obj = (pmap_cs_profile_t*)phystokv(kvtophys_nofail((vm_offset_t)*profile_obj));
119 }
120
121 exit:
122 if ((ret != KERN_SUCCESS) && (payload_addr != 0)) {
123 kmem_free(kernel_map, payload_addr, payload_size_aligned);
124 payload_addr = 0;
125 payload_size_aligned = 0;
126 }
127
128 return ret;
129 }
130
131 kern_return_t
ppl_unregister_provisioning_profile(void * profile_obj)132 ppl_unregister_provisioning_profile(
133 void *profile_obj)
134 {
135 pmap_cs_profile_t *ppl_profile_obj = profile_obj;
136 kern_return_t ret = KERN_DENIED;
137
138 ret = pmap_unregister_provisioning_profile(ppl_profile_obj);
139 if (ret != KERN_SUCCESS) {
140 return ret;
141 }
142
143 /* Get the original payload address */
144 const pmap_profile_payload_t *pmap_payload = ppl_profile_obj->original_payload;
145 const vm_address_t payload_addr = (const vm_address_t)pmap_payload;
146
147 /* Get the original payload size */
148 vm_size_t payload_size = pmap_payload->profile_blob_size + sizeof(*pmap_payload);
149 payload_size = round_page(payload_size);
150
151 /* Free the payload */
152 kmem_free(kernel_map, payload_addr, payload_size);
153 pmap_payload = NULL;
154
155 return KERN_SUCCESS;
156 }
157
158 kern_return_t
ppl_associate_provisioning_profile(void * sig_obj,void * profile_obj)159 ppl_associate_provisioning_profile(
160 void *sig_obj,
161 void *profile_obj)
162 {
163 return pmap_associate_provisioning_profile(sig_obj, profile_obj);
164 }
165
166 kern_return_t
ppl_disassociate_provisioning_profile(void * sig_obj)167 ppl_disassociate_provisioning_profile(
168 void *sig_obj)
169 {
170 return pmap_disassociate_provisioning_profile(sig_obj);
171 }
172
173 void
ppl_set_compilation_service_cdhash(const uint8_t cdhash[CS_CDHASH_LEN])174 ppl_set_compilation_service_cdhash(
175 const uint8_t cdhash[CS_CDHASH_LEN])
176 {
177 pmap_set_compilation_service_cdhash(cdhash);
178 }
179
180 bool
ppl_match_compilation_service_cdhash(const uint8_t cdhash[CS_CDHASH_LEN])181 ppl_match_compilation_service_cdhash(
182 const uint8_t cdhash[CS_CDHASH_LEN])
183 {
184 return pmap_match_compilation_service_cdhash(cdhash);
185 }
186
187 void
ppl_set_local_signing_public_key(const uint8_t public_key[XNU_LOCAL_SIGNING_KEY_SIZE])188 ppl_set_local_signing_public_key(
189 const uint8_t public_key[XNU_LOCAL_SIGNING_KEY_SIZE])
190 {
191 return pmap_set_local_signing_public_key(public_key);
192 }
193
194 uint8_t*
ppl_get_local_signing_public_key(void)195 ppl_get_local_signing_public_key(void)
196 {
197 return pmap_get_local_signing_public_key();
198 }
199
200 void
ppl_unrestrict_local_signing_cdhash(const uint8_t cdhash[CS_CDHASH_LEN])201 ppl_unrestrict_local_signing_cdhash(
202 const uint8_t cdhash[CS_CDHASH_LEN])
203 {
204 pmap_unrestrict_local_signing(cdhash);
205 }
206
207 vm_size_t
ppl_managed_code_signature_size(void)208 ppl_managed_code_signature_size(void)
209 {
210 return pmap_cs_blob_limit;
211 }
212
213 kern_return_t
ppl_register_code_signature(const vm_address_t signature_addr,const vm_size_t signature_size,const vm_offset_t code_directory_offset,const char * signature_path,void ** sig_obj,vm_address_t * ppl_signature_addr)214 ppl_register_code_signature(
215 const vm_address_t signature_addr,
216 const vm_size_t signature_size,
217 const vm_offset_t code_directory_offset,
218 const char *signature_path,
219 void **sig_obj,
220 vm_address_t *ppl_signature_addr)
221 {
222 pmap_cs_code_directory_t *cd_entry = NULL;
223
224 /* PPL doesn't care about the signature path */
225 (void)signature_path;
226
227 kern_return_t ret = pmap_cs_register_code_signature_blob(
228 signature_addr,
229 signature_size,
230 code_directory_offset,
231 (pmap_cs_code_directory_t**)sig_obj);
232
233 if (ret != KERN_SUCCESS) {
234 return ret;
235 }
236 cd_entry = *((pmap_cs_code_directory_t**)sig_obj);
237
238 if (ppl_signature_addr) {
239 *ppl_signature_addr = (vm_address_t)cd_entry->superblob;
240 }
241
242 return KERN_SUCCESS;
243 }
244
245 kern_return_t
ppl_unregister_code_signature(void * sig_obj)246 ppl_unregister_code_signature(
247 void *sig_obj)
248 {
249 return pmap_cs_unregister_code_signature_blob(sig_obj);
250 }
251
252 kern_return_t
ppl_verify_code_signature(void * sig_obj)253 ppl_verify_code_signature(
254 void *sig_obj)
255 {
256 return pmap_cs_verify_code_signature_blob(sig_obj);
257 }
258
259 kern_return_t
ppl_reconstitute_code_signature(void * sig_obj,vm_address_t * unneeded_addr,vm_size_t * unneeded_size)260 ppl_reconstitute_code_signature(
261 void *sig_obj,
262 vm_address_t *unneeded_addr,
263 vm_size_t *unneeded_size)
264 {
265 return pmap_cs_unlock_unneeded_code_signature(
266 sig_obj,
267 unneeded_addr,
268 unneeded_size);
269 }
270
271 #pragma mark Address Spaces
272
273 kern_return_t
ppl_associate_code_signature(pmap_t pmap,void * sig_obj,const vm_address_t region_addr,const vm_size_t region_size,const vm_offset_t region_offset)274 ppl_associate_code_signature(
275 pmap_t pmap,
276 void *sig_obj,
277 const vm_address_t region_addr,
278 const vm_size_t region_size,
279 const vm_offset_t region_offset)
280 {
281 return pmap_cs_associate(
282 pmap,
283 sig_obj,
284 region_addr,
285 region_size,
286 region_offset);
287 }
288
289 kern_return_t
ppl_associate_jit_region(pmap_t pmap,const vm_address_t region_addr,const vm_size_t region_size)290 ppl_associate_jit_region(
291 pmap_t pmap,
292 const vm_address_t region_addr,
293 const vm_size_t region_size)
294 {
295 return pmap_cs_associate(
296 pmap,
297 PMAP_CS_ASSOCIATE_JIT,
298 region_addr,
299 region_size,
300 0);
301 }
302
303 kern_return_t
ppl_associate_debug_region(pmap_t pmap,const vm_address_t region_addr,const vm_size_t region_size)304 ppl_associate_debug_region(
305 pmap_t pmap,
306 const vm_address_t region_addr,
307 const vm_size_t region_size)
308 {
309 return pmap_cs_associate(
310 pmap,
311 PMAP_CS_ASSOCIATE_COW,
312 region_addr,
313 region_size,
314 0);
315 }
316
317 kern_return_t
ppl_allow_invalid_code(pmap_t pmap)318 ppl_allow_invalid_code(
319 pmap_t pmap)
320 {
321 return pmap_cs_allow_invalid(pmap);
322 }
323
324 kern_return_t
ppl_address_space_exempt(const pmap_t pmap)325 ppl_address_space_exempt(
326 const pmap_t pmap)
327 {
328 if (pmap_performs_stage2_translations(pmap) == true) {
329 return KERN_SUCCESS;
330 }
331
332 return KERN_DENIED;
333 }
334
335 kern_return_t
ppl_fork_prepare(pmap_t old_pmap,pmap_t new_pmap)336 ppl_fork_prepare(
337 pmap_t old_pmap,
338 pmap_t new_pmap)
339 {
340 return pmap_cs_fork_prepare(old_pmap, new_pmap);
341 }
342
343 kern_return_t
ppl_acquire_signing_identifier(const void * sig_obj,const char ** signing_id)344 ppl_acquire_signing_identifier(
345 const void *sig_obj,
346 const char **signing_id)
347 {
348 const pmap_cs_code_directory_t *cd_entry = sig_obj;
349
350 /* If we reach here, the identifier must have been setup */
351 assert(cd_entry->identifier != NULL);
352
353 if (signing_id) {
354 *signing_id = cd_entry->identifier;
355 }
356
357 return KERN_SUCCESS;
358 }
359
360 #pragma mark Entitlements
361
362 kern_return_t
ppl_associate_kernel_entitlements(void * sig_obj,const void * kernel_entitlements)363 ppl_associate_kernel_entitlements(
364 void *sig_obj,
365 const void *kernel_entitlements)
366 {
367 pmap_cs_code_directory_t *cd_entry = sig_obj;
368 return pmap_associate_kernel_entitlements(cd_entry, kernel_entitlements);
369 }
370
371 kern_return_t
ppl_resolve_kernel_entitlements(pmap_t pmap,const void ** kernel_entitlements)372 ppl_resolve_kernel_entitlements(
373 pmap_t pmap,
374 const void **kernel_entitlements)
375 {
376 kern_return_t ret = KERN_DENIED;
377 const void *entitlements = NULL;
378
379 ret = pmap_resolve_kernel_entitlements(pmap, &entitlements);
380 if ((ret == KERN_SUCCESS) && (kernel_entitlements != NULL)) {
381 *kernel_entitlements = entitlements;
382 }
383
384 return ret;
385 }
386
387 kern_return_t
ppl_accelerate_entitlements(void * sig_obj,CEQueryContext_t * ce_ctx)388 ppl_accelerate_entitlements(
389 void *sig_obj,
390 CEQueryContext_t *ce_ctx)
391 {
392 pmap_cs_code_directory_t *cd_entry = sig_obj;
393 kern_return_t ret = KERN_DENIED;
394
395 ret = pmap_accelerate_entitlements(cd_entry);
396
397 /*
398 * We only ever get KERN_ABORTED when we cannot accelerate the entitlements
399 * because it would consume too much memory. In this case, we still want to
400 * return the ce_ctx since we don't want the system to fall-back to non-PPL
401 * locked down memory, so we switch this to a success case.
402 */
403 if (ret == KERN_ABORTED) {
404 ret = KERN_SUCCESS;
405 }
406
407 /* Return the accelerated context to the caller */
408 if ((ret == KERN_SUCCESS) && (ce_ctx != NULL)) {
409 *ce_ctx = cd_entry->ce_ctx;
410 }
411
412 return ret;
413 }
414
415 #pragma mark Image4
416
417 void*
ppl_image4_storage_data(size_t * allocated_size)418 ppl_image4_storage_data(
419 size_t *allocated_size)
420 {
421 return pmap_image4_pmap_data(allocated_size);
422 }
423
424 void
ppl_image4_set_nonce(const img4_nonce_domain_index_t ndi,const img4_nonce_t * nonce)425 ppl_image4_set_nonce(
426 const img4_nonce_domain_index_t ndi,
427 const img4_nonce_t *nonce)
428 {
429 return pmap_image4_set_nonce(ndi, nonce);
430 }
431
432 void
ppl_image4_roll_nonce(const img4_nonce_domain_index_t ndi)433 ppl_image4_roll_nonce(
434 const img4_nonce_domain_index_t ndi)
435 {
436 return pmap_image4_roll_nonce(ndi);
437 }
438
439 errno_t
ppl_image4_copy_nonce(const img4_nonce_domain_index_t ndi,img4_nonce_t * nonce_out)440 ppl_image4_copy_nonce(
441 const img4_nonce_domain_index_t ndi,
442 img4_nonce_t *nonce_out)
443 {
444 return pmap_image4_copy_nonce(ndi, nonce_out);
445 }
446
447 errno_t
ppl_image4_execute_object(img4_runtime_object_spec_index_t obj_spec_index,const img4_buff_t * payload,const img4_buff_t * manifest)448 ppl_image4_execute_object(
449 img4_runtime_object_spec_index_t obj_spec_index,
450 const img4_buff_t *payload,
451 const img4_buff_t *manifest)
452 {
453 errno_t err = EINVAL;
454 kern_return_t kr = KERN_DENIED;
455 img4_buff_t payload_aligned = IMG4_BUFF_INIT;
456 img4_buff_t manifest_aligned = IMG4_BUFF_INIT;
457 vm_address_t payload_addr = 0;
458 vm_size_t payload_len_aligned = 0;
459 vm_address_t manifest_addr = 0;
460 vm_size_t manifest_len_aligned = 0;
461
462 if (payload == NULL) {
463 printf("invalid object execution request: no payload\n");
464 goto out;
465 }
466
467 /*
468 * The PPL will attempt to lockdown both the payload and the manifest before executing
469 * the object. In order for that to happen, both the artifacts need to be page-aligned.
470 */
471 payload_len_aligned = round_page(payload->i4b_len);
472 if (manifest != NULL) {
473 manifest_len_aligned = round_page(manifest->i4b_len);
474 }
475
476 kr = kmem_alloc(
477 kernel_map,
478 &payload_addr,
479 payload_len_aligned,
480 KMA_KOBJECT,
481 VM_KERN_MEMORY_SECURITY);
482
483 if (kr != KERN_SUCCESS) {
484 printf("unable to allocate memory for image4 payload: %d\n", kr);
485 err = ENOMEM;
486 goto out;
487 }
488
489 /* Copy in the payload */
490 memcpy((uint8_t*)payload_addr, payload->i4b_bytes, payload->i4b_len);
491
492 /* Construct the aligned payload buffer */
493 payload_aligned.i4b_bytes = (uint8_t*)payload_addr;
494 payload_aligned.i4b_len = payload->i4b_len;
495
496 if (manifest != NULL) {
497 kr = kmem_alloc(
498 kernel_map,
499 &manifest_addr,
500 manifest_len_aligned,
501 KMA_KOBJECT,
502 VM_KERN_MEMORY_SECURITY);
503
504 if (kr != KERN_SUCCESS) {
505 printf("unable to allocate memory for image4 manifest: %d\n", kr);
506 err = ENOMEM;
507 goto out;
508 }
509
510 /* Construct the aligned manifest buffer */
511 manifest_aligned.i4b_bytes = (uint8_t*)manifest_addr;
512 manifest_aligned.i4b_len = manifest->i4b_len;
513
514 /* Copy in the manifest */
515 memcpy((uint8_t*)manifest_addr, manifest->i4b_bytes, manifest->i4b_len);
516 }
517
518 err = pmap_image4_execute_object(obj_spec_index, &payload_aligned, &manifest_aligned);
519 if (err != 0) {
520 printf("unable to execute image4 object: %d\n", err);
521 goto out;
522 }
523
524 out:
525 /* We always free the manifest as it isn't required anymore */
526 if (manifest_addr != 0) {
527 kmem_free(kernel_map, manifest_addr, manifest_len_aligned);
528 manifest_addr = 0;
529 manifest_len_aligned = 0;
530 }
531
532 /* If we encountered an error -- free the allocated payload */
533 if ((err != 0) && (payload_addr != 0)) {
534 kmem_free(kernel_map, payload_addr, payload_len_aligned);
535 payload_addr = 0;
536 payload_len_aligned = 0;
537 }
538
539 return err;
540 }
541
542 errno_t
ppl_image4_copy_object(img4_runtime_object_spec_index_t obj_spec_index,vm_address_t object_out,size_t * object_length)543 ppl_image4_copy_object(
544 img4_runtime_object_spec_index_t obj_spec_index,
545 vm_address_t object_out,
546 size_t *object_length)
547 {
548 errno_t err = EINVAL;
549 kern_return_t kr = KERN_DENIED;
550 vm_address_t object_addr = 0;
551 vm_size_t object_len_aligned = 0;
552
553 if (object_out == 0) {
554 printf("invalid object copy request: no object input buffer\n");
555 goto out;
556 } else if (object_length == NULL) {
557 printf("invalid object copy request: no object input length\n");
558 goto out;
559 }
560
561 /*
562 * The PPL will attempt to pin the input buffer in order to ensure that the kernel
563 * didn't pass in PPL-owned buffers. The PPL cannot pin the same page more than once,
564 * and attempting to do so will panic the system. Hence, we allocate fresh pages for
565 * for the PPL to pin.
566 *
567 * We can send in the address for the length pointer since that is allocated on the
568 * stack, so the PPL can pin our stack for the duration of the call as no other
569 * thread can be using our stack, meaning the PPL will never attempt to double-pin
570 * the page.
571 */
572 object_len_aligned = round_page(*object_length);
573
574 kr = kmem_alloc(
575 kernel_map,
576 &object_addr,
577 object_len_aligned,
578 KMA_KOBJECT,
579 VM_KERN_MEMORY_SECURITY);
580
581 if (kr != KERN_SUCCESS) {
582 printf("unable to allocate memory for image4 object: %d\n", kr);
583 err = ENOMEM;
584 goto out;
585 }
586
587 err = pmap_image4_copy_object(obj_spec_index, object_addr, object_length);
588 if (err != 0) {
589 printf("unable to copy image4 object: %d\n", err);
590 goto out;
591 }
592
593 /* Copy the data back into the caller passed buffer */
594 memcpy((void*)object_out, (void*)object_addr, *object_length);
595
596 out:
597 /* We don't ever need to keep around our page-aligned buffer */
598 if (object_addr != 0) {
599 kmem_free(kernel_map, object_addr, object_len_aligned);
600 object_addr = 0;
601 object_len_aligned = 0;
602 }
603
604 return err;
605 }
606
607 const void*
ppl_image4_get_monitor_exports(void)608 ppl_image4_get_monitor_exports(void)
609 {
610 /*
611 * AppleImage4 can query the PMAP_CS runtime on its own since the PMAP_CS
612 * runtime is compiled within the kernel extension itself. As a result, we
613 * never expect this KPI to be called when the system uses the PPL monitor.
614 */
615
616 printf("explicit monitor-exports-get not required for the PPL\n");
617 return NULL;
618 }
619
620 errno_t
ppl_image4_set_release_type(__unused const char * release_type)621 ppl_image4_set_release_type(
622 __unused const char *release_type)
623 {
624 /*
625 * AppleImage4 stores the release type in the CTRR protected memory region
626 * of its kernel extension. This is accessible by the PMAP_CS runtime as the
627 * runtime is compiled alongside the kernel extension. As a result, we never
628 * expect this KPI to be called when the system uses the PPL monitor.
629 */
630
631 printf("explicit release-type-set set not required for the PPL\n");
632 return ENOTSUP;
633 }
634
635 errno_t
ppl_image4_set_bnch_shadow(__unused const img4_nonce_domain_index_t ndi)636 ppl_image4_set_bnch_shadow(
637 __unused const img4_nonce_domain_index_t ndi)
638 {
639 /*
640 * AppleImage4 stores the BNCH shadow in the CTRR protected memory region
641 * of its kernel extension. This is accessible by the PMAP_CS runtime as the
642 * runtime is compiled alongside the kernel extension. As a result, we never
643 * expect this KPI to be called when the system uses the PPL monitor.
644 */
645
646 printf("explicit BNCH-shadow-set not required for the PPL\n");
647 return ENOTSUP;
648 }
649
650 #endif /* PMAP_CS_PPL_MONITOR */
651