]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_core.c
xnu-2782.20.48.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
index 70fb531b359c4497125858701368bab1cb21fd7d..2bd9de059d460bb414c7a13b14082d4afec284f3 100644 (file)
 #include <mach/task.h>         /* task_suspend() */
 #include <kern/task.h>         /* get_task_numacts() */
 
+#include <security/audit/audit.h>
+
 typedef struct {
        int     flavor;                 /* the number for this flavor */
        mach_msg_type_number_t  count;  /* count of ints in this flavor */
 } mythread_state_flavor_t;
 
-#if defined (__ppc__)
-/* 64 bit */
-mythread_state_flavor_t thread_flavor_array64[]={
-               {PPC_THREAD_STATE64 , PPC_THREAD_STATE64_COUNT},
-               {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT}, 
-               {PPC_EXCEPTION_STATE64, PPC_EXCEPTION_STATE64_COUNT},
-               {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT}
-               };
-
-/* 32 bit */
-mythread_state_flavor_t thread_flavor_array[]={
-               {PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT},
-               {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT}, 
-               {PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT},
-               {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT}
-               };
-
-#elif defined (__i386__)
+#if defined (__i386__) || defined (__x86_64__)
 mythread_state_flavor_t thread_flavor_array [] = { 
                {x86_THREAD_STATE, x86_THREAD_STATE_COUNT},
                {x86_FLOAT_STATE, x86_FLOAT_STATE_COUNT},
@@ -108,18 +93,55 @@ typedef struct {
 /* 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,
        thread_state_t tstate, mach_msg_type_number_t *count);
 void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
+extern kern_return_t task_suspend_internal(task_t);
 
+static cpu_type_t process_cpu_type(proc_t proc);
+static cpu_type_t process_cpu_subtype(proc_t proc);
 
 #ifdef SECURE_KERNEL
-__private_extern__ int do_coredump = 0;        /* default: don't dump cores */
+__XNU_PRIVATE_EXTERN int do_coredump = 0;      /* default: don't dump cores */
 #else
-__private_extern__ int do_coredump = 1;        /* default: dump cores */
+__XNU_PRIVATE_EXTERN int do_coredump = 1;      /* default: dump cores */
+#endif
+__XNU_PRIVATE_EXTERN int sugid_coredump = 0; /* default: but not SGUID binaries */
+
+
+/* cpu_type returns only the most generic indication of the current CPU. */
+/* in a core we want to know the kind of process. */
+
+static cpu_type_t
+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)) {
+               what_we_think = CPU_TYPE_X86_64;
+       } else {
+               what_we_think = CPU_TYPE_I386;
+       }
 #endif
-__private_extern__ int sugid_coredump = 0; /* default: but not SGUID binaries */
+       return what_we_think;
+}
+
+static cpu_type_t
+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)) {
+               what_we_think = CPU_SUBTYPE_X86_64_ALL;
+       } else {
+               what_we_think = CPU_SUBTYPE_I386_ALL;
+       }
+#endif
+       return what_we_think;
+}
 
 void
 collectth_state(thread_t th_act, void *tirp)
@@ -168,6 +190,9 @@ collectth_state(thread_t th_act, void *tirp)
  *             indicated
  *
  * Parameters: core_proc                       Process to dump core [*]
+ *                             reserve_mb                      If non-zero, leave filesystem with
+ *                                                                     at least this much free space.
+ *                             ignore_ulimit           If set, ignore the process's core file ulimit.  
  *
  * Returns:    0                               Success
  *             EFAULT                          Failed
@@ -178,7 +203,7 @@ collectth_state(thread_t th_act, void *tirp)
  */
 #define        MAX_TSTATE_FLAVORS      10
 int
-coredump(proc_t core_proc)
+coredump(proc_t core_proc, uint32_t reserve_mb, int ignore_ulimit)
 {
 /* Begin assumptions that limit us to only the current process */
        vfs_context_t ctx = vfs_context_current();
@@ -192,9 +217,9 @@ coredump(proc_t core_proc)
        int             command_size, header_size, tstate_size;
        int             hoffset;
        off_t           foffset;
-       vm_map_offset_t vmoffset;
+       mach_vm_offset_t vmoffset;
        vm_offset_t     header;
-       vm_map_size_t   vmsize;
+       mach_vm_size_t  vmsize;
        vm_prot_t       prot;
        vm_prot_t       maxprot;
        vm_inherit_t    inherit;
@@ -216,12 +241,19 @@ coredump(proc_t core_proc)
        int             is_64 = 0;
        size_t          mach_header_sz = sizeof(struct mach_header);
        size_t          segment_command_sz = sizeof(struct segment_command);
+       
+       if (current_proc() != core_proc) {
+               panic("coredump() called against proc that is not current_proc: %p", core_proc);
+       }
 
        if (do_coredump == 0 ||         /* Not dumping at all */
            ( (sugid_coredump == 0) &&  /* Not dumping SUID/SGID binaries */
-             ( (cred->cr_svuid != cred->cr_ruid) ||
-               (cred->cr_svgid != cred->cr_rgid)))) {
-           
+             ( (kauth_cred_getsvuid(cred) != kauth_cred_getruid(cred)) ||
+               (kauth_cred_getsvgid(cred) != kauth_cred_getrgid(cred))))) {
+
+#if CONFIG_AUDIT
+               audit_proc_coredump(core_proc, NULL, EFAULT);
+#endif
                return (EFAULT);
        }
 
@@ -233,15 +265,16 @@ coredump(proc_t core_proc)
 
        mapsize = get_vmmap_size(map);
 
-       if (mapsize >=  core_proc->p_rlimit[RLIMIT_CORE].rlim_cur)
+       if ((mapsize >=  core_proc->p_rlimit[RLIMIT_CORE].rlim_cur) && (ignore_ulimit == 0))
                return (EFAULT);
-       (void) task_suspend(task);
+       (void) task_suspend_internal(task);
 
        MALLOC(alloced_name, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
 
        /* create name according to sysctl'able format string */
        /* if name creation fails, fall back to historical behaviour... */
-       if (proc_core_name(core_proc->p_comm, kauth_cred_getuid(cred),
+       if (alloced_name == NULL ||
+           proc_core_name(core_proc->p_comm, kauth_cred_getuid(cred),
                           core_proc->p_pid, alloced_name, MAXPATHLEN)) {
                snprintf(stack_name, sizeof(stack_name),
                         "/cores/core.%d", core_proc->p_pid);
@@ -266,6 +299,12 @@ coredump(proc_t core_proc)
        vnode_setattr(vp, &va, ctx);
        core_proc->p_acflag |= ACORE;
 
+       if ((reserve_mb > 0) &&
+           ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) {
+               error = ENOSPC;
+               goto out;
+       }
+
        /*
         *      If the task is modified while dumping the file
         *      (e.g., changes in threads or VM, the resulting
@@ -274,17 +313,8 @@ coredump(proc_t core_proc)
 
        thread_count = get_task_numacts(task);
        segment_count = get_vmmap_entries(map); /* XXX */
-#if defined (__ppc__)
-       if (is_64) {
-               tir1.flavor_count = sizeof(thread_flavor_array64)/sizeof(mythread_state_flavor_t);
-               bcopy(thread_flavor_array64, flavors,sizeof(thread_flavor_array64));
-       } else {
-#endif /* __ppc __ */
-               tir1.flavor_count = sizeof(thread_flavor_array)/sizeof(mythread_state_flavor_t);
-               bcopy(thread_flavor_array, flavors,sizeof(thread_flavor_array));
-#if defined (__ppc__)
-       }
-#endif /* __ppc __ */
+       tir1.flavor_count = sizeof(thread_flavor_array)/sizeof(mythread_state_flavor_t);
+       bcopy(thread_flavor_array, flavors,sizeof(thread_flavor_array));
        tstate_size = 0;
        for (i = 0; i < tir1.flavor_count; i++)
                tstate_size += sizeof(mythread_state_flavor_t) +
@@ -295,9 +325,10 @@ coredump(proc_t core_proc)
 
        header_size = command_size + mach_header_sz;
 
-       (void) kmem_alloc(kernel_map,
-                                   (vm_offset_t *)&header,
-                                   (vm_size_t)header_size);
+       if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size) != KERN_SUCCESS) {
+               error = ENOMEM;
+               goto out;
+       }
 
        /*
         *      Set up Mach-O header.
@@ -305,8 +336,8 @@ coredump(proc_t core_proc)
        if (is_64) {
                mh64 = (struct mach_header_64 *)header;
                mh64->magic = MH_MAGIC_64;
-               mh64->cputype = cpu_type();
-               mh64->cpusubtype = cpu_subtype();
+               mh64->cputype = process_cpu_type(core_proc);
+               mh64->cpusubtype = process_cpu_subtype(core_proc);
                mh64->filetype = MH_CORE;
                mh64->ncmds = segment_count + thread_count;
                mh64->sizeofcmds = command_size;
@@ -314,8 +345,8 @@ coredump(proc_t core_proc)
        } else {
                mh = (struct mach_header *)header;
                mh->magic = MH_MAGIC;
-               mh->cputype = cpu_type();
-               mh->cpusubtype = cpu_subtype();
+               mh->cputype = process_cpu_type(core_proc);
+               mh->cpusubtype = process_cpu_subtype(core_proc);
                mh->filetype = MH_CORE;
                mh->ncmds = segment_count + thread_count;
                mh->sizeofcmds = command_size;
@@ -391,10 +422,10 @@ coredump(proc_t core_proc)
                        sc->cmdsize = sizeof(struct segment_command);
                        /* segment name is zeroed by kmem_alloc */
                        sc->segname[0] = 0;
-                       sc->vmaddr = CAST_DOWN(vm_offset_t,vmoffset);
-                       sc->vmsize = CAST_DOWN(vm_size_t,vmsize);
-                       sc->fileoff = CAST_DOWN(uint32_t,foffset);
-                       sc->filesize = CAST_DOWN(uint32_t,vmsize);
+                       sc->vmaddr = CAST_DOWN_EXPLICIT(vm_offset_t,vmoffset);
+                       sc->vmsize = CAST_DOWN_EXPLICIT(vm_size_t,vmsize);
+                       sc->fileoff = CAST_DOWN_EXPLICIT(uint32_t,foffset); /* will never truncate */
+                       sc->filesize = CAST_DOWN_EXPLICIT(uint32_t,vmsize); /* will never truncate */
                        sc->maxprot = maxprot;
                        sc->initprot = prot;
                        sc->nsects = 0;
@@ -416,21 +447,11 @@ coredump(proc_t core_proc)
                if ((maxprot & VM_PROT_READ) == VM_PROT_READ
                        && vbr.user_tag != VM_MEMORY_IOKIT
                        && coredumpok(map,vmoffset)) {
-                       vm_map_size_t   tmp_vmsize = vmsize;
-                       off_t           xfer_foffset = foffset;
-
-                       //LP64todo - works around vn_rdwr_64() 2G limit
-                       while (tmp_vmsize > 0) {
-                               vm_map_size_t   xfer_vmsize = tmp_vmsize;
-                               if (xfer_vmsize > INT_MAX)
-                                       xfer_vmsize = INT_MAX;
-                               error = vn_rdwr_64(UIO_WRITE, vp,
-                                               vmoffset, xfer_vmsize, xfer_foffset,
+                       
+                       error = vn_rdwr_64(UIO_WRITE, vp, vmoffset, vmsize, foffset,
                                        (IS_64BIT_PROCESS(core_proc) ? UIO_USERSPACE64 : UIO_USERSPACE32), 
-                                       IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
-                               tmp_vmsize -= xfer_vmsize;
-                               xfer_foffset += xfer_vmsize;
-                       }
+                                       IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int64_t *) 0, core_proc);
+
                }
 
                hoffset += segment_command_sz;
@@ -465,11 +486,14 @@ coredump(proc_t core_proc)
         *      file.  OK to use a 32 bit write for this.
         */
        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
-                       UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
+                       UIO_SYSSPACE, IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
        kmem_free(kernel_map, header, header_size);
 out:
        error1 = vnode_close(vp, FWRITE, ctx);
 out2:
+#if CONFIG_AUDIT
+       audit_proc_coredump(core_proc, name, error);
+#endif
        if (alloced_name != NULL)
                FREE(alloced_name, M_TEMP);
        if (error == 0)