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