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;
88 #if CONFIG_ENFORCE_SIGNED_CODE
89 #define DEFAULT_CS_ENFORCEMENT_ENABLE 1
91 #define DEFAULT_CS_ENFORCEMENT_ENABLE 0
93 SECURITY_READ_ONLY_LATE(int) cs_enforcement_enable
= DEFAULT_CS_ENFORCEMENT_ENABLE
;
95 #if CONFIG_ENFORCE_LIBRARY_VALIDATION
96 #define DEFAULT_CS_LIBRARY_VA_ENABLE 1
98 #define DEFAULT_CS_LIBRARY_VA_ENABLE 0
100 SECURITY_READ_ONLY_LATE(int) cs_library_val_enable
= DEFAULT_CS_LIBRARY_VA_ENABLE
;
102 #endif /* !SECURE_KERNEL */
103 int cs_all_vnodes
= 0;
105 static lck_grp_t
*cs_lockgrp
;
107 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_kill
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_kill
, 0, "");
108 SYSCTL_INT(_vm
, OID_AUTO
, cs_force_hard
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_force_hard
, 0, "");
109 SYSCTL_INT(_vm
, OID_AUTO
, cs_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_debug
, 0, "");
111 SYSCTL_INT(_vm
, OID_AUTO
, cs_all_vnodes
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_all_vnodes
, 0, "");
114 SYSCTL_INT(_vm
, OID_AUTO
, cs_enforcement
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_enforcement_enable
, 0, "");
115 SYSCTL_INT(_vm
, OID_AUTO
, cs_enforcement_panic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_enforcement_panic
, 0, "");
117 #if !CONFIG_ENFORCE_LIBRARY_VALIDATION
118 SYSCTL_INT(_vm
, OID_AUTO
, cs_library_validation
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &cs_library_val_enable
, 0, "");
120 #endif /* !SECURE_KERNEL */
122 int panic_on_cs_killed
= 0;
126 #if MACH_ASSERT && __x86_64__
127 panic_on_cs_killed
= 1;
128 #endif /* MACH_ASSERT && __x86_64__ */
129 PE_parse_boot_argn("panic_on_cs_killed", &panic_on_cs_killed
,
130 sizeof (panic_on_cs_killed
));
132 int disable_cs_enforcement
= 0;
133 PE_parse_boot_argn("cs_enforcement_disable", &disable_cs_enforcement
,
134 sizeof (disable_cs_enforcement
));
135 if (disable_cs_enforcement
) {
136 cs_enforcement_enable
= 0;
139 PE_parse_boot_argn("cs_enforcement_panic", &panic
, sizeof(panic
));
140 cs_enforcement_panic
= (panic
!= 0);
143 PE_parse_boot_argn("cs_debug", &cs_debug
, sizeof (cs_debug
));
145 #if !CONFIG_ENFORCE_LIBRARY_VALIDATION
146 PE_parse_boot_argn("cs_library_val_enable", &cs_library_val_enable
,
147 sizeof (cs_library_val_enable
));
149 #endif /* !SECURE_KERNEL */
151 lck_grp_attr_t
*attr
= lck_grp_attr_alloc_init();
152 cs_lockgrp
= lck_grp_alloc_init("KERNCS", attr
);
156 cs_allow_invalid(struct proc
*p
)
159 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
161 #if CONFIG_MACF && CONFIG_ENFORCE_SIGNED_CODE
162 /* There needs to be a MAC policy to implement this hook, or else the
163 * kill bits will be cleared here every time. If we have
164 * CONFIG_ENFORCE_SIGNED_CODE, we can assume there is a policy
165 * implementing the hook.
167 if( 0 != mac_proc_check_run_cs_invalid(p
)) {
168 if(cs_debug
) printf("CODE SIGNING: cs_allow_invalid() "
169 "not allowed: pid %d\n",
173 if(cs_debug
) printf("CODE SIGNING: cs_allow_invalid() "
177 p
->p_csflags
&= ~(CS_KILL
| CS_HARD
);
178 if (p
->p_csflags
& CS_VALID
)
180 p
->p_csflags
|= CS_DEBUGGED
;
183 vm_map_switch_protect(get_task_map(p
->task
), FALSE
);
185 return (p
->p_csflags
& (CS_KILL
| CS_HARD
)) == 0;
193 int send_kill
= 0, retval
= 0, verbose
= cs_debug
;
199 printf("CODE SIGNING: cs_invalid_page(0x%llx): p=%d[%s]\n",
200 vaddr
, p
->p_pid
, p
->p_comm
);
204 /* XXX for testing */
206 p
->p_csflags
|= CS_KILL
;
208 p
->p_csflags
|= CS_HARD
;
210 /* CS_KILL triggers a kill signal, and no you can't have the page. Nothing else. */
211 if (p
->p_csflags
& CS_KILL
) {
212 if (panic_on_cs_killed
&&
213 vaddr
>= SHARED_REGION_BASE
&&
214 vaddr
< SHARED_REGION_BASE
+ SHARED_REGION_SIZE
) {
215 panic("<rdar://14393620> cs_invalid_page(va=0x%llx): killing p=%p\n", (uint64_t) vaddr
, p
);
217 p
->p_csflags
|= CS_KILLED
;
224 if (panic_on_cs_killed
&&
225 vaddr
>= SHARED_REGION_BASE
&&
226 vaddr
< SHARED_REGION_BASE
+ SHARED_REGION_SIZE
) {
227 panic("<rdar://14393620> cs_invalid_page(va=0x%llx): cs error p=%p\n", (uint64_t) vaddr
, p
);
229 #endif /* __x86_64__ */
231 /* CS_HARD means fail the mapping operation so the process stays valid. */
232 if (p
->p_csflags
& CS_HARD
) {
235 if (p
->p_csflags
& CS_VALID
) {
236 p
->p_csflags
&= ~CS_VALID
;
237 cs_procs_invalidated
++;
241 csflags
= p
->p_csflags
;
245 printf("CODE SIGNING: cs_invalid_page(0x%llx): "
246 "p=%d[%s] final status 0x%x, %s page%s\n",
247 vaddr
, p
->p_pid
, p
->p_comm
, p
->p_csflags
,
248 retval
? "denying" : "allowing (remove VALID)",
249 send_kill
? " sending SIGKILL" : "");
252 threadsignal(current_thread(), SIGKILL
, EXC_BAD_ACCESS
);
259 * Assumes p (if passed in) is locked with proc_lock().
263 cs_enforcement(struct proc
*p
)
266 if (cs_enforcement_enable
)
272 if (p
!= NULL
&& (p
->p_csflags
& CS_ENFORCEMENT
))
279 * Library validation functions
282 cs_require_lv(struct proc
*p
)
285 if (cs_library_val_enable
)
291 if (p
!= NULL
&& (p
->p_csflags
& CS_REQUIRE_LV
))
298 * Function: csblob_get_platform_binary
300 * Description: This function returns true if the binary is
301 * in the trust cache.
305 csblob_get_platform_binary(struct cs_blob
*blob
)
307 if (blob
&& blob
->csb_platform_binary
)
313 * Function: csblob_get_flags
315 * Description: This function returns the flags for a given blob
319 csblob_get_flags(struct cs_blob
*blob
)
322 return blob
->csb_flags
;
327 * Function: csproc_get_blob
329 * Description: This function returns the cs_blob
333 csproc_get_blob(struct proc
*p
)
338 if (NULL
== p
->p_textvp
)
341 return ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
);
345 * Function: csproc_get_blob
347 * Description: This function returns the cs_blob
351 csvnode_get_blob(struct vnode
*vp
, off_t offset
)
353 return ubc_cs_blob_get(vp
, -1, offset
);
357 * Function: csblob_get_teamid
359 * Description: This function returns a pointer to the
363 csblob_get_teamid(struct cs_blob
*csblob
)
365 return csblob
->csb_teamid
;
369 * Function: csblob_get_identity
371 * Description: This function returns a pointer to the
375 csblob_get_identity(struct cs_blob
*csblob
)
377 const CS_CodeDirectory
*cd
;
379 cd
= (const CS_CodeDirectory
*)csblob_find_blob(csblob
, CSSLOT_CODEDIRECTORY
, CSMAGIC_CODEDIRECTORY
);
383 if (cd
->identOffset
== 0)
386 return ((const char *)cd
) + ntohl(cd
->identOffset
);
390 * Function: csblob_get_cdhash
392 * Description: This function returns a pointer to the
393 * cdhash of csblob (20 byte array)
396 csblob_get_cdhash(struct cs_blob
*csblob
)
398 return csblob
->csb_cdhash
;
402 * Function: csproc_get_teamid
404 * Description: This function returns a pointer to the
405 * team id of the process p
408 csproc_get_teamid(struct proc
*p
)
410 struct cs_blob
*csblob
;
412 csblob
= csproc_get_blob(p
);
416 return csblob_get_teamid(csblob
);
420 * Function: csvnode_get_teamid
422 * Description: This function returns a pointer to the
423 * team id of the binary at the given offset in vnode vp
426 csvnode_get_teamid(struct vnode
*vp
, off_t offset
)
428 struct cs_blob
*csblob
;
433 csblob
= ubc_cs_blob_get(vp
, -1, offset
);
437 return csblob_get_teamid(csblob
);
441 * Function: csproc_get_platform_binary
443 * Description: This function returns the value
444 * of the platform_binary field for proc p
447 csproc_get_platform_binary(struct proc
*p
)
449 struct cs_blob
*csblob
;
451 csblob
= csproc_get_blob(p
);
453 /* If there is no csblob this returns 0 because
454 it is true that it is not a platform binary */
455 return (csblob
== NULL
) ? 0 : csblob
->csb_platform_binary
;
459 csproc_get_platform_path(struct proc
*p
)
461 struct cs_blob
*csblob
= csproc_get_blob(p
);
463 return (csblob
== NULL
) ? 0 : csblob
->csb_platform_path
;
467 * Function: csfg_get_platform_binary
469 * Description: This function returns the
470 * platform binary field for the
474 csfg_get_platform_binary(struct fileglob
*fg
)
476 int platform_binary
= 0;
477 struct ubc_info
*uip
;
480 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
483 vp
= (struct vnode
*)fg
->fg_data
;
488 if (!UBCINFOEXISTS(vp
))
495 if (uip
->cs_blobs
== NULL
)
498 /* It is OK to extract the teamid from the first blob
499 because all blobs of a vnode must have the same teamid */
500 platform_binary
= uip
->cs_blobs
->csb_platform_binary
;
504 return platform_binary
;
508 csfg_get_cdhash(struct fileglob
*fg
, uint64_t offset
, size_t *cdhash_size
)
512 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
515 vp
= (struct vnode
*)fg
->fg_data
;
519 struct cs_blob
*csblob
= NULL
;
520 if ((csblob
= ubc_cs_blob_get(vp
, -1, offset
)) == NULL
)
524 *cdhash_size
= CS_CDHASH_LEN
;
526 return csblob
->csb_cdhash
;
530 * Function: csfg_get_teamid
532 * Description: This returns a pointer to
533 * the teamid for the fileglob fg
536 csfg_get_teamid(struct fileglob
*fg
)
538 struct ubc_info
*uip
;
539 const char *str
= NULL
;
542 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
545 vp
= (struct vnode
*)fg
->fg_data
;
550 if (!UBCINFOEXISTS(vp
))
557 if (uip
->cs_blobs
== NULL
)
560 /* It is OK to extract the teamid from the first blob
561 because all blobs of a vnode must have the same teamid */
562 str
= uip
->cs_blobs
->csb_teamid
;
570 cs_entitlement_flags(struct proc
*p
)
572 return (p
->p_csflags
& CS_ENTITLEMENT_FLAGS
);
576 cs_restricted(struct proc
*p
)
578 return (p
->p_csflags
& CS_RESTRICT
) ? 1 : 0;
582 * Function: csfg_get_path
584 * Description: This populates the buffer passed in
585 * with the path of the vnode
586 * When calling this, the fileglob
587 * cannot go away. The caller must have a
588 * a reference on the fileglob or fileproc
591 csfg_get_path(struct fileglob
*fg
, char *path
, int *len
)
595 if (FILEGLOB_DTYPE(fg
) != DTYPE_VNODE
)
598 vp
= (struct vnode
*)fg
->fg_data
;
600 /* vn_getpath returns 0 for success,
602 return vn_getpath(vp
, path
, len
);
605 /* Retrieve the entitlements blob for a process.
607 * EINVAL no text vnode associated with the process
608 * EBADEXEC invalid code signing data
609 * 0 no error occurred
611 * On success, out_start and out_length will point to the
612 * entitlements blob if found; or will be set to NULL/zero
613 * if there were no entitlements.
617 cs_entitlements_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
619 struct cs_blob
*csblob
;
624 if (NULL
== p
->p_textvp
)
627 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
630 return csblob_get_entitlements(csblob
, out_start
, out_length
);
633 /* Retrieve the codesign identity for a process.
635 * NULL an error occured
636 * string the cs_identity
640 cs_identity_get(proc_t p
)
642 struct cs_blob
*csblob
;
644 if (NULL
== p
->p_textvp
)
647 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
650 return csblob_get_identity(csblob
);
654 /* Retrieve the codesign blob for a process.
656 * EINVAL no text vnode associated with the process
657 * 0 no error occurred
659 * On success, out_start and out_length will point to the
660 * cms blob if found; or will be set to NULL/zero
661 * if there were no blob.
665 cs_blob_get(proc_t p
, void **out_start
, size_t *out_length
)
667 struct cs_blob
*csblob
;
672 if (NULL
== p
->p_textvp
)
675 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
678 *out_start
= (void *)csblob
->csb_mem_kaddr
;
679 *out_length
= csblob
->csb_mem_size
;
685 * return cshash of a process, cdhash is of size CS_CDHASH_LEN
689 cs_get_cdhash(struct proc
*p
)
691 struct cs_blob
*csblob
;
693 if (NULL
== p
->p_textvp
)
696 if ((csblob
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
)) == NULL
)
699 return csblob
->csb_cdhash
;