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/task.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_kern.h>
68 #include <kern/assert.h>
70 #include <pexpert/pexpert.h>
72 #include <mach/shared_region.h>
74 #include <libkern/section_keywords.h>
76 unsigned long cs_procs_killed
= 0;
77 unsigned long cs_procs_invalidated
= 0;
79 int cs_force_kill
= 0;
80 int cs_force_hard
= 0;
83 const int cs_enforcement_enable
= 1;
84 const int cs_library_val_enable
= 1;
85 #else /* !SECURE_KERNEL */
86 int cs_enforcement_panic
=0;
87 int cs_relax_platform_task_ports
= 0;
89 #if CONFIG_ENFORCE_SIGNED_CODE
90 #define DEFAULT_CS_ENFORCEMENT_ENABLE 1
92 #define DEFAULT_CS_ENFORCEMENT_ENABLE 0
94 SECURITY_READ_ONLY_LATE(int) cs_enforcement_enable
= DEFAULT_CS_ENFORCEMENT_ENABLE
;
96 #if CONFIG_ENFORCE_LIBRARY_VALIDATION
97 #define DEFAULT_CS_LIBRARY_VA_ENABLE 1
99 #define DEFAULT_CS_LIBRARY_VA_ENABLE 0
101 SECURITY_READ_ONLY_LATE(int) cs_library_val_enable
= DEFAULT_CS_LIBRARY_VA_ENABLE
;
103 #endif /* !SECURE_KERNEL */
104 int cs_all_vnodes
= 0;
106 static lck_grp_t
*cs_lockgrp
;
108 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_kill
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_kill
, 0, "");
109 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_hard
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_hard
, 0, "");
110 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_debug
, 0, "");
112 SYSCTL_INT(_vm
, OID_AUTO
, cs_all_vnodes
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_all_vnodes
, 0, "");
115 SYSCTL_INT(_vm
, OID_AUTO
, cs_enforcement
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_enforcement_enable
, 0, "");
116 SYSCTL_INT(_vm
, OID_AUTO
, cs_enforcement_panic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_enforcement_panic
, 0, "");
118 #if !CONFIG_ENFORCE_LIBRARY_VALIDATION
119 SYSCTL_INT(_vm
, OID_AUTO
, cs_library_validation
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_library_val_enable
, 0, "");
121 #endif /* !SECURE_KERNEL */
123 int panic_on_cs_killed
= 0;
128 #if MACH_ASSERT && __x86_64__
129 panic_on_cs_killed
= 1;
130 #endif /* MACH_ASSERT && __x86_64__ */
131 PE_parse_boot_argn("panic_on_cs_killed", &panic_on_cs_killed
,
132 sizeof (panic_on_cs_killed
));
134 int disable_cs_enforcement
= 0;
135 PE_parse_boot_argn("cs_enforcement_disable", &disable_cs_enforcement
,
136 sizeof (disable_cs_enforcement
));
137 if (disable_cs_enforcement
) {
138 cs_enforcement_enable
= 0;
141 PE_parse_boot_argn("cs_enforcement_panic", &panic
, sizeof(panic
));
142 cs_enforcement_panic
= (panic
!= 0);
145 PE_parse_boot_argn("cs_relax_platform_task_ports",
146 &cs_relax_platform_task_ports
,
147 sizeof(cs_relax_platform_task_ports
));
149 PE_parse_boot_argn("cs_debug", &cs_debug
, sizeof (cs_debug
));
151 #if !CONFIG_ENFORCE_LIBRARY_VALIDATION
152 PE_parse_boot_argn("cs_library_val_enable", &cs_library_val_enable
,
153 sizeof (cs_library_val_enable
));
155 #endif /* !SECURE_KERNEL */
157 lck_grp_attr_t
*attr
= lck_grp_attr_alloc_init();
158 cs_lockgrp
= lck_grp_alloc_init("KERNCS", attr
);
159 lck_grp_attr_free(attr
);
163 cs_allow_invalid(struct proc
*p
)
166 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
168 #if CONFIG_MACF && CONFIG_ENFORCE_SIGNED_CODE
169 /* There needs to be a MAC policy to implement this hook, or else the
170 * kill bits will be cleared here every time. If we have
171 * CONFIG_ENFORCE_SIGNED_CODE, we can assume there is a policy
172 * implementing the hook.
174 if( 0 != mac_proc_check_run_cs_invalid(p
)) {
175 if(cs_debug
) printf("CODE SIGNING: cs_allow_invalid() "
176 "not allowed: pid %d\n",
180 if(cs_debug
) printf("CODE SIGNING: cs_allow_invalid() "
184 p
->p_csflags
&= ~(CS_KILL
| CS_HARD
);
185 if (p
->p_csflags
& CS_VALID
)
187 p
->p_csflags
|= CS_DEBUGGED
;
192 vm_map_switch_protect(get_task_map(p
->task
), FALSE
);
194 return (p
->p_csflags
& (CS_KILL
| CS_HARD
)) == 0;
198 cs_invalid_page(addr64_t vaddr
, boolean_t
*cs_killed
)
201 int send_kill
= 0, retval
= 0, verbose
= cs_debug
;
207 printf("CODE SIGNING: cs_invalid_page(0x%llx): p=%d[%s]\n",
208 vaddr
, p
->p_pid
, p
->p_comm
);
212 /* XXX for testing */
214 p
->p_csflags
|= CS_KILL
;
216 p
->p_csflags
|= CS_HARD
;
218 /* CS_KILL triggers a kill signal, and no you can't have the page. Nothing else. */
219 if (p
->p_csflags
& CS_KILL
) {
220 p
->p_csflags
|= CS_KILLED
;
226 /* CS_HARD means fail the mapping operation so the process stays valid. */
227 if (p
->p_csflags
& CS_HARD
) {
230 if (p
->p_csflags
& CS_VALID
) {
231 p
->p_csflags
&= ~CS_VALID
;
232 cs_procs_invalidated
++;
236 csflags
= p
->p_csflags
;
240 printf("CODE SIGNING: cs_invalid_page(0x%llx): "
241 "p=%d[%s] final status 0x%x, %s page%s\n",
242 vaddr
, p
->p_pid
, p
->p_comm
, p
->p_csflags
,
243 retval
? "denying" : "allowing (remove VALID)",
244 send_kill
? " sending SIGKILL" : "");
247 /* We will set the exit reason for the thread later */
248 threadsignal(current_thread(), SIGKILL
, EXC_BAD_ACCESS
, FALSE
);
252 } else if (cs_killed
) {
261 * Assumes p (if passed in) is locked with proc_lock().
265 cs_enforcement(struct proc
*p
)
268 if (cs_enforcement_enable
)
274 if (p
!= NULL
&& (p
->p_csflags
& CS_ENFORCEMENT
))
281 * Returns whether a given process is still valid.
284 cs_valid(struct proc
*p
)
290 if (p
!= NULL
&& (p
->p_csflags
& CS_VALID
))
297 * Library validation functions
300 cs_require_lv(struct proc
*p
)
303 if (cs_library_val_enable
)
309 if (p
!= NULL
&& (p
->p_csflags
& CS_REQUIRE_LV
))
316 * <rdar://problem/24634089> added to allow system level library
317 * validation check at mac_cred_label_update_execve time
320 cs_system_require_lv(void)
322 return cs_library_val_enable
? 1 : 0;
326 * Function: csblob_get_base_offset
328 * Description: This function returns the base offset into the Mach-O binary
333 csblob_get_base_offset(struct cs_blob
*blob
)
335 return blob
->csb_base_offset
;
339 * Function: csblob_get_size
341 * Description: This function returns the size of a given blob.
345 csblob_get_size(struct cs_blob
*blob
)
347 return blob
->csb_mem_size
;
351 * Function: csblob_get_addr
353 * Description: This function returns the address of a given blob.
357 csblob_get_addr(struct cs_blob
*blob
)
359 return blob
->csb_mem_kaddr
;
363 * Function: csblob_get_platform_binary
365 * Description: This function returns true if the binary is
366 * in the trust cache.
370 csblob_get_platform_binary(struct cs_blob
*blob
)
372 if (blob
&& blob
->csb_platform_binary
)
378 * Function: csblob_get_flags
380 * Description: This function returns the flags for a given blob
384 csblob_get_flags(struct cs_blob
*blob
)
386 return blob
->csb_flags
;
390 * Function: csblob_get_hashtype
392 * Description: This function returns the hash type for a given blob
396 csblob_get_hashtype(struct cs_blob
const * const blob
)
398 return blob
->csb_hashtype
!= NULL
? cs_hash_type(blob
->csb_hashtype
) : 0;
402 * Function: csproc_get_blob
404 * Description: This function returns the cs_blob
408 csproc_get_blob(struct proc
*p
)
413 if (NULL
== p
->p_textvp
)
416 return ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
);
420 * Function: csproc_get_blob
422 * Description: This function returns the cs_blob
426 csvnode_get_blob(struct vnode
*vp
, off_t offset
)
428 return ubc_cs_blob_get(vp
, -1, offset
);
432 * Function: csblob_get_teamid
434 * Description: This function returns a pointer to the
438 csblob_get_teamid(struct cs_blob
*csblob
)
440 return csblob
->csb_teamid
;
444 * Function: csblob_get_identity
446 * Description: This function returns a pointer to the
450 csblob_get_identity(struct cs_blob
*csblob
)
452 const CS_CodeDirectory
*cd
;
454 cd
= (const CS_CodeDirectory
*)csblob_find_blob(csblob
, CSSLOT_CODEDIRECTORY
, CSMAGIC_CODEDIRECTORY
);
458 if (cd
->identOffset
== 0)
461 return ((const char *)cd
) + ntohl(cd
->identOffset
);
465 * Function: csblob_get_cdhash
467 * Description: This function returns a pointer to the
468 * cdhash of csblob (20 byte array)
471 csblob_get_cdhash(struct cs_blob
*csblob
)
473 return csblob
->csb_cdhash
;
477 * Function: csblob_get_signer_type
479 * Description: This function returns the signer type
483 csblob_get_signer_type(struct cs_blob
*csblob
)
485 return csblob
->csb_signer_type
;
489 csblob_entitlements_dictionary_copy(struct cs_blob
*csblob
)
491 if (!csblob
->csb_entitlements
) return NULL
;
492 osobject_retain(csblob
->csb_entitlements
);
493 return csblob
->csb_entitlements
;
497 csblob_entitlements_dictionary_set(struct cs_blob
*csblob
, void * entitlements
)
499 assert(csblob
->csb_entitlements
== NULL
);
500 if (entitlements
) osobject_retain(entitlements
);
501 csblob
->csb_entitlements
= entitlements
;
505 * Function: csproc_get_teamid
507 * Description: This function returns a pointer to the
508 * team id of the process p
511 csproc_get_teamid(struct proc
*p
)
513 struct cs_blob
*csblob
;
515 csblob
= csproc_get_blob(p
);
519 return csblob_get_teamid(csblob
);
523 * Function: csproc_get_signer_type
525 * Description: This function returns the signer type
529 csproc_get_signer_type(struct proc
*p
)
531 struct cs_blob
*csblob
;
533 csblob
= csproc_get_blob(p
);
535 return CS_SIGNER_TYPE_UNKNOWN
;
537 return csblob_get_signer_type(csblob
);
541 * Function: csvnode_get_teamid
543 * Description: This function returns a pointer to the
544 * team id of the binary at the given offset in vnode vp
547 csvnode_get_teamid(struct vnode
*vp
, off_t offset
)
549 struct cs_blob
*csblob
;
554 csblob
= ubc_cs_blob_get(vp
, -1, offset
);
558 return csblob_get_teamid(csblob
);
562 * Function: csproc_get_platform_binary
564 * Description: This function returns the value
565 * of the platform_binary field for proc p
568 csproc_get_platform_binary(struct proc
*p
)
570 struct cs_blob
*csblob
;
572 csblob
= csproc_get_blob(p
);
574 /* If there is no csblob this returns 0 because
575 it is true that it is not a platform binary */
576 return (csblob
== NULL
) ? 0 : csblob
->csb_platform_binary
;
580 csproc_get_platform_path(struct proc
*p
)
582 struct cs_blob
*csblob
= csproc_get_blob(p
);
584 return (csblob
== NULL
) ? 0 : csblob
->csb_platform_path
;
587 #if DEVELOPMENT || DEBUG
589 csproc_clear_platform_binary(struct proc
*p
)
591 struct cs_blob
*csblob
= csproc_get_blob(p
);
593 if (csblob
== NULL
) {
598 printf("clearing platform binary on proc/task: pid = %d\n", p
->p_pid
);
601 csblob
->csb_platform_binary
= 0;
602 csblob
->csb_platform_path
= 0;
603 task_set_platform_binary(proc_task(p
), FALSE
);
608 * Function: csproc_get_prod_signed
610 * Description: Returns 1 if process is not signed with a developer identity.
611 * Note the inverted meaning from the cs_flag to make the error case safer.
612 * Will go away with rdar://problem/28322552.
615 csproc_get_prod_signed(struct proc
*p
)
617 return ((p
->p_csflags
& CS_DEV_CODE
) == 0);
622 * Function: csfg_get_platform_binary
624 * Description: This function returns the
625 * platform binary field for the
629 csfg_get_platform_binary(struct fileglob
*fg
)
631 int platform_binary
= 0;
632 struct ubc_info
*uip
;
635 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
638 vp
= (struct vnode
*)fg
->fg_data
;
643 if (!UBCINFOEXISTS(vp
))
650 if (uip
->cs_blobs
== NULL
)
653 /* It is OK to extract the teamid from the first blob
654 because all blobs of a vnode must have the same teamid */
655 platform_binary
= uip
->cs_blobs
->csb_platform_binary
;
659 return platform_binary
;
663 csfg_get_cdhash(struct fileglob
*fg
, uint64_t offset
, size_t *cdhash_size
)
667 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
670 vp
= (struct vnode
*)fg
->fg_data
;
674 struct cs_blob
*csblob
= NULL
;
675 if ((csblob
= ubc_cs_blob_get(vp
, -1, offset
)) == NULL
)
679 *cdhash_size
= CS_CDHASH_LEN
;
681 return csblob
->csb_cdhash
;
685 * Function: csfg_get_signer_type
687 * Description: This returns the signer type
688 * for the fileglob fg
691 csfg_get_signer_type(struct fileglob
*fg
)
693 struct ubc_info
*uip
;
694 unsigned int signer_type
= CS_SIGNER_TYPE_UNKNOWN
;
697 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
698 return CS_SIGNER_TYPE_UNKNOWN
;
700 vp
= (struct vnode
*)fg
->fg_data
;
702 return CS_SIGNER_TYPE_UNKNOWN
;
705 if (!UBCINFOEXISTS(vp
))
712 if (uip
->cs_blobs
== NULL
)
715 /* It is OK to extract the signer type from the first blob,
716 because all blobs of a vnode must have the same signer type. */
717 signer_type
= uip
->cs_blobs
->csb_signer_type
;
725 * Function: csfg_get_teamid
727 * Description: This returns a pointer to
728 * the teamid for the fileglob fg
731 csfg_get_teamid(struct fileglob
*fg
)
733 struct ubc_info
*uip
;
734 const char *str
= NULL
;
737 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
740 vp
= (struct vnode
*)fg
->fg_data
;
745 if (!UBCINFOEXISTS(vp
))
752 if (uip
->cs_blobs
== NULL
)
755 /* It is OK to extract the teamid from the first blob
756 because all blobs of a vnode must have the same teamid */
757 str
= uip
->cs_blobs
->csb_teamid
;
765 * Function: csfg_get_prod_signed
767 * Description: Returns 1 if code is not signed with a developer identity.
768 * Note the inverted meaning from the cs_flag to make the error case safer.
769 * Will go away with rdar://problem/28322552.
772 csfg_get_prod_signed(struct fileglob
*fg
)
774 struct ubc_info
*uip
;
778 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
781 vp
= (struct vnode
*)fg
->fg_data
;
786 if (!UBCINFOEXISTS(vp
))
793 if (uip
->cs_blobs
== NULL
)
796 /* It is OK to extract the flag from the first blob
797 because all blobs of a vnode must have the same cs_flags */
798 prod_signed
= (uip
->cs_blobs
->csb_flags
& CS_DEV_CODE
) == 0;
807 cs_entitlement_flags(struct proc
*p
)
809 return (p
->p_csflags
& CS_ENTITLEMENT_FLAGS
);
813 cs_restricted(struct proc
*p
)
815 return (p
->p_csflags
& CS_RESTRICT
) ? 1 : 0;
819 * Function: csfg_get_path
821 * Description: This populates the buffer passed in
822 * with the path of the vnode
823 * When calling this, the fileglob
824 * cannot go away. The caller must have a
825 * a reference on the fileglob or fileproc
828 csfg_get_path(struct fileglob
*fg
, char *path
, int *len
)
832 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
835 vp
= (struct vnode
*)fg
->fg_data
;
837 /* vn_getpath returns 0 for success,
839 return vn_getpath(vp
, path
, len
);
842 /* Retrieve the entitlements blob for a process.
844 * EINVAL no text vnode associated with the process
845 * EBADEXEC invalid code signing data
846 * 0 no error occurred
848 * On success, out_start and out_length will point to the
849 * entitlements blob if found; or will be set to NULL/zero
850 * if there were no entitlements.
854 cs_entitlements_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
856 struct cs_blob
*csblob
;
861 if (NULL
== p
->p_textvp
)
864 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
867 return csblob_get_entitlements(csblob
, out_start
, out_length
);
870 /* Retrieve the codesign identity for a process.
872 * NULL an error occured
873 * string the cs_identity
877 cs_identity_get(proc_t p
)
879 struct cs_blob
*csblob
;
881 if (NULL
== p
->p_textvp
)
884 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
887 return csblob_get_identity(csblob
);
891 /* Retrieve the codesign blob for a process.
893 * EINVAL no text vnode associated with the process
894 * 0 no error occurred
896 * On success, out_start and out_length will point to the
897 * cms blob if found; or will be set to NULL/zero
898 * if there were no blob.
902 cs_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
904 struct cs_blob
*csblob
;
909 if (NULL
== p
->p_textvp
)
912 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
915 *out_start
= (void *)csblob
->csb_mem_kaddr
;
916 *out_length
= csblob
->csb_mem_size
;
922 * return cshash of a process, cdhash is of size CS_CDHASH_LEN
926 cs_get_cdhash(struct proc
*p
)
928 struct cs_blob
*csblob
;
930 if (NULL
== p
->p_textvp
)
933 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
936 return csblob
->csb_cdhash
;