]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_core.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
29 *
30 * File: bsd/kern/kern_core.c
31 *
32 * This file contains machine independent code for performing core dumps.
33 *
34 */
35 #if CONFIG_COREDUMP
36
37 #include <mach/vm_param.h>
38 #include <mach/thread_status.h>
39 #include <sys/content_protection.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/signalvar.h>
43 #include <sys/resourcevar.h>
44 #include <sys/namei.h>
45 #include <sys/vnode_internal.h>
46 #include <sys/proc_internal.h>
47 #include <sys/kauth.h>
48 #include <sys/timeb.h>
49 #include <sys/times.h>
50 #include <sys/acct.h>
51 #include <sys/file_internal.h>
52 #include <sys/uio.h>
53 #include <sys/kernel.h>
54 #include <sys/stat.h>
55
56 #include <mach-o/loader.h>
57 #include <mach/vm_region.h>
58 #include <mach/vm_statistics.h>
59
60 #include <vm/vm_kern.h>
61 #include <vm/vm_protos.h> /* last */
62 #include <vm/vm_map.h> /* current_map() */
63 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
64 #include <mach/task.h> /* task_suspend() */
65 #include <kern/task.h> /* get_task_numacts() */
66
67 #include <security/audit/audit.h>
68
69 #if CONFIG_CSR
70 #include <sys/codesign.h>
71 #include <sys/csr.h>
72 #endif
73
74 typedef struct {
75 int flavor; /* the number for this flavor */
76 mach_msg_type_number_t count; /* count of ints in this flavor */
77 } mythread_state_flavor_t;
78
79 #if defined (__i386__) || defined (__x86_64__)
80 mythread_state_flavor_t thread_flavor_array [] = {
81 {x86_THREAD_STATE, x86_THREAD_STATE_COUNT},
82 {x86_FLOAT_STATE, x86_FLOAT_STATE_COUNT},
83 {x86_EXCEPTION_STATE, x86_EXCEPTION_STATE_COUNT},
84 };
85 int mynum_flavors=3;
86 #elif defined (__arm__)
87 mythread_state_flavor_t thread_flavor_array[]={
88 {ARM_THREAD_STATE , ARM_THREAD_STATE_COUNT},
89 {ARM_VFP_STATE, ARM_VFP_STATE_COUNT},
90 {ARM_EXCEPTION_STATE, ARM_EXCEPTION_STATE_COUNT}
91 };
92 int mynum_flavors=3;
93
94 #elif defined (__arm64__)
95 mythread_state_flavor_t thread_flavor_array[]={
96 {ARM_THREAD_STATE64 , ARM_THREAD_STATE64_COUNT},
97 /* ARM64_TODO: VFP */
98 {ARM_EXCEPTION_STATE64, ARM_EXCEPTION_STATE64_COUNT}
99 };
100 int mynum_flavors=2;
101 #else
102 #error architecture not supported
103 #endif
104
105
106 typedef struct {
107 vm_offset_t header;
108 int hoffset;
109 mythread_state_flavor_t *flavors;
110 int tstate_size;
111 int flavor_count;
112 } tir_t;
113
114 extern int freespace_mb(vnode_t vp);
115
116 /* XXX not in a Mach header anywhere */
117 kern_return_t thread_getstatus(thread_t act, int flavor,
118 thread_state_t tstate, mach_msg_type_number_t *count);
119 void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
120
121 #ifdef SECURE_KERNEL
122 __XNU_PRIVATE_EXTERN int do_coredump = 0; /* default: don't dump cores */
123 #else
124 __XNU_PRIVATE_EXTERN int do_coredump = 1; /* default: dump cores */
125 #endif
126 __XNU_PRIVATE_EXTERN int sugid_coredump = 0; /* default: but not SGUID binaries */
127
128
129 /* cpu_type returns only the most generic indication of the current CPU. */
130 /* in a core we want to know the kind of process. */
131
132 static cpu_type_t
133 process_cpu_type(proc_t core_proc)
134 {
135 cpu_type_t what_we_think;
136 #if defined (__i386__) || defined (__x86_64__)
137 if (IS_64BIT_PROCESS(core_proc)) {
138 what_we_think = CPU_TYPE_X86_64;
139 } else {
140 what_we_think = CPU_TYPE_I386;
141 }
142 #elif defined (__arm__) || defined(__arm64__)
143 if (IS_64BIT_PROCESS(core_proc)) {
144 what_we_think = CPU_TYPE_ARM64;
145 } else {
146 what_we_think = CPU_TYPE_ARM;
147 }
148 #endif
149 return what_we_think;
150 }
151
152 static cpu_type_t
153 process_cpu_subtype(proc_t core_proc)
154 {
155 cpu_type_t what_we_think;
156 #if defined (__i386__) || defined (__x86_64__)
157 if (IS_64BIT_PROCESS(core_proc)) {
158 what_we_think = CPU_SUBTYPE_X86_64_ALL;
159 } else {
160 what_we_think = CPU_SUBTYPE_I386_ALL;
161 }
162 #elif defined (__arm__) || defined(__arm64__)
163 if (IS_64BIT_PROCESS(core_proc)) {
164 what_we_think = CPU_SUBTYPE_ARM64_ALL;
165 } else {
166 what_we_think = CPU_SUBTYPE_ARM_ALL;
167 }
168 #endif
169 return what_we_think;
170 }
171
172 static void
173 collectth_state(thread_t th_act, void *tirp)
174 {
175 vm_offset_t header;
176 int hoffset, i ;
177 mythread_state_flavor_t *flavors;
178 struct thread_command *tc;
179 tir_t *t = (tir_t *)tirp;
180
181 /*
182 * Fill in thread command structure.
183 */
184 header = t->header;
185 hoffset = t->hoffset;
186 flavors = t->flavors;
187
188 tc = (struct thread_command *) (header + hoffset);
189 tc->cmd = LC_THREAD;
190 tc->cmdsize = sizeof(struct thread_command)
191 + t->tstate_size;
192 hoffset += sizeof(struct thread_command);
193 /*
194 * Follow with a struct thread_state_flavor and
195 * the appropriate thread state struct for each
196 * thread state flavor.
197 */
198 for (i = 0; i < t->flavor_count; i++) {
199 *(mythread_state_flavor_t *)(header+hoffset) =
200 flavors[i];
201 hoffset += sizeof(mythread_state_flavor_t);
202 thread_getstatus(th_act, flavors[i].flavor,
203 (thread_state_t)(header+hoffset),
204 &flavors[i].count);
205 hoffset += flavors[i].count*sizeof(int);
206 }
207
208 t->hoffset = hoffset;
209 }
210
211 /*
212 * coredump
213 *
214 * Description: Create a core image on the file "core" for the process
215 * indicated
216 *
217 * Parameters: core_proc Process to dump core [*]
218 * reserve_mb If non-zero, leave filesystem with
219 * at least this much free space.
220 * coredump_flags Extra options (ignore rlimit, run fsync)
221 *
222 * Returns: 0 Success
223 * EFAULT Failed
224 *
225 * IMPORTANT: This function can only be called on the current process, due
226 * to assumptions below; see variable declaration section for
227 * details.
228 */
229 #define MAX_TSTATE_FLAVORS 10
230 int
231 coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags)
232 {
233 /* Begin assumptions that limit us to only the current process */
234 vfs_context_t ctx = vfs_context_current();
235 vm_map_t map = current_map();
236 task_t task = current_task();
237 /* End assumptions */
238 kauth_cred_t cred = vfs_context_ucred(ctx);
239 int error = 0;
240 struct vnode_attr va;
241 int thread_count, segment_count;
242 int command_size, header_size, tstate_size;
243 int hoffset;
244 off_t foffset;
245 mach_vm_offset_t vmoffset;
246 vm_offset_t header;
247 mach_vm_size_t vmsize;
248 vm_prot_t prot;
249 vm_prot_t maxprot;
250 vm_inherit_t inherit;
251 int error1 = 0;
252 char stack_name[MAXCOMLEN+6];
253 char *alloced_name = NULL;
254 char *name;
255 mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
256 vm_size_t mapsize;
257 int i;
258 uint32_t nesting_depth = 0;
259 kern_return_t kret;
260 struct vm_region_submap_info_64 vbr;
261 mach_msg_type_number_t vbrcount = 0;
262 tir_t tir1;
263 struct vnode * vp;
264 struct mach_header *mh = NULL; /* protected by is_64 */
265 struct mach_header_64 *mh64 = NULL; /* protected by is_64 */
266 int is_64 = 0;
267 size_t mach_header_sz = sizeof(struct mach_header);
268 size_t segment_command_sz = sizeof(struct segment_command);
269
270 if (current_proc() != core_proc) {
271 panic("coredump() called against proc that is not current_proc: %p", core_proc);
272 }
273
274 if (do_coredump == 0 || /* Not dumping at all */
275 ( (sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */
276 ( (kauth_cred_getsvuid(cred) != kauth_cred_getruid(cred)) ||
277 (kauth_cred_getsvgid(cred) != kauth_cred_getrgid(cred))))) {
278
279 #if CONFIG_AUDIT
280 audit_proc_coredump(core_proc, NULL, EFAULT);
281 #endif
282 return (EFAULT);
283 }
284
285 #if CONFIG_CSR
286 /* If the process is restricted, CSR isn't configured to allow
287 * restricted processes to be debugged, and CSR isn't configured in
288 * AppleInternal mode, then don't dump core. */
289 if (cs_restricted(core_proc) &&
290 csr_check(CSR_ALLOW_TASK_FOR_PID) &&
291 csr_check(CSR_ALLOW_APPLE_INTERNAL)) {
292 #if CONFIG_AUDIT
293 audit_proc_coredump(core_proc, NULL, EFAULT);
294 #endif
295 return (EFAULT);
296 }
297 #endif
298
299 if (IS_64BIT_PROCESS(core_proc)) {
300 is_64 = 1;
301 mach_header_sz = sizeof(struct mach_header_64);
302 segment_command_sz = sizeof(struct segment_command_64);
303 }
304
305 mapsize = get_vmmap_size(map);
306
307 if (((coredump_flags & COREDUMP_IGNORE_ULIMIT) == 0) &&
308 (mapsize >= core_proc->p_rlimit[RLIMIT_CORE].rlim_cur))
309 return (EFAULT);
310
311 (void) task_suspend_internal(task);
312
313 MALLOC(alloced_name, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
314
315 /* create name according to sysctl'able format string */
316 /* if name creation fails, fall back to historical behaviour... */
317 if (alloced_name == NULL ||
318 proc_core_name(core_proc->p_comm, kauth_cred_getuid(cred),
319 core_proc->p_pid, alloced_name, MAXPATHLEN)) {
320 snprintf(stack_name, sizeof(stack_name),
321 "/cores/core.%d", core_proc->p_pid);
322 name = stack_name;
323 } else
324 name = alloced_name;
325
326 if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, ctx)))
327 goto out2;
328
329 VATTR_INIT(&va);
330 VATTR_WANTED(&va, va_nlink);
331 /* Don't dump to non-regular files or files with links. */
332 if (vp->v_type != VREG ||
333 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
334 error = EFAULT;
335 goto out;
336 }
337
338 VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */
339 VATTR_SET(&va, va_data_size, 0);
340 if (core_proc == initproc) {
341 VATTR_SET(&va, va_dataprotect_class, PROTECTION_CLASS_D);
342 }
343 vnode_setattr(vp, &va, ctx);
344 core_proc->p_acflag |= ACORE;
345
346 if ((reserve_mb > 0) &&
347 ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) {
348 error = ENOSPC;
349 goto out;
350 }
351
352 /*
353 * If the task is modified while dumping the file
354 * (e.g., changes in threads or VM, the resulting
355 * file will not necessarily be correct.
356 */
357
358 thread_count = get_task_numacts(task);
359 segment_count = get_vmmap_entries(map); /* XXX */
360 tir1.flavor_count = sizeof(thread_flavor_array)/sizeof(mythread_state_flavor_t);
361 bcopy(thread_flavor_array, flavors,sizeof(thread_flavor_array));
362 tstate_size = 0;
363 for (i = 0; i < tir1.flavor_count; i++)
364 tstate_size += sizeof(mythread_state_flavor_t) +
365 (flavors[i].count * sizeof(int));
366 command_size = segment_count * segment_command_sz +
367 thread_count*sizeof(struct thread_command) +
368 tstate_size*thread_count;
369
370 header_size = command_size + mach_header_sz;
371
372 if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
373 error = ENOMEM;
374 goto out;
375 }
376
377 /*
378 * Set up Mach-O header.
379 */
380 if (is_64) {
381 mh64 = (struct mach_header_64 *)header;
382 mh64->magic = MH_MAGIC_64;
383 mh64->cputype = process_cpu_type(core_proc);
384 mh64->cpusubtype = process_cpu_subtype(core_proc);
385 mh64->filetype = MH_CORE;
386 mh64->ncmds = segment_count + thread_count;
387 mh64->sizeofcmds = command_size;
388 mh64->reserved = 0; /* 8 byte alignment */
389 } else {
390 mh = (struct mach_header *)header;
391 mh->magic = MH_MAGIC;
392 mh->cputype = process_cpu_type(core_proc);
393 mh->cpusubtype = process_cpu_subtype(core_proc);
394 mh->filetype = MH_CORE;
395 mh->ncmds = segment_count + thread_count;
396 mh->sizeofcmds = command_size;
397 }
398
399 hoffset = mach_header_sz; /* offset into header */
400 foffset = round_page(header_size); /* offset into file */
401 vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */
402
403 /*
404 * We use to check for an error, here, now we try and get
405 * as much as we can
406 */
407 while (segment_count > 0) {
408 struct segment_command *sc;
409 struct segment_command_64 *sc64;
410
411 /*
412 * Get region information for next region.
413 */
414
415 while (1) {
416 vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
417 if((kret = mach_vm_region_recurse(map,
418 &vmoffset, &vmsize, &nesting_depth,
419 (vm_region_recurse_info_t)&vbr,
420 &vbrcount)) != KERN_SUCCESS) {
421 break;
422 }
423 /*
424 * If we get a valid mapping back, but we're dumping
425 * a 32 bit process, and it's over the allowable
426 * address space of a 32 bit process, it's the same
427 * as if mach_vm_region_recurse() failed.
428 */
429 if (!(is_64) &&
430 (vmoffset + vmsize > VM_MAX_ADDRESS)) {
431 kret = KERN_INVALID_ADDRESS;
432 break;
433 }
434 if(vbr.is_submap) {
435 nesting_depth++;
436 continue;
437 } else {
438 break;
439 }
440 }
441 if(kret != KERN_SUCCESS)
442 break;
443
444 prot = vbr.protection;
445 maxprot = vbr.max_protection;
446 inherit = vbr.inheritance;
447 /*
448 * Fill in segment command structure.
449 */
450 if (is_64) {
451 sc64 = (struct segment_command_64 *)(header + hoffset);
452 sc64->cmd = LC_SEGMENT_64;
453 sc64->cmdsize = sizeof(struct segment_command_64);
454 /* segment name is zeroed by kmem_alloc */
455 sc64->segname[0] = 0;
456 sc64->vmaddr = vmoffset;
457 sc64->vmsize = vmsize;
458 sc64->fileoff = foffset;
459 sc64->filesize = vmsize;
460 sc64->maxprot = maxprot;
461 sc64->initprot = prot;
462 sc64->nsects = 0;
463 sc64->flags = 0;
464 } else {
465 sc = (struct segment_command *) (header + hoffset);
466 sc->cmd = LC_SEGMENT;
467 sc->cmdsize = sizeof(struct segment_command);
468 /* segment name is zeroed by kmem_alloc */
469 sc->segname[0] = 0;
470 sc->vmaddr = CAST_DOWN_EXPLICIT(vm_offset_t,vmoffset);
471 sc->vmsize = CAST_DOWN_EXPLICIT(vm_size_t,vmsize);
472 sc->fileoff = CAST_DOWN_EXPLICIT(uint32_t,foffset); /* will never truncate */
473 sc->filesize = CAST_DOWN_EXPLICIT(uint32_t,vmsize); /* will never truncate */
474 sc->maxprot = maxprot;
475 sc->initprot = prot;
476 sc->nsects = 0;
477 sc->flags = 0;
478 }
479
480 /*
481 * Write segment out. Try as hard as possible to
482 * get read access to the data.
483 */
484 if ((prot & VM_PROT_READ) == 0) {
485 mach_vm_protect(map, vmoffset, vmsize, FALSE,
486 prot|VM_PROT_READ);
487 }
488 /*
489 * Only actually perform write if we can read.
490 * Note: if we can't read, then we end up with
491 * a hole in the file.
492 */
493 if ((maxprot & VM_PROT_READ) == VM_PROT_READ
494 && vbr.user_tag != VM_MEMORY_IOKIT
495 && coredumpok(map,vmoffset)) {
496
497 error = vn_rdwr_64(UIO_WRITE, vp, vmoffset, vmsize, foffset,
498 (IS_64BIT_PROCESS(core_proc) ? UIO_USERSPACE64 : UIO_USERSPACE32),
499 IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int64_t *) 0, core_proc);
500
501 }
502
503 hoffset += segment_command_sz;
504 foffset += vmsize;
505 vmoffset += vmsize;
506 segment_count--;
507 }
508
509 /*
510 * If there are remaining segments which have not been written
511 * out because break in the loop above, then they were not counted
512 * because they exceed the real address space of the executable
513 * type: remove them from the header's count. This is OK, since
514 * we are allowed to have a sparse area following the segments.
515 */
516 if (is_64) {
517 mh64->ncmds -= segment_count;
518 mh64->sizeofcmds -= segment_count * segment_command_sz;
519 } else {
520 mh->ncmds -= segment_count;
521 mh->sizeofcmds -= segment_count * segment_command_sz;
522 }
523
524 tir1.header = header;
525 tir1.hoffset = hoffset;
526 tir1.flavors = flavors;
527 tir1.tstate_size = tstate_size;
528 task_act_iterate_wth_args(task, collectth_state,&tir1);
529
530 /*
531 * Write out the Mach header at the beginning of the
532 * file. OK to use a 32 bit write for this.
533 */
534 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
535 UIO_SYSSPACE, IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
536 kmem_free(kernel_map, header, header_size);
537
538 if ((coredump_flags & COREDUMP_FULLFSYNC) && error == 0)
539 error = VNOP_IOCTL(vp, F_FULLFSYNC, (caddr_t)NULL, 0, ctx);
540 out:
541 error1 = vnode_close(vp, FWRITE, ctx);
542 out2:
543 #if CONFIG_AUDIT
544 audit_proc_coredump(core_proc, name, error);
545 #endif
546 if (alloced_name != NULL)
547 FREE(alloced_name, M_TEMP);
548 if (error == 0)
549 error = error1;
550
551 return (error);
552 }
553
554 #else /* CONFIG_COREDUMP */
555
556 /* When core dumps aren't needed, no need to compile this file at all */
557
558 #error assertion failed: this section is not compiled
559
560 #endif /* CONFIG_COREDUMP */