2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/proc_internal.h>
34 #include <sys/sysctl.h>
35 #include <sys/signal.h>
36 #include <sys/signalvar.h>
37 #include <sys/codesign.h>
39 #include <sys/fcntl.h>
41 #include <sys/file_internal.h>
42 #include <sys/kauth.h>
43 #include <sys/mount.h>
46 #include <sys/socketvar.h>
47 #include <sys/vnode.h>
48 #include <sys/vnode_internal.h>
51 #include <sys/ubc_internal.h>
53 #include <security/mac.h>
54 #include <security/mac_policy.h>
55 #include <security/mac_framework.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_map.h>
59 #include <mach/mach_vm.h>
61 #include <kern/kern_types.h>
62 #include <kern/startup.h>
63 #include <kern/task.h>
65 #include <vm/vm_map.h>
67 #include <vm/vm_kern.h>
70 #include <kern/assert.h>
72 #include <pexpert/pexpert.h>
74 #include <mach/shared_region.h>
76 #include <libkern/section_keywords.h>
77 #include <libkern/ptrauth_utils.h>
80 unsigned long cs_procs_killed
= 0;
81 unsigned long cs_procs_invalidated
= 0;
83 int cs_force_kill
= 0;
84 int cs_force_hard
= 0;
86 // If set, AMFI will error out early on unsigned code, before evaluation the normal policy.
87 int cs_debug_fail_on_unsigned_code
= 0;
88 // If the previous mode is enabled, we count the resulting failures here.
89 unsigned int cs_debug_unsigned_exec_failures
= 0;
90 unsigned int cs_debug_unsigned_mmap_failures
= 0;
92 #if CONFIG_ENFORCE_SIGNED_CODE
93 #define DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE 1
94 #define DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE 1
96 #define DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE 1
97 #define DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE 0
100 #if CONFIG_ENFORCE_LIBRARY_VALIDATION
101 #define DEFAULT_CS_LIBRARY_VA_ENABLE 1
103 #define DEFAULT_CS_LIBRARY_VA_ENABLE 0
109 * Here we split cs_enforcement_enable into cs_system_enforcement_enable and cs_process_enforcement_enable
111 * cs_system_enforcement_enable governs whether or not system level code signing enforcement mechanisms
112 * are applied on the system. Today, the only such mechanism is code signing enforcement of the dyld shared
115 * cs_process_enforcement_enable governs whether code signing enforcement mechanisms are applied to all
116 * processes or only those that opt into such enforcement.
118 * (On iOS and related, both of these are set by default. On macOS, only cs_system_enforcement_enable
119 * is set by default. Processes can then be opted into code signing enforcement on a case by case basis.)
121 SECURITY_READ_ONLY_EARLY(int) cs_system_enforcement_enable
= DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE
;
122 SECURITY_READ_ONLY_EARLY(int) cs_process_enforcement_enable
= DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE
;
123 SECURITY_READ_ONLY_EARLY(int) cs_library_val_enable
= DEFAULT_CS_LIBRARY_VA_ENABLE
;
125 #else /* !SECURE_KERNEL */
126 int cs_enforcement_panic
= 0;
127 int cs_relax_platform_task_ports
= 0;
129 SECURITY_READ_ONLY_LATE(int) cs_system_enforcement_enable
= DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE
;
130 SECURITY_READ_ONLY_LATE(int) cs_process_enforcement_enable
= DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE
;
132 SECURITY_READ_ONLY_LATE(int) cs_library_val_enable
= DEFAULT_CS_LIBRARY_VA_ENABLE
;
134 #endif /* !SECURE_KERNEL */
135 int cs_all_vnodes
= 0;
137 static lck_grp_t
*cs_lockgrp
;
139 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_kill
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_kill
, 0, "");
140 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_hard
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_hard
, 0, "");
141 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_debug
, 0, "");
142 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug_fail_on_unsigned_code
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
143 &cs_debug_fail_on_unsigned_code
, 0, "");
144 SYSCTL_UINT(_vm
, OID_AUTO
, cs_debug_unsigned_exec_failures
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
145 &cs_debug_unsigned_exec_failures
, 0, "");
146 SYSCTL_UINT(_vm
, OID_AUTO
, cs_debug_unsigned_mmap_failures
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
147 &cs_debug_unsigned_mmap_failures
, 0, "");
149 SYSCTL_INT(_vm
, OID_AUTO
, cs_all_vnodes
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_all_vnodes
, 0, "");
152 SYSCTL_INT(_vm
, OID_AUTO
, cs_system_enforcement
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_system_enforcement_enable
, 0, "");
153 SYSCTL_INT(_vm
, OID_AUTO
, cs_process_enforcement
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_process_enforcement_enable
, 0, "");
154 SYSCTL_INT(_vm
, OID_AUTO
, cs_enforcement_panic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_enforcement_panic
, 0, "");
156 #if !CONFIG_ENFORCE_LIBRARY_VALIDATION
157 SYSCTL_INT(_vm
, OID_AUTO
, cs_library_validation
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &cs_library_val_enable
, 0, "");
159 #endif /* !SECURE_KERNEL */
161 int panic_on_cs_killed
= 0;
168 #if PLATFORM_WatchOS || __x86_64__
169 panic_on_cs_killed
= 1;
170 #endif /* watchos || x86_64 */
171 #endif /* MACH_ASSERT */
172 PE_parse_boot_argn("panic_on_cs_killed", &panic_on_cs_killed
,
173 sizeof(panic_on_cs_killed
));
175 int disable_cs_enforcement
= 0;
176 PE_parse_boot_argn("cs_enforcement_disable", &disable_cs_enforcement
,
177 sizeof(disable_cs_enforcement
));
178 if (disable_cs_enforcement
&& PE_i_can_has_debugger(NULL
) != 0) {
179 cs_system_enforcement_enable
= 0;
180 cs_process_enforcement_enable
= 0;
183 PE_parse_boot_argn("cs_enforcement_panic", &panic
, sizeof(panic
));
184 cs_enforcement_panic
= (panic
!= 0);
187 PE_parse_boot_argn("cs_relax_platform_task_ports",
188 &cs_relax_platform_task_ports
,
189 sizeof(cs_relax_platform_task_ports
));
191 PE_parse_boot_argn("cs_debug", &cs_debug
, sizeof(cs_debug
));
193 #if !CONFIG_ENFORCE_LIBRARY_VALIDATION
194 PE_parse_boot_argn("cs_library_val_enable", &cs_library_val_enable
,
195 sizeof(cs_library_val_enable
));
197 #endif /* !SECURE_KERNEL */
199 lck_grp_attr_t
*attr
= lck_grp_attr_alloc_init();
200 cs_lockgrp
= lck_grp_alloc_init("KERNCS", attr
);
201 lck_grp_attr_free(attr
);
203 STARTUP(CODESIGNING
, STARTUP_RANK_FIRST
, cs_init
);
206 cs_allow_invalid(struct proc
*p
)
209 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
212 /* There needs to be a MAC policy to implement this hook, or else the
213 * kill bits will be cleared here every time. If we have
214 * CONFIG_ENFORCE_SIGNED_CODE, we can assume there is a policy
215 * implementing the hook.
217 if (0 != mac_proc_check_run_cs_invalid(p
)) {
219 printf("CODE SIGNING: cs_allow_invalid() "
220 "not allowed: pid %d\n",
226 printf("CODE SIGNING: cs_allow_invalid() "
231 p
->p_csflags
&= ~(CS_KILL
| CS_HARD
);
232 if (p
->p_csflags
& CS_VALID
) {
233 p
->p_csflags
|= CS_DEBUGGED
;
236 task_t procTask
= proc_task(p
);
238 vm_map_t proc_map
= get_task_map_reference(procTask
);
240 if (vm_map_cs_wx_enable(proc_map
) != KERN_SUCCESS
) {
241 printf("CODE SIGNING: cs_allow_invalid() not allowed by pmap: pid %d\n", p
->p_pid
);
243 vm_map_deallocate(proc_map
);
249 /* allow a debugged process to hide some (debug-only!) memory */
250 task_set_memory_ownership_transfer(p
->task
, TRUE
);
252 vm_map_switch_protect(get_task_map(p
->task
), FALSE
);
254 return (p
->p_csflags
& (CS_KILL
| CS_HARD
)) == 0;
258 cs_invalid_page(addr64_t vaddr
, boolean_t
*cs_killed
)
261 int send_kill
= 0, retval
= 0, verbose
= cs_debug
;
266 printf("CODE SIGNING: cs_invalid_page(0x%llx): p=%d[%s]\n",
267 vaddr
, p
->p_pid
, p
->p_comm
);
272 /* XXX for testing */
274 p
->p_csflags
|= CS_KILL
;
277 p
->p_csflags
|= CS_HARD
;
280 /* CS_KILL triggers a kill signal, and no you can't have the page. Nothing else. */
281 if (p
->p_csflags
& CS_KILL
) {
282 p
->p_csflags
|= CS_KILLED
;
288 /* CS_HARD means fail the mapping operation so the process stays valid. */
289 if (p
->p_csflags
& CS_HARD
) {
292 if (p
->p_csflags
& CS_VALID
) {
293 p
->p_csflags
&= ~CS_VALID
;
294 cs_procs_invalidated
++;
296 cs_process_invalidated(NULL
);
302 printf("CODE SIGNING: cs_invalid_page(0x%llx): "
303 "p=%d[%s] final status 0x%x, %s page%s\n",
304 vaddr
, p
->p_pid
, p
->p_comm
, p
->p_csflags
,
305 retval
? "denying" : "allowing (remove VALID)",
306 send_kill
? " sending SIGKILL" : "");
310 /* We will set the exit reason for the thread later */
311 threadsignal(current_thread(), SIGKILL
, EXC_BAD_ACCESS
, FALSE
);
315 } else if (cs_killed
) {
323 * Called after a process got its CS_VALID bit removed, either by
324 * a previous call to cs_invalid_page, or through other means.
325 * Called from fault handler with vm object lock held.
326 * Called with proc lock held for current_proc or, if passed in, p,
327 * to ensure MACF hook can suspend the task before other threads
328 * can access the memory that is paged in after cs_invalid_page
329 * returns 0 due to missing CS_HARD|CS_KILL.
332 cs_process_invalidated(struct proc
* __unused p
)
338 mac_proc_notify_cs_invalidated(p
);
343 * Assumes p (if passed in) is locked with proc_lock().
347 cs_process_enforcement(struct proc
*p
)
349 if (cs_process_enforcement_enable
) {
357 if (p
!= NULL
&& (p
->p_csflags
& CS_ENFORCEMENT
)) {
365 cs_process_global_enforcement(void)
367 return cs_process_enforcement_enable
? 1 : 0;
371 cs_system_enforcement(void)
373 return cs_system_enforcement_enable
? 1 : 0;
377 cs_vm_supports_4k_translations(void)
384 * Returns whether a given process is still valid.
387 cs_valid(struct proc
*p
)
393 if (p
!= NULL
&& (p
->p_csflags
& CS_VALID
)) {
401 * Library validation functions
404 cs_require_lv(struct proc
*p
)
406 if (cs_library_val_enable
) {
414 if (p
!= NULL
&& (p
->p_csflags
& CS_REQUIRE_LV
)) {
422 csproc_forced_lv(struct proc
* p
)
427 if (p
!= NULL
&& (p
->p_csflags
& CS_FORCED_LV
)) {
434 * <rdar://problem/24634089> added to allow system level library
435 * validation check at mac_cred_label_update_execve time
438 cs_system_require_lv(void)
440 return cs_library_val_enable
? 1 : 0;
444 * Function: csblob_get_base_offset
446 * Description: This function returns the base offset into the (possibly universal) binary
451 csblob_get_base_offset(struct cs_blob
*blob
)
453 return blob
->csb_base_offset
;
457 * Function: csblob_get_size
459 * Description: This function returns the size of a given blob.
463 csblob_get_size(struct cs_blob
*blob
)
465 return blob
->csb_mem_size
;
469 * Function: csblob_get_addr
471 * Description: This function returns the address of a given blob.
475 csblob_get_addr(struct cs_blob
*blob
)
477 return blob
->csb_mem_kaddr
;
481 * Function: csblob_get_platform_binary
483 * Description: This function returns true if the binary is
484 * in the trust cache.
488 csblob_get_platform_binary(struct cs_blob
*blob
)
490 if (blob
&& blob
->csb_platform_binary
) {
497 * Function: csblob_get_flags
499 * Description: This function returns the flags for a given blob
503 csblob_get_flags(struct cs_blob
*blob
)
505 return blob
->csb_flags
;
509 * Function: csblob_get_hashtype
511 * Description: This function returns the hash type for a given blob
515 csblob_get_hashtype(struct cs_blob
const * const blob
)
517 return blob
->csb_hashtype
!= NULL
? cs_hash_type(blob
->csb_hashtype
) : 0;
521 * Function: csproc_get_blob
523 * Description: This function returns the cs_blob
527 csproc_get_blob(struct proc
*p
)
533 if (NULL
== p
->p_textvp
) {
537 if ((p
->p_csflags
& CS_SIGNED
) == 0) {
541 return ubc_cs_blob_get(p
->p_textvp
, -1, -1, p
->p_textoff
);
545 * Function: csvnode_get_blob
547 * Description: This function returns the cs_blob
551 csvnode_get_blob(struct vnode
*vp
, off_t offset
)
553 return ubc_cs_blob_get(vp
, -1, -1, offset
);
557 * Function: csblob_get_teamid
559 * Description: This function returns a pointer to the
563 csblob_get_teamid(struct cs_blob
*csblob
)
565 return csblob
->csb_teamid
;
569 * Function: csblob_get_identity
571 * Description: This function returns a pointer to the
575 csblob_get_identity(struct cs_blob
*csblob
)
577 const CS_CodeDirectory
*cd
;
579 cd
= (const CS_CodeDirectory
*)csblob_find_blob(csblob
, CSSLOT_CODEDIRECTORY
, CSMAGIC_CODEDIRECTORY
);
584 if (cd
->identOffset
== 0) {
588 return ((const char *)cd
) + ntohl(cd
->identOffset
);
592 * Function: csblob_get_cdhash
594 * Description: This function returns a pointer to the
595 * cdhash of csblob (20 byte array)
598 csblob_get_cdhash(struct cs_blob
*csblob
)
600 ptrauth_utils_auth_blob_generic(csblob
->csb_cdhash
,
601 sizeof(csblob
->csb_cdhash
),
602 OS_PTRAUTH_DISCRIMINATOR("cs_blob.csb_cd_signature"),
603 PTRAUTH_ADDR_DIVERSIFY
,
604 csblob
->csb_cdhash_signature
);
605 return csblob
->csb_cdhash
;
609 * Function: csblob_get_signer_type
611 * Description: This function returns the signer type
615 csblob_get_signer_type(struct cs_blob
*csblob
)
617 return csblob
->csb_signer_type
;
621 csblob_entitlements_dictionary_copy(struct cs_blob
*csblob
)
623 if (!csblob
->csb_entitlements
) {
626 osobject_retain(csblob
->csb_entitlements
);
627 return csblob
->csb_entitlements
;
631 csblob_entitlements_dictionary_set(struct cs_blob
*csblob
, void * entitlements
)
633 assert(csblob
->csb_entitlements
== NULL
);
635 osobject_retain(entitlements
);
637 csblob
->csb_entitlements
= entitlements
;
641 * Function: csproc_get_teamid
643 * Description: This function returns a pointer to the
644 * team id of the process p
647 csproc_get_teamid(struct proc
*p
)
649 struct cs_blob
*csblob
;
651 csblob
= csproc_get_blob(p
);
652 if (csblob
== NULL
) {
656 return csblob_get_teamid(csblob
);
660 csproc_get_identity(struct proc
*p
)
662 struct cs_blob
*csblob
= NULL
;
664 csblob
= csproc_get_blob(p
);
665 if (csblob
== NULL
) {
669 return csblob_get_identity(csblob
);
673 * Function: csproc_get_signer_type
675 * Description: This function returns the signer type
679 csproc_get_signer_type(struct proc
*p
)
681 struct cs_blob
*csblob
;
683 csblob
= csproc_get_blob(p
);
684 if (csblob
== NULL
) {
685 return CS_SIGNER_TYPE_UNKNOWN
;
688 return csblob_get_signer_type(csblob
);
692 * Function: csvnode_get_teamid
694 * Description: This function returns a pointer to the
695 * team id of the binary at the given offset in vnode vp
698 csvnode_get_teamid(struct vnode
*vp
, off_t offset
)
700 struct cs_blob
*csblob
;
706 csblob
= ubc_cs_blob_get(vp
, -1, -1, offset
);
707 if (csblob
== NULL
) {
711 return csblob_get_teamid(csblob
);
715 * Function: csproc_get_platform_binary
717 * Description: This function returns the value
718 * of the platform_binary field for proc p
721 csproc_get_platform_binary(struct proc
*p
)
723 struct cs_blob
*csblob
;
725 csblob
= csproc_get_blob(p
);
727 /* If there is no csblob this returns 0 because
728 * it is true that it is not a platform binary */
729 return (csblob
== NULL
) ? 0 : csblob
->csb_platform_binary
;
733 csproc_get_platform_path(struct proc
*p
)
735 struct cs_blob
*csblob
;
737 csblob
= csproc_get_blob(p
);
739 return (csblob
== NULL
) ? 0 : csblob
->csb_platform_path
;
742 #if DEVELOPMENT || DEBUG
744 csproc_clear_platform_binary(struct proc
*p
)
746 struct cs_blob
*csblob
= csproc_get_blob(p
);
748 if (csblob
== NULL
) {
753 printf("clearing platform binary on proc/task: pid = %d\n", p
->p_pid
);
756 csblob
->csb_platform_binary
= 0;
757 csblob
->csb_platform_path
= 0;
758 task_set_platform_binary(proc_task(p
), FALSE
);
763 csproc_disable_enforcement(struct proc
* __unused p
)
765 #if !CONFIG_ENFORCE_SIGNED_CODE
768 p
->p_csflags
&= (~CS_ENFORCEMENT
);
769 vm_map_cs_enforcement_set(get_task_map(p
->task
), FALSE
);
775 /* Function: csproc_mark_invalid_allowed
777 * Description: Mark the process as being allowed to go invalid. Called as part of
778 * task_for_pid and ptrace policy. Note CS_INVALID_ALLOWED only matters for
779 * processes that have been opted into CS_ENFORCEMENT.
782 csproc_mark_invalid_allowed(struct proc
* __unused p
)
784 #if !CONFIG_ENFORCE_SIGNED_CODE
787 p
->p_csflags
|= CS_INVALID_ALLOWED
;
794 * Function: csproc_check_invalid_allowed
796 * Description: Returns 1 if the process has been marked as allowed to go invalid
797 * because it gave its task port to an allowed process.
800 csproc_check_invalid_allowed(struct proc
* __unused p
)
802 #if !CONFIG_ENFORCE_SIGNED_CODE
807 if (p
!= NULL
&& (p
->p_csflags
& CS_INVALID_ALLOWED
)) {
815 * Function: csproc_get_prod_signed
817 * Description: Returns 1 if process is not signed with a developer identity.
818 * Note the inverted meaning from the cs_flag to make the error case safer.
819 * Will go away with rdar://problem/28322552.
822 csproc_get_prod_signed(struct proc
*p
)
824 return (p
->p_csflags
& CS_DEV_CODE
) == 0;
829 * Function: csfg_get_platform_binary
831 * Description: This function returns the
832 * platform binary field for the
836 csfg_get_platform_binary(struct fileglob
*fg
)
838 int platform_binary
= 0;
839 struct ubc_info
*uip
;
842 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
846 vp
= (struct vnode
*)fg
->fg_data
;
852 if (!UBCINFOEXISTS(vp
)) {
861 if (uip
->cs_blobs
== NULL
) {
865 /* It is OK to extract the teamid from the first blob
866 * because all blobs of a vnode must have the same teamid */
867 platform_binary
= uip
->cs_blobs
->csb_platform_binary
;
871 return platform_binary
;
875 csfg_get_supplement_platform_binary(struct fileglob
*fg __unused
)
877 #if CONFIG_SUPPLEMENTAL_SIGNATURES
878 int platform_binary
= 0;
879 struct ubc_info
*uip
;
882 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
886 vp
= (struct vnode
*)fg
->fg_data
;
892 if (!UBCINFOEXISTS(vp
)) {
901 if (uip
->cs_blob_supplement
== NULL
) {
905 platform_binary
= uip
->cs_blob_supplement
->csb_platform_binary
;
909 return platform_binary
;
911 // Supplemental signatures are only allowed in CONFIG_SUPPLEMENTAL_SIGNATURES
912 // Return false if anyone asks about them
918 csfg_get_cdhash(struct fileglob
*fg
, uint64_t offset
, size_t *cdhash_size
)
922 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
926 vp
= (struct vnode
*)fg
->fg_data
;
931 struct cs_blob
*csblob
= NULL
;
932 if ((csblob
= ubc_cs_blob_get(vp
, -1, -1, offset
)) == NULL
) {
937 *cdhash_size
= CS_CDHASH_LEN
;
939 ptrauth_utils_auth_blob_generic(csblob
->csb_cdhash
,
940 sizeof(csblob
->csb_cdhash
),
941 OS_PTRAUTH_DISCRIMINATOR("cs_blob.csb_cd_signature"),
942 PTRAUTH_ADDR_DIVERSIFY
,
943 csblob
->csb_cdhash_signature
);
944 return csblob
->csb_cdhash
;
948 csfg_get_supplement_cdhash(struct fileglob
*fg __unused
, uint64_t offset __unused
, size_t *cdhash_size __unused
)
950 #if CONFIG_SUPPLEMENTAL_SIGNATURES
953 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
957 vp
= (struct vnode
*)fg
->fg_data
;
962 struct cs_blob
*csblob
= NULL
;
963 if ((csblob
= ubc_cs_blob_get_supplement(vp
, offset
)) == NULL
) {
968 *cdhash_size
= CS_CDHASH_LEN
;
970 ptrauth_utils_auth_blob_generic(csblob
->csb_cdhash
,
971 sizeof(csblob
->csb_cdhash
),
972 OS_PTRAUTH_DISCRIMINATOR("cs_blob.csb_cd_signature"),
973 PTRAUTH_ADDR_DIVERSIFY
,
974 csblob
->csb_cdhash_signature
);
975 return csblob
->csb_cdhash
;
977 // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES
978 // return NULL if anyone asks about them
984 csfg_get_supplement_linkage_cdhash(struct fileglob
*fg __unused
, uint64_t offset __unused
, size_t *cdhash_size __unused
)
986 #if CONFIG_SUPPLEMENTAL_SIGNATURES
989 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
993 vp
= (struct vnode
*)fg
->fg_data
;
998 struct cs_blob
*csblob
= NULL
;
999 if ((csblob
= ubc_cs_blob_get_supplement(vp
, offset
)) == NULL
) {
1004 *cdhash_size
= CS_CDHASH_LEN
;
1007 return csblob
->csb_linkage
;
1009 // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES
1010 // return NULL if anyone asks about them
1016 * Function: csfg_get_signer_type
1018 * Description: This returns the signer type
1019 * for the fileglob fg
1022 csfg_get_signer_type(struct fileglob
*fg
)
1024 struct ubc_info
*uip
;
1025 unsigned int signer_type
= CS_SIGNER_TYPE_UNKNOWN
;
1028 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1029 return CS_SIGNER_TYPE_UNKNOWN
;
1032 vp
= (struct vnode
*)fg
->fg_data
;
1034 return CS_SIGNER_TYPE_UNKNOWN
;
1038 if (!UBCINFOEXISTS(vp
)) {
1042 uip
= vp
->v_ubcinfo
;
1047 if (uip
->cs_blobs
== NULL
) {
1051 /* It is OK to extract the signer type from the first blob,
1052 * because all blobs of a vnode must have the same signer type. */
1053 signer_type
= uip
->cs_blobs
->csb_signer_type
;
1061 csfg_get_supplement_signer_type(struct fileglob
*fg __unused
)
1063 #if CONFIG_SUPPLEMENTAL_SIGNATURES
1064 struct ubc_info
*uip
;
1065 unsigned int signer_type
= CS_SIGNER_TYPE_UNKNOWN
;
1068 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1069 return CS_SIGNER_TYPE_UNKNOWN
;
1072 vp
= (struct vnode
*)fg
->fg_data
;
1074 return CS_SIGNER_TYPE_UNKNOWN
;
1078 if (!UBCINFOEXISTS(vp
)) {
1082 uip
= vp
->v_ubcinfo
;
1087 if (uip
->cs_blob_supplement
== NULL
) {
1091 signer_type
= uip
->cs_blob_supplement
->csb_signer_type
;
1097 // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES
1098 // Return unknown if anyone asks
1099 return CS_SIGNER_TYPE_UNKNOWN
;
1104 * Function: csfg_get_teamid
1106 * Description: This returns a pointer to
1107 * the teamid for the fileglob fg
1110 csfg_get_teamid(struct fileglob
*fg
)
1112 struct ubc_info
*uip
;
1113 const char *str
= NULL
;
1116 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1120 vp
= (struct vnode
*)fg
->fg_data
;
1126 if (!UBCINFOEXISTS(vp
)) {
1130 uip
= vp
->v_ubcinfo
;
1135 if (uip
->cs_blobs
== NULL
) {
1139 /* It is OK to extract the teamid from the first blob
1140 * because all blobs of a vnode must have the same teamid */
1141 str
= uip
->cs_blobs
->csb_teamid
;
1149 csfg_get_supplement_teamid(struct fileglob
*fg __unused
)
1151 #if CONFIG_SUPPLEMENTAL_SIGNATURES
1152 struct ubc_info
*uip
;
1153 const char *str
= NULL
;
1156 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1160 vp
= (struct vnode
*)fg
->fg_data
;
1166 if (!UBCINFOEXISTS(vp
)) {
1170 uip
= vp
->v_ubcinfo
;
1175 if (uip
->cs_blob_supplement
== NULL
) {
1179 str
= uip
->cs_blob_supplement
->csb_supplement_teamid
;
1185 // Supplemental Signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES
1186 // Return NULL if anyone asks
1192 * Function: csfg_get_prod_signed
1194 * Description: Returns 1 if code is not signed with a developer identity.
1195 * Note the inverted meaning from the cs_flag to make the error case safer.
1196 * Will go away with rdar://problem/28322552.
1199 csfg_get_prod_signed(struct fileglob
*fg
)
1201 struct ubc_info
*uip
;
1203 int prod_signed
= 0;
1205 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1209 vp
= (struct vnode
*)fg
->fg_data
;
1215 if (!UBCINFOEXISTS(vp
)) {
1219 uip
= vp
->v_ubcinfo
;
1224 if (uip
->cs_blobs
== NULL
) {
1228 /* It is OK to extract the flag from the first blob
1229 * because all blobs of a vnode must have the same cs_flags */
1230 prod_signed
= (uip
->cs_blobs
->csb_flags
& CS_DEV_CODE
) == 0;
1238 csfg_get_supplement_prod_signed(struct fileglob
*fg __unused
)
1240 #if CONFIG_SUPPLEMENTAL_SIGNATURES
1241 struct ubc_info
*uip
;
1243 int prod_signed
= 0;
1245 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1249 vp
= (struct vnode
*)fg
->fg_data
;
1255 if (!UBCINFOEXISTS(vp
)) {
1259 uip
= vp
->v_ubcinfo
;
1264 if (uip
->cs_blob_supplement
== NULL
) {
1268 /* It is OK to extract the flag from the first blob
1269 * because all blobs of a vnode must have the same cs_flags */
1270 prod_signed
= (uip
->cs_blob_supplement
->csb_flags
& CS_DEV_CODE
) == 0;
1276 // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES
1277 // Indicate development signed if anyone tries to ask about one.
1283 * Function: csfg_get_identity
1285 * Description: This function returns the codesign identity
1289 csfg_get_identity(struct fileglob
*fg
, off_t offset
)
1292 struct cs_blob
*csblob
= NULL
;
1294 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1298 vp
= (struct vnode
*)fg
->fg_data
;
1303 csblob
= ubc_cs_blob_get(vp
, -1, -1, offset
);
1304 if (csblob
== NULL
) {
1308 return csblob_get_identity(csblob
);
1312 * Function: csfg_get_platform_identifier
1314 * Description: This function returns the codesign platform
1315 * identifier for the fileglob. Assumes the fileproc
1316 * is being held busy to keep the fileglob consistent.
1319 csfg_get_platform_identifier(struct fileglob
*fg
, off_t offset
)
1323 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1327 vp
= (struct vnode
*)fg
->fg_data
;
1332 return csvnode_get_platform_identifier(vp
, offset
);
1336 * Function: csvnode_get_platform_identifier
1338 * Description: This function returns the codesign platform
1339 * identifier for the vnode. Assumes a vnode reference
1343 csvnode_get_platform_identifier(struct vnode
*vp
, off_t offset
)
1345 struct cs_blob
*csblob
;
1346 const CS_CodeDirectory
*code_dir
;
1348 csblob
= ubc_cs_blob_get(vp
, -1, -1, offset
);
1349 if (csblob
== NULL
) {
1353 code_dir
= csblob
->csb_cd
;
1354 if (code_dir
== NULL
|| ntohl(code_dir
->length
) < 8) {
1358 return code_dir
->platform
;
1362 * Function: csproc_get_platform_identifier
1364 * Description: This function returns the codesign platform
1365 * identifier for the proc. Assumes proc will remain
1366 * valid through call.
1369 csproc_get_platform_identifier(struct proc
*p
)
1371 if (NULL
== p
->p_textvp
) {
1375 return csvnode_get_platform_identifier(p
->p_textvp
, p
->p_textoff
);
1379 cs_entitlement_flags(struct proc
*p
)
1381 return p
->p_csflags
& CS_ENTITLEMENT_FLAGS
;
1385 cs_restricted(struct proc
*p
)
1387 return (p
->p_csflags
& CS_RESTRICT
) ? 1 : 0;
1391 csproc_hardened_runtime(struct proc
* p
)
1393 return (p
->p_csflags
& CS_RUNTIME
) ? 1 : 0;
1397 * Function: csfg_get_path
1399 * Description: This populates the buffer passed in
1400 * with the path of the vnode
1401 * When calling this, the fileglob
1402 * cannot go away. The caller must have a
1403 * a reference on the fileglob or fileproc
1406 csfg_get_path(struct fileglob
*fg
, char *path
, int *len
)
1410 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
) {
1414 vp
= (struct vnode
*)fg
->fg_data
;
1416 /* vn_getpath returns 0 for success,
1417 * or an error code */
1418 return vn_getpath(vp
, path
, len
);
1422 * Retrieve the entitlements blob for a vnode
1424 * EINVAL no text vnode associated with the process
1425 * EBADEXEC invalid code signing data
1426 * 0 no error occurred
1428 * On success, out_start and out_length will point to the
1429 * entitlements blob if found; or will be set to NULL/zero
1430 * if there were no entitlements.
1433 cs_entitlements_blob_get_vnode(vnode_t vnode
, off_t offset
, void **out_start
, size_t *out_length
)
1435 struct cs_blob
*csblob
;
1440 if (vnode
== NULL
) {
1444 if ((csblob
= ubc_cs_blob_get(vnode
, -1, -1, offset
)) == NULL
) {
1448 return csblob_get_entitlements(csblob
, out_start
, out_length
);
1452 * Retrieve the entitlements blob for a process.
1454 * EINVAL no text vnode associated with the process
1455 * EBADEXEC invalid code signing data
1456 * 0 no error occurred
1458 * On success, out_start and out_length will point to the
1459 * entitlements blob if found; or will be set to NULL/zero
1460 * if there were no entitlements.
1463 cs_entitlements_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
1465 if ((p
->p_csflags
& CS_SIGNED
) == 0) {
1469 return cs_entitlements_blob_get_vnode(p
->p_textvp
, p
->p_textoff
, out_start
, out_length
);
1473 /* Retrieve the cached entitlements for a process
1475 * EINVAL no text vnode associated with the process
1476 * EBADEXEC invalid code signing data
1477 * 0 no error occurred
1479 * Note: the entitlements may be NULL if there is nothing cached.
1483 cs_entitlements_dictionary_copy(proc_t p
, void **entitlements
)
1485 struct cs_blob
*csblob
;
1487 *entitlements
= NULL
;
1489 if ((p
->p_csflags
& CS_SIGNED
) == 0) {
1493 if (NULL
== p
->p_textvp
) {
1497 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, -1, p
->p_textoff
)) == NULL
) {
1501 *entitlements
= csblob_entitlements_dictionary_copy(csblob
);
1505 /* Retrieve the codesign identity for a process.
1507 * NULL an error occured
1508 * string the cs_identity
1512 cs_identity_get(proc_t p
)
1514 struct cs_blob
*csblob
;
1516 if ((p
->p_csflags
& CS_SIGNED
) == 0) {
1520 if (NULL
== p
->p_textvp
) {
1524 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, -1, p
->p_textoff
)) == NULL
) {
1528 return csblob_get_identity(csblob
);
1532 * DO NOT USE THIS FUNCTION!
1533 * Use the properly guarded csproc_get_blob instead.
1535 * This is currently here to allow detached signatures to work
1536 * properly. The only user of this function is also checking
1541 cs_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
1543 struct cs_blob
*csblob
;
1548 if (NULL
== p
->p_textvp
) {
1552 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, -1, p
->p_textoff
)) == NULL
) {
1556 *out_start
= (void *)csblob
->csb_mem_kaddr
;
1557 *out_length
= csblob
->csb_mem_size
;
1563 * return cshash of a process, cdhash is of size CS_CDHASH_LEN
1567 cs_get_cdhash(struct proc
*p
)
1569 struct cs_blob
*csblob
;
1571 if ((p
->p_csflags
& CS_SIGNED
) == 0) {
1575 if (NULL
== p
->p_textvp
) {
1579 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, -1, p
->p_textoff
)) == NULL
) {
1583 ptrauth_utils_auth_blob_generic(csblob
->csb_cdhash
,
1584 sizeof(csblob
->csb_cdhash
),
1585 OS_PTRAUTH_DISCRIMINATOR("cs_blob.csb_cd_signature"),
1586 PTRAUTH_ADDR_DIVERSIFY
,
1587 csblob
->csb_cdhash_signature
);
1588 return csblob
->csb_cdhash
;