]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_core.c
xnu-4903.221.2.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
index 9477378efef55ebf546573a516b513eafc58661d..07acd675c7f7c170fb73c04f787a75fbd0a2b8ae 100644 (file)
  *     This file contains machine independent code for performing core dumps.
  *
  */
+#if CONFIG_COREDUMP
 
 #include <mach/vm_param.h>
 #include <mach/thread_status.h>
-
+#include <sys/content_protection.h>
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/signalvar.h>
 
 #include <security/audit/audit.h>
 
+#if CONFIG_CSR
+#include <sys/codesign.h>
+#include <sys/csr.h>
+#endif
+
 typedef struct {
        int     flavor;                 /* the number for this flavor */
        mach_msg_type_number_t  count;  /* count of ints in this flavor */
@@ -77,6 +83,21 @@ mythread_state_flavor_t thread_flavor_array [] = {
                {x86_EXCEPTION_STATE, x86_EXCEPTION_STATE_COUNT},
                };
 int mynum_flavors=3;
+#elif defined (__arm__)
+mythread_state_flavor_t thread_flavor_array[]={
+               {ARM_THREAD_STATE , ARM_THREAD_STATE_COUNT},
+               {ARM_VFP_STATE, ARM_VFP_STATE_COUNT}, 
+               {ARM_EXCEPTION_STATE, ARM_EXCEPTION_STATE_COUNT}
+               };
+int mynum_flavors=3;
+
+#elif defined (__arm64__)
+mythread_state_flavor_t thread_flavor_array[]={
+               {ARM_THREAD_STATE64 , ARM_THREAD_STATE64_COUNT},
+               /* ARM64_TODO: VFP */
+               {ARM_EXCEPTION_STATE64, ARM_EXCEPTION_STATE64_COUNT}
+               };
+int mynum_flavors=2;
 #else
 #error architecture not supported
 #endif
@@ -90,19 +111,13 @@ typedef struct {
        int flavor_count;
 } tir_t;
 
-/* XXX should be static */
-void collectth_state(thread_t th_act, void *tirp);
-
 extern int freespace_mb(vnode_t vp);
 
 /* XXX not in a Mach header anywhere */
-kern_return_t thread_getstatus(register thread_t act, int flavor,
+kern_return_t thread_getstatus(thread_t act, int flavor,
        thread_state_t tstate, mach_msg_type_number_t *count);
 void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
 
-static cpu_type_t process_cpu_type(proc_t proc);
-static cpu_type_t process_cpu_subtype(proc_t proc);
-
 #ifdef SECURE_KERNEL
 __XNU_PRIVATE_EXTERN int do_coredump = 0;      /* default: don't dump cores */
 #else
@@ -119,12 +134,19 @@ process_cpu_type(proc_t core_proc)
 {
        cpu_type_t what_we_think;
 #if defined (__i386__) || defined (__x86_64__)
-    if (IS_64BIT_PROCESS(core_proc)) {
+       if (IS_64BIT_PROCESS(core_proc)) {
                what_we_think = CPU_TYPE_X86_64;
        } else {
                what_we_think = CPU_TYPE_I386;
        }
+#elif defined (__arm__) || defined(__arm64__)
+       if (IS_64BIT_PROCESS(core_proc)) {
+               what_we_think = CPU_TYPE_ARM64;
+       } else {
+               what_we_think = CPU_TYPE_ARM;
+       }
 #endif
+
        return what_we_think;
 }
 
@@ -133,16 +155,22 @@ process_cpu_subtype(proc_t core_proc)
 {
        cpu_type_t what_we_think;
 #if defined (__i386__) || defined (__x86_64__)
-    if (IS_64BIT_PROCESS(core_proc)) {
+       if (IS_64BIT_PROCESS(core_proc)) {
                what_we_think = CPU_SUBTYPE_X86_64_ALL;
        } else {
                what_we_think = CPU_SUBTYPE_I386_ALL;
        }
+#elif defined (__arm__) || defined(__arm64__)
+       if (IS_64BIT_PROCESS(core_proc)) {
+               what_we_think = CPU_SUBTYPE_ARM64_ALL;
+       } else {
+               what_we_think = CPU_SUBTYPE_ARM_ALL;
+       }
 #endif
        return what_we_think;
 }
 
-void
+static void
 collectth_state(thread_t th_act, void *tirp)
 {
        vm_offset_t     header;
@@ -181,7 +209,6 @@ collectth_state(thread_t th_act, void *tirp)
                t->hoffset = hoffset;
 }
 
-
 /*
  * coredump
  *
@@ -256,6 +283,20 @@ coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags)
                return (EFAULT);
        }
 
+#if CONFIG_CSR
+       /* If the process is restricted, CSR isn't configured to allow
+        * restricted processes to be debugged, and CSR isn't configured in
+        * AppleInternal mode, then don't dump core. */
+       if (cs_restricted(core_proc) &&
+           csr_check(CSR_ALLOW_TASK_FOR_PID) &&
+           csr_check(CSR_ALLOW_APPLE_INTERNAL)) {
+#if CONFIG_AUDIT
+               audit_proc_coredump(core_proc, NULL, EFAULT);
+#endif
+               return (EFAULT);
+       }
+#endif
+
        if (IS_64BIT_PROCESS(core_proc)) {
                is_64 = 1;
                mach_header_sz = sizeof(struct mach_header_64);
@@ -297,6 +338,9 @@ coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags)
 
        VATTR_INIT(&va);        /* better to do it here than waste more stack in vnode_setsize */
        VATTR_SET(&va, va_data_size, 0);
+       if (core_proc == initproc) {
+               VATTR_SET(&va, va_dataprotect_class, PROTECTION_CLASS_D);
+       }
        vnode_setattr(vp, &va, ctx);
        core_proc->p_acflag |= ACORE;
 
@@ -507,3 +551,11 @@ out2:
 
        return (error);
 }
+
+#else /* CONFIG_COREDUMP */
+
+/* When core dumps aren't needed, no need to compile this file at all */
+
+#error assertion failed: this section is not compiled
+
+#endif /* CONFIG_COREDUMP */