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 int flavor
; /* the number for this flavor */
75 mach_msg_type_number_t count
; /* count of ints in this flavor */
76 } mythread_state_flavor_t
;
78 #if defined (__i386__) || defined (__x86_64__)
79 mythread_state_flavor_t thread_flavor_array
[] = {
80 {x86_THREAD_STATE
, x86_THREAD_STATE_COUNT
},
81 {x86_FLOAT_STATE
, x86_FLOAT_STATE_COUNT
},
82 {x86_EXCEPTION_STATE
, x86_EXCEPTION_STATE_COUNT
},
84 int mynum_flavors
= 3;
85 #elif defined (__arm__)
86 mythread_state_flavor_t thread_flavor_array
[] = {
87 {ARM_THREAD_STATE
, ARM_THREAD_STATE_COUNT
},
88 {ARM_VFP_STATE
, ARM_VFP_STATE_COUNT
},
89 {ARM_EXCEPTION_STATE
, ARM_EXCEPTION_STATE_COUNT
}
91 int mynum_flavors
= 3;
93 #elif defined (__arm64__)
94 mythread_state_flavor_t thread_flavor_array
[] = {
95 {ARM_THREAD_STATE64
, ARM_THREAD_STATE64_COUNT
},
97 {ARM_EXCEPTION_STATE64
, ARM_EXCEPTION_STATE64_COUNT
}
99 int mynum_flavors
= 2;
101 #error architecture not supported
108 mythread_state_flavor_t
*flavors
;
113 extern int freespace_mb(vnode_t vp
);
115 /* XXX not in a Mach header anywhere */
116 kern_return_t
thread_getstatus(thread_t act
, int flavor
,
117 thread_state_t tstate
, mach_msg_type_number_t
*count
);
118 void task_act_iterate_wth_args(task_t
, void (*)(thread_t
, void *), void *);
121 __XNU_PRIVATE_EXTERN
int do_coredump
= 0; /* default: don't dump cores */
123 __XNU_PRIVATE_EXTERN
int do_coredump
= 1; /* default: dump cores */
125 __XNU_PRIVATE_EXTERN
int sugid_coredump
= 0; /* default: but not SGUID binaries */
128 /* cpu_type returns only the most generic indication of the current CPU. */
129 /* in a core we want to know the kind of process. */
132 process_cpu_type(proc_t core_proc
)
134 cpu_type_t what_we_think
;
135 #if defined (__i386__) || defined (__x86_64__)
136 if (IS_64BIT_PROCESS(core_proc
)) {
137 what_we_think
= CPU_TYPE_X86_64
;
139 what_we_think
= CPU_TYPE_I386
;
141 #elif defined (__arm__) || defined(__arm64__)
142 if (IS_64BIT_PROCESS(core_proc
)) {
143 what_we_think
= CPU_TYPE_ARM64
;
145 what_we_think
= CPU_TYPE_ARM
;
149 return what_we_think
;
153 process_cpu_subtype(proc_t core_proc
)
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
;
160 what_we_think
= CPU_SUBTYPE_I386_ALL
;
162 #elif defined (__arm__) || defined(__arm64__)
163 if (IS_64BIT_PROCESS(core_proc
)) {
164 what_we_think
= CPU_SUBTYPE_ARM64_ALL
;
166 what_we_think
= CPU_SUBTYPE_ARM_ALL
;
169 return what_we_think
;
173 collectth_state(thread_t th_act
, void *tirp
)
177 mythread_state_flavor_t
*flavors
;
178 struct thread_command
*tc
;
179 tir_t
*t
= (tir_t
*)tirp
;
182 * Fill in thread command structure.
185 hoffset
= t
->hoffset
;
186 flavors
= t
->flavors
;
188 tc
= (struct thread_command
*) (header
+ hoffset
);
190 tc
->cmdsize
= sizeof(struct thread_command
)
192 hoffset
+= sizeof(struct thread_command
);
194 * Follow with a struct thread_state_flavor and
195 * the appropriate thread state struct for each
196 * thread state flavor.
198 for (i
= 0; i
< t
->flavor_count
; i
++) {
199 *(mythread_state_flavor_t
*)(header
+ hoffset
) =
201 hoffset
+= sizeof(mythread_state_flavor_t
);
202 thread_getstatus(th_act
, flavors
[i
].flavor
,
203 (thread_state_t
)(header
+ hoffset
),
205 hoffset
+= flavors
[i
].count
* sizeof(int);
208 t
->hoffset
= hoffset
;
214 * Description: Create a core image on the file "core" for the process
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)
225 * IMPORTANT: This function can only be called on the current process, due
226 * to assumptions below; see variable declaration section for
229 #define MAX_TSTATE_FLAVORS 10
231 coredump(proc_t core_proc
, uint32_t reserve_mb
, int coredump_flags
)
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
);
240 struct vnode_attr va
;
241 int thread_count
, segment_count
;
242 int command_size
, header_size
, tstate_size
;
245 mach_vm_offset_t vmoffset
;
247 mach_vm_size_t vmsize
;
250 vm_inherit_t inherit
;
252 char stack_name
[MAXCOMLEN
+ 6];
253 char *alloced_name
= NULL
;
255 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
258 uint32_t nesting_depth
= 0;
260 struct vm_region_submap_info_64 vbr
;
261 mach_msg_type_number_t vbrcount
= 0;
264 struct mach_header
*mh
= NULL
; /* protected by is_64 */
265 struct mach_header_64
*mh64
= NULL
; /* protected by is_64 */
267 size_t mach_header_sz
= sizeof(struct mach_header
);
268 size_t segment_command_sz
= sizeof(struct segment_command
);
270 if (current_proc() != core_proc
) {
271 panic("coredump() called against proc that is not current_proc: %p", core_proc
);
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
))))) {
283 error
= mac_proc_check_dump_core(core_proc
);
289 if (IS_64BIT_PROCESS(core_proc
)) {
291 mach_header_sz
= sizeof(struct mach_header_64
);
292 segment_command_sz
= sizeof(struct segment_command_64
);
295 mapsize
= get_vmmap_size(map
);
297 if (((coredump_flags
& COREDUMP_IGNORE_ULIMIT
) == 0) &&
298 (mapsize
>= core_proc
->p_rlimit
[RLIMIT_CORE
].rlim_cur
)) {
303 (void) task_suspend_internal(task
);
305 MALLOC(alloced_name
, char *, MAXPATHLEN
, M_TEMP
, M_NOWAIT
| M_ZERO
);
307 /* create name according to sysctl'able format string */
308 /* if name creation fails, fall back to historical behaviour... */
309 if (alloced_name
== NULL
||
310 proc_core_name(core_proc
->p_comm
, kauth_cred_getuid(cred
),
311 core_proc
->p_pid
, alloced_name
, MAXPATHLEN
)) {
312 snprintf(stack_name
, sizeof(stack_name
),
313 "/cores/core.%d", core_proc
->p_pid
);
319 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), S_IRUSR
, VNODE_LOOKUP_NOFOLLOW
, &vp
, ctx
))) {
324 VATTR_WANTED(&va
, va_nlink
);
325 /* Don't dump to non-regular files or files with links. */
326 if (vp
->v_type
!= VREG
||
327 vnode_getattr(vp
, &va
, ctx
) || va
.va_nlink
!= 1) {
332 VATTR_INIT(&va
); /* better to do it here than waste more stack in vnode_setsize */
333 VATTR_SET(&va
, va_data_size
, 0);
334 if (core_proc
== initproc
) {
335 VATTR_SET(&va
, va_dataprotect_class
, PROTECTION_CLASS_D
);
337 vnode_setattr(vp
, &va
, ctx
);
338 core_proc
->p_acflag
|= ACORE
;
340 if ((reserve_mb
> 0) &&
341 ((freespace_mb(vp
) - (mapsize
>> 20)) < reserve_mb
)) {
347 * If the task is modified while dumping the file
348 * (e.g., changes in threads or VM, the resulting
349 * file will not necessarily be correct.
352 thread_count
= get_task_numacts(task
);
353 segment_count
= get_vmmap_entries(map
); /* XXX */
354 tir1
.flavor_count
= sizeof(thread_flavor_array
) / sizeof(mythread_state_flavor_t
);
355 bcopy(thread_flavor_array
, flavors
, sizeof(thread_flavor_array
));
357 for (i
= 0; i
< tir1
.flavor_count
; i
++) {
358 tstate_size
+= sizeof(mythread_state_flavor_t
) +
359 (flavors
[i
].count
* sizeof(int));
361 command_size
= segment_count
* segment_command_sz
+
362 thread_count
* sizeof(struct thread_command
) +
363 tstate_size
* thread_count
;
365 header_size
= command_size
+ mach_header_sz
;
367 if (kmem_alloc(kernel_map
, &header
, (vm_size_t
)header_size
, VM_KERN_MEMORY_DIAG
) != KERN_SUCCESS
) {
373 * Set up Mach-O header.
376 mh64
= (struct mach_header_64
*)header
;
377 mh64
->magic
= MH_MAGIC_64
;
378 mh64
->cputype
= process_cpu_type(core_proc
);
379 mh64
->cpusubtype
= process_cpu_subtype(core_proc
);
380 mh64
->filetype
= MH_CORE
;
381 mh64
->ncmds
= segment_count
+ thread_count
;
382 mh64
->sizeofcmds
= command_size
;
383 mh64
->reserved
= 0; /* 8 byte alignment */
385 mh
= (struct mach_header
*)header
;
386 mh
->magic
= MH_MAGIC
;
387 mh
->cputype
= process_cpu_type(core_proc
);
388 mh
->cpusubtype
= process_cpu_subtype(core_proc
);
389 mh
->filetype
= MH_CORE
;
390 mh
->ncmds
= segment_count
+ thread_count
;
391 mh
->sizeofcmds
= command_size
;
394 hoffset
= mach_header_sz
; /* offset into header */
395 foffset
= round_page(header_size
); /* offset into file */
396 vmoffset
= MACH_VM_MIN_ADDRESS
; /* offset into VM */
399 * We use to check for an error, here, now we try and get
402 while (segment_count
> 0) {
403 struct segment_command
*sc
;
404 struct segment_command_64
*sc64
;
407 * Get region information for next region.
411 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
412 if ((kret
= mach_vm_region_recurse(map
,
413 &vmoffset
, &vmsize
, &nesting_depth
,
414 (vm_region_recurse_info_t
)&vbr
,
415 &vbrcount
)) != KERN_SUCCESS
) {
419 * If we get a valid mapping back, but we're dumping
420 * a 32 bit process, and it's over the allowable
421 * address space of a 32 bit process, it's the same
422 * as if mach_vm_region_recurse() failed.
425 (vmoffset
+ vmsize
> VM_MAX_ADDRESS
)) {
426 kret
= KERN_INVALID_ADDRESS
;
436 if (kret
!= KERN_SUCCESS
) {
440 prot
= vbr
.protection
;
441 maxprot
= vbr
.max_protection
;
442 inherit
= vbr
.inheritance
;
444 * Fill in segment command structure.
447 sc64
= (struct segment_command_64
*)(header
+ hoffset
);
448 sc64
->cmd
= LC_SEGMENT_64
;
449 sc64
->cmdsize
= sizeof(struct segment_command_64
);
450 /* segment name is zeroed by kmem_alloc */
451 sc64
->segname
[0] = 0;
452 sc64
->vmaddr
= vmoffset
;
453 sc64
->vmsize
= vmsize
;
454 sc64
->fileoff
= foffset
;
455 sc64
->filesize
= vmsize
;
456 sc64
->maxprot
= maxprot
;
457 sc64
->initprot
= prot
;
461 sc
= (struct segment_command
*) (header
+ hoffset
);
462 sc
->cmd
= LC_SEGMENT
;
463 sc
->cmdsize
= sizeof(struct segment_command
);
464 /* segment name is zeroed by kmem_alloc */
466 sc
->vmaddr
= CAST_DOWN_EXPLICIT(vm_offset_t
, vmoffset
);
467 sc
->vmsize
= CAST_DOWN_EXPLICIT(vm_size_t
, vmsize
);
468 sc
->fileoff
= CAST_DOWN_EXPLICIT(uint32_t, foffset
); /* will never truncate */
469 sc
->filesize
= CAST_DOWN_EXPLICIT(uint32_t, vmsize
); /* will never truncate */
470 sc
->maxprot
= maxprot
;
477 * Write segment out. Try as hard as possible to
478 * get read access to the data.
480 if ((prot
& VM_PROT_READ
) == 0) {
481 mach_vm_protect(map
, vmoffset
, vmsize
, FALSE
,
482 prot
| VM_PROT_READ
);
485 * Only actually perform write if we can read.
486 * Note: if we can't read, then we end up with
487 * a hole in the file.
489 if ((maxprot
& VM_PROT_READ
) == VM_PROT_READ
490 && vbr
.user_tag
!= VM_MEMORY_IOKIT
491 && coredumpok(map
, vmoffset
)) {
492 error
= vn_rdwr_64(UIO_WRITE
, vp
, vmoffset
, vmsize
, foffset
,
493 (IS_64BIT_PROCESS(core_proc
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
494 IO_NOCACHE
| IO_NODELOCKED
| IO_UNIT
, cred
, (int64_t *) 0, core_proc
);
497 hoffset
+= segment_command_sz
;
504 * If there are remaining segments which have not been written
505 * out because break in the loop above, then they were not counted
506 * because they exceed the real address space of the executable
507 * type: remove them from the header's count. This is OK, since
508 * we are allowed to have a sparse area following the segments.
511 mh64
->ncmds
-= segment_count
;
512 mh64
->sizeofcmds
-= segment_count
* segment_command_sz
;
514 mh
->ncmds
-= segment_count
;
515 mh
->sizeofcmds
-= segment_count
* segment_command_sz
;
518 tir1
.header
= header
;
519 tir1
.hoffset
= hoffset
;
520 tir1
.flavors
= flavors
;
521 tir1
.tstate_size
= tstate_size
;
522 task_act_iterate_wth_args(task
, collectth_state
, &tir1
);
525 * Write out the Mach header at the beginning of the
526 * file. OK to use a 32 bit write for this.
528 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)header
, header_size
, (off_t
)0,
529 UIO_SYSSPACE
, IO_NOCACHE
| IO_NODELOCKED
| IO_UNIT
, cred
, (int *) 0, core_proc
);
530 kmem_free(kernel_map
, header
, header_size
);
532 if ((coredump_flags
& COREDUMP_FULLFSYNC
) && error
== 0) {
533 error
= VNOP_IOCTL(vp
, F_FULLFSYNC
, (caddr_t
)NULL
, 0, ctx
);
536 error1
= vnode_close(vp
, FWRITE
, ctx
);
539 audit_proc_coredump(core_proc
, name
, error
);
541 if (alloced_name
!= NULL
) {
542 FREE(alloced_name
, M_TEMP
);
551 #else /* CONFIG_COREDUMP */
553 /* When core dumps aren't needed, no need to compile this file at all */
555 #error assertion failed: this section is not compiled
557 #endif /* CONFIG_COREDUMP */