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