2 * Copyright (c) 2000-2006 Apple Computer, 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@
28 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
30 * File: bsd/kern/kern_core.c
32 * This file contains machine independent code for performing core dumps.
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>
51 #include <sys/file_internal.h>
53 #include <sys/kernel.h>
56 #include <mach-o/loader.h>
57 #include <mach/vm_region.h>
58 #include <mach/vm_statistics.h>
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() */
67 #include <security/audit/audit.h>
70 #include <security/mac_framework.h>
71 #endif /* CONFIG_MACF */
74 #include <sys/codesign.h>
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
;
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
},
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
}
96 int mynum_flavors
= 3;
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
}
104 int mynum_flavors
= 2;
106 #error architecture not supported
113 mythread_state_flavor_t
*flavors
;
118 extern int freespace_mb(vnode_t vp
);
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 *);
126 __XNU_PRIVATE_EXTERN
int do_coredump
= 0; /* default: don't dump cores */
128 __XNU_PRIVATE_EXTERN
int do_coredump
= 1; /* default: dump cores */
130 __XNU_PRIVATE_EXTERN
int sugid_coredump
= 0; /* default: but not SGUID binaries */
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. */
137 process_cpu_type(proc_t core_proc
)
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
;
144 what_we_think
= CPU_TYPE_I386
;
146 #elif defined (__arm__) || defined(__arm64__)
147 if (IS_64BIT_PROCESS(core_proc
)) {
148 what_we_think
= CPU_TYPE_ARM64
;
150 what_we_think
= CPU_TYPE_ARM
;
154 return what_we_think
;
158 process_cpu_subtype(proc_t core_proc
)
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
;
165 what_we_think
= CPU_SUBTYPE_I386_ALL
;
167 #elif defined (__arm__) || defined(__arm64__)
168 if (IS_64BIT_PROCESS(core_proc
)) {
169 what_we_think
= CPU_SUBTYPE_ARM64_ALL
;
171 what_we_think
= CPU_SUBTYPE_ARM_ALL
;
174 return what_we_think
;
178 collectth_state(thread_t th_act
, void *tirp
)
182 mythread_state_flavor_t
*flavors
;
183 struct thread_command
*tc
;
184 tir_t
*t
= (tir_t
*)tirp
;
187 * Fill in thread command structure.
190 hoffset
= t
->hoffset
;
191 flavors
= t
->flavors
;
193 tc
= (struct thread_command
*) (header
+ hoffset
);
195 tc
->cmdsize
= sizeof(struct thread_command
)
197 hoffset
+= sizeof(struct thread_command
);
199 * Follow with a struct thread_state_flavor and
200 * the appropriate thread state struct for each
201 * thread state flavor.
203 for (i
= 0; i
< t
->flavor_count
; i
++) {
204 *(mythread_state_flavor_t
*)(header
+ hoffset
) =
206 hoffset
+= sizeof(mythread_state_flavor_t
);
207 thread_getstatus(th_act
, flavors
[i
].flavor
,
208 (thread_state_t
)(header
+ hoffset
),
210 hoffset
+= flavors
[i
].count
* sizeof(int);
213 t
->hoffset
= hoffset
;
219 * Description: Create a core image on the file "core" for the process
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)
230 * IMPORTANT: This function can only be called on the current process, due
231 * to assumptions below; see variable declaration section for
234 #define MAX_TSTATE_FLAVORS 10
236 coredump(proc_t core_proc
, uint32_t reserve_mb
, int coredump_flags
)
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
);
245 struct vnode_attr va
;
246 int thread_count
, segment_count
;
247 int command_size
, header_size
, tstate_size
;
250 mach_vm_offset_t vmoffset
;
252 mach_vm_size_t vmsize
;
255 vm_inherit_t inherit
;
257 char stack_name
[MAXCOMLEN
+ 6];
258 char *alloced_name
= NULL
;
260 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
263 uint32_t nesting_depth
= 0;
265 struct vm_region_submap_info_64 vbr
;
266 mach_msg_type_number_t vbrcount
= 0;
269 struct mach_header
*mh
= NULL
; /* protected by is_64 */
270 struct mach_header_64
*mh64
= NULL
; /* protected by is_64 */
272 size_t mach_header_sz
= sizeof(struct mach_header
);
273 size_t segment_command_sz
= sizeof(struct segment_command
);
275 if (current_proc() != core_proc
) {
276 panic("coredump() called against proc that is not current_proc: %p", core_proc
);
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
))))) {
288 error
= mac_proc_check_dump_core(core_proc
);
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
)) {
306 if (IS_64BIT_PROCESS(core_proc
)) {
308 mach_header_sz
= sizeof(struct mach_header_64
);
309 segment_command_sz
= sizeof(struct segment_command_64
);
312 mapsize
= get_vmmap_size(map
);
314 if (((coredump_flags
& COREDUMP_IGNORE_ULIMIT
) == 0) &&
315 (mapsize
>= core_proc
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)) {
320 (void) task_suspend_internal(task
);
322 MALLOC(alloced_name
, char *, MAXPATHLEN
, M_TEMP
, M_NOWAIT
| M_ZERO
);
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
);
336 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), S_IRUSR
, VNODE_LOOKUP_NOFOLLOW
, &vp
, ctx
))) {
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) {
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
);
354 vnode_setattr(vp
, &va
, ctx
);
355 core_proc
->p_acflag
|= ACORE
;
357 if ((reserve_mb
> 0) &&
358 ((freespace_mb(vp
) - (mapsize
>> 20)) < reserve_mb
)) {
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.
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
));
374 for (i
= 0; i
< tir1
.flavor_count
; i
++) {
375 tstate_size
+= sizeof(mythread_state_flavor_t
) +
376 (flavors
[i
].count
* sizeof(int));
378 command_size
= segment_count
* segment_command_sz
+
379 thread_count
* sizeof(struct thread_command
) +
380 tstate_size
* thread_count
;
382 header_size
= command_size
+ mach_header_sz
;
384 if (kmem_alloc(kernel_map
, &header
, (vm_size_t
)header_size
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
390 * Set up Mach-O header.
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 */
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
;
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 */
416 * We use to check for an error, here, now we try and get
419 while (segment_count
> 0) {
420 struct segment_command
*sc
;
421 struct segment_command_64
*sc64
;
424 * Get region information for next region.
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
) {
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.
442 (vmoffset
+ vmsize
> VM_MAX_ADDRESS
)) {
443 kret
= KERN_INVALID_ADDRESS
;
453 if (kret
!= KERN_SUCCESS
) {
457 prot
= vbr
.protection
;
458 maxprot
= vbr
.max_protection
;
459 inherit
= vbr
.inheritance
;
461 * Fill in segment command structure.
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
;
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 */
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
;
494 * Write segment out. Try as hard as possible to
495 * get read access to the data.
497 if ((prot
& VM_PROT_READ
) == 0) {
498 mach_vm_protect(map
, vmoffset
, vmsize
, FALSE
,
499 prot
| VM_PROT_READ
);
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.
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
);
514 hoffset
+= segment_command_sz
;
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.
528 mh64
->ncmds
-= segment_count
;
529 mh64
->sizeofcmds
-= segment_count
* segment_command_sz
;
531 mh
->ncmds
-= segment_count
;
532 mh
->sizeofcmds
-= segment_count
* segment_command_sz
;
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
);
542 * Write out the Mach header at the beginning of the
543 * file. OK to use a 32 bit write for this.
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
);
549 if ((coredump_flags
& COREDUMP_FULLFSYNC
) && error
== 0) {
550 error
= VNOP_IOCTL(vp
, F_FULLFSYNC
, (caddr_t
)NULL
, 0, ctx
);
553 error1
= vnode_close(vp
, FWRITE
, ctx
);
556 audit_proc_coredump(core_proc
, name
, error
);
558 if (alloced_name
!= NULL
) {
559 FREE(alloced_name
, M_TEMP
);
568 #else /* CONFIG_COREDUMP */
570 /* When core dumps aren't needed, no need to compile this file at all */
572 #error assertion failed: this section is not compiled
574 #endif /* CONFIG_COREDUMP */