]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_core.c
xnu-1228.9.59.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
A
65
66typedef struct {
67 int flavor; /* the number for this flavor */
2d21ac55 68 mach_msg_type_number_t count; /* count of ints in this flavor */
1c79356b
A
69} mythread_state_flavor_t;
70
71#if defined (__ppc__)
2d21ac55
A
72/* 64 bit */
73mythread_state_flavor_t thread_flavor_array64[]={
74 {PPC_THREAD_STATE64 , PPC_THREAD_STATE64_COUNT},
75 {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT},
76 {PPC_EXCEPTION_STATE64, PPC_EXCEPTION_STATE64_COUNT},
77 {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT}
78 };
1c79356b 79
2d21ac55 80/* 32 bit */
1c79356b
A
81mythread_state_flavor_t thread_flavor_array[]={
82 {PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT},
83 {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT},
55e303ae
A
84 {PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT},
85 {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT}
1c79356b 86 };
2d21ac55 87
1c79356b
A
88#elif defined (__i386__)
89mythread_state_flavor_t thread_flavor_array [] = {
0c530ab8
A
90 {x86_THREAD_STATE, x86_THREAD_STATE_COUNT},
91 {x86_FLOAT_STATE, x86_FLOAT_STATE_COUNT},
92 {x86_EXCEPTION_STATE, x86_EXCEPTION_STATE_COUNT},
1c79356b 93 };
0c530ab8 94int mynum_flavors=3;
1c79356b
A
95#else
96#error architecture not supported
97#endif
98
99
100typedef struct {
101 vm_offset_t header;
102 int hoffset;
103 mythread_state_flavor_t *flavors;
104 int tstate_size;
2d21ac55 105 int flavor_count;
1c79356b
A
106} tir_t;
107
e5568f75 108/* XXX should be static */
91447636 109void collectth_state(thread_t th_act, void *tirp);
e5568f75
A
110
111/* XXX not in a Mach header anywhere */
91447636 112kern_return_t thread_getstatus(register thread_t act, int flavor,
e5568f75 113 thread_state_t tstate, mach_msg_type_number_t *count);
91447636 114void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
e5568f75
A
115
116
593a1d5f
A
117#ifdef SECURE_KERNEL
118__private_extern__ int do_coredump = 0; /* default: don't dump cores */
119#else
91447636 120__private_extern__ int do_coredump = 1; /* default: dump cores */
593a1d5f 121#endif
91447636 122__private_extern__ int sugid_coredump = 0; /* default: but not SGUID binaries */
e5568f75 123
55e303ae 124void
91447636 125collectth_state(thread_t th_act, void *tirp)
1c79356b
A
126{
127 vm_offset_t header;
128 int hoffset, i ;
129 mythread_state_flavor_t *flavors;
130 struct thread_command *tc;
91447636
A
131 tir_t *t = (tir_t *)tirp;
132
1c79356b
A
133 /*
134 * Fill in thread command structure.
135 */
136 header = t->header;
137 hoffset = t->hoffset;
138 flavors = t->flavors;
139
140 tc = (struct thread_command *) (header + hoffset);
141 tc->cmd = LC_THREAD;
142 tc->cmdsize = sizeof(struct thread_command)
143 + t->tstate_size;
144 hoffset += sizeof(struct thread_command);
145 /*
146 * Follow with a struct thread_state_flavor and
147 * the appropriate thread state struct for each
148 * thread state flavor.
149 */
2d21ac55 150 for (i = 0; i < t->flavor_count; i++) {
1c79356b
A
151 *(mythread_state_flavor_t *)(header+hoffset) =
152 flavors[i];
153 hoffset += sizeof(mythread_state_flavor_t);
154 thread_getstatus(th_act, flavors[i].flavor,
e5568f75 155 (thread_state_t)(header+hoffset),
1c79356b
A
156 &flavors[i].count);
157 hoffset += flavors[i].count*sizeof(int);
158 }
159
160 t->hoffset = hoffset;
161}
e5568f75 162
2d21ac55 163
1c79356b 164/*
2d21ac55
A
165 * coredump
166 *
167 * Description: Create a core image on the file "core" for the process
168 * indicated
169 *
170 * Parameters: core_proc Process to dump core [*]
171 *
172 * Returns: 0 Success
173 * EFAULT Failed
174 *
175 * IMPORTANT: This function can only be called on the current process, due
176 * to assumptions below; see variable declaration section for
177 * details.
1c79356b
A
178 */
179#define MAX_TSTATE_FLAVORS 10
180int
2d21ac55 181coredump(proc_t core_proc)
1c79356b 182{
2d21ac55
A
183/* Begin assumptions that limit us to only the current process */
184 vfs_context_t ctx = vfs_context_current();
185 vm_map_t map = current_map();
186 task_t task = current_task();
187/* End assumptions */
188 kauth_cred_t cred = vfs_context_ucred(ctx);
189 int error = 0;
91447636 190 struct vnode_attr va;
1c79356b
A
191 int thread_count, segment_count;
192 int command_size, header_size, tstate_size;
91447636
A
193 int hoffset;
194 off_t foffset;
195 vm_map_offset_t vmoffset;
1c79356b 196 vm_offset_t header;
91447636 197 vm_map_size_t vmsize;
1c79356b
A
198 vm_prot_t prot;
199 vm_prot_t maxprot;
200 vm_inherit_t inherit;
2d21ac55
A
201 int error1 = 0;
202 char stack_name[MAXCOMLEN+6];
203 char *alloced_name = NULL;
e5568f75 204 char *name;
1c79356b 205 mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
e5568f75 206 vm_size_t mapsize;
1c79356b 207 int i;
2d21ac55 208 uint32_t nesting_depth = 0;
1c79356b
A
209 kern_return_t kret;
210 struct vm_region_submap_info_64 vbr;
2d21ac55 211 mach_msg_type_number_t vbrcount = 0;
1c79356b
A
212 tir_t tir1;
213 struct vnode * vp;
2d21ac55
A
214 struct mach_header *mh = NULL; /* protected by is_64 */
215 struct mach_header_64 *mh64 = NULL; /* protected by is_64 */
91447636
A
216 int is_64 = 0;
217 size_t mach_header_sz = sizeof(struct mach_header);
218 size_t segment_command_sz = sizeof(struct segment_command);
1c79356b 219
e5568f75
A
220 if (do_coredump == 0 || /* Not dumping at all */
221 ( (sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */
91447636
A
222 ( (cred->cr_svuid != cred->cr_ruid) ||
223 (cred->cr_svgid != cred->cr_rgid)))) {
e5568f75 224
1c79356b 225 return (EFAULT);
e5568f75 226 }
1c79356b 227
2d21ac55 228 if (IS_64BIT_PROCESS(core_proc)) {
91447636
A
229 is_64 = 1;
230 mach_header_sz = sizeof(struct mach_header_64);
231 segment_command_sz = sizeof(struct segment_command_64);
232 }
233
1c79356b
A
234 mapsize = get_vmmap_size(map);
235
2d21ac55 236 if (mapsize >= core_proc->p_rlimit[RLIMIT_CORE].rlim_cur)
1c79356b
A
237 return (EFAULT);
238 (void) task_suspend(task);
239
2d21ac55 240 MALLOC(alloced_name, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
e5568f75 241
2d21ac55 242 /* create name according to sysctl'able format string */
e5568f75 243 /* if name creation fails, fall back to historical behaviour... */
2d21ac55
A
244 if (proc_core_name(core_proc->p_comm, kauth_cred_getuid(cred),
245 core_proc->p_pid, alloced_name, MAXPATHLEN)) {
246 snprintf(stack_name, sizeof(stack_name),
247 "/cores/core.%d", core_proc->p_pid);
248 name = stack_name;
249 } else
250 name = alloced_name;
e5568f75 251
2d21ac55
A
252 if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, ctx)))
253 goto out2;
91447636
A
254
255 VATTR_INIT(&va);
256 VATTR_WANTED(&va, va_nlink);
1c79356b
A
257 /* Don't dump to non-regular files or files with links. */
258 if (vp->v_type != VREG ||
2d21ac55 259 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
1c79356b
A
260 error = EFAULT;
261 goto out;
262 }
263
91447636
A
264 VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */
265 VATTR_SET(&va, va_data_size, 0);
2d21ac55
A
266 vnode_setattr(vp, &va, ctx);
267 core_proc->p_acflag |= ACORE;
1c79356b
A
268
269 /*
270 * If the task is modified while dumping the file
271 * (e.g., changes in threads or VM, the resulting
272 * file will not necessarily be correct.
273 */
274
275 thread_count = get_task_numacts(task);
276 segment_count = get_vmmap_entries(map); /* XXX */
2d21ac55
A
277#if defined (__ppc__)
278 if (is_64) {
279 tir1.flavor_count = sizeof(thread_flavor_array64)/sizeof(mythread_state_flavor_t);
280 bcopy(thread_flavor_array64, flavors,sizeof(thread_flavor_array64));
281 } else {
282#endif /* __ppc __ */
283 tir1.flavor_count = sizeof(thread_flavor_array)/sizeof(mythread_state_flavor_t);
284 bcopy(thread_flavor_array, flavors,sizeof(thread_flavor_array));
285#if defined (__ppc__)
286 }
287#endif /* __ppc __ */
1c79356b 288 tstate_size = 0;
2d21ac55 289 for (i = 0; i < tir1.flavor_count; i++)
1c79356b
A
290 tstate_size += sizeof(mythread_state_flavor_t) +
291 (flavors[i].count * sizeof(int));
91447636 292 command_size = segment_count * segment_command_sz +
1c79356b
A
293 thread_count*sizeof(struct thread_command) +
294 tstate_size*thread_count;
295
91447636 296 header_size = command_size + mach_header_sz;
1c79356b 297
91447636 298 (void) kmem_alloc(kernel_map,
1c79356b
A
299 (vm_offset_t *)&header,
300 (vm_size_t)header_size);
301
302 /*
303 * Set up Mach-O header.
304 */
91447636
A
305 if (is_64) {
306 mh64 = (struct mach_header_64 *)header;
307 mh64->magic = MH_MAGIC_64;
308 mh64->cputype = cpu_type();
309 mh64->cpusubtype = cpu_subtype();
310 mh64->filetype = MH_CORE;
311 mh64->ncmds = segment_count + thread_count;
312 mh64->sizeofcmds = command_size;
313 mh64->reserved = 0; /* 8 byte alignment */
314 } else {
315 mh = (struct mach_header *)header;
316 mh->magic = MH_MAGIC;
317 mh->cputype = cpu_type();
318 mh->cpusubtype = cpu_subtype();
319 mh->filetype = MH_CORE;
320 mh->ncmds = segment_count + thread_count;
321 mh->sizeofcmds = command_size;
322 }
323
324 hoffset = mach_header_sz; /* offset into header */
325 foffset = round_page(header_size); /* offset into file */
326 vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */
327
55e303ae
A
328 /*
329 * We use to check for an error, here, now we try and get
1c79356b
A
330 * as much as we can
331 */
91447636
A
332 while (segment_count > 0) {
333 struct segment_command *sc;
334 struct segment_command_64 *sc64;
335
1c79356b
A
336 /*
337 * Get region information for next region.
338 */
339
340 while (1) {
341 vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
91447636
A
342 if((kret = mach_vm_region_recurse(map,
343 &vmoffset, &vmsize, &nesting_depth,
344 (vm_region_recurse_info_t)&vbr,
345 &vbrcount)) != KERN_SUCCESS) {
346 break;
347 }
348 /*
349 * If we get a valid mapping back, but we're dumping
350 * a 32 bit process, and it's over the allowable
351 * address space of a 32 bit process, it's the same
352 * as if mach_vm_region_recurse() failed.
353 */
354 if (!(is_64) &&
355 (vmoffset + vmsize > VM_MAX_ADDRESS)) {
356 kret = KERN_INVALID_ADDRESS;
1c79356b
A
357 break;
358 }
359 if(vbr.is_submap) {
360 nesting_depth++;
361 continue;
362 } else {
363 break;
364 }
365 }
366 if(kret != KERN_SUCCESS)
367 break;
368
369 prot = vbr.protection;
370 maxprot = vbr.max_protection;
371 inherit = vbr.inheritance;
372 /*
373 * Fill in segment command structure.
374 */
91447636
A
375 if (is_64) {
376 sc64 = (struct segment_command_64 *)(header + hoffset);
377 sc64->cmd = LC_SEGMENT_64;
378 sc64->cmdsize = sizeof(struct segment_command_64);
379 /* segment name is zeroed by kmem_alloc */
380 sc64->segname[0] = 0;
381 sc64->vmaddr = vmoffset;
382 sc64->vmsize = vmsize;
383 sc64->fileoff = foffset;
384 sc64->filesize = vmsize;
385 sc64->maxprot = maxprot;
386 sc64->initprot = prot;
387 sc64->nsects = 0;
388 } else {
389 sc = (struct segment_command *) (header + hoffset);
390 sc->cmd = LC_SEGMENT;
391 sc->cmdsize = sizeof(struct segment_command);
392 /* segment name is zeroed by kmem_alloc */
393 sc->segname[0] = 0;
394 sc->vmaddr = CAST_DOWN(vm_offset_t,vmoffset);
395 sc->vmsize = CAST_DOWN(vm_size_t,vmsize);
396 sc->fileoff = CAST_DOWN(uint32_t,foffset);
397 sc->filesize = CAST_DOWN(uint32_t,vmsize);
398 sc->maxprot = maxprot;
399 sc->initprot = prot;
400 sc->nsects = 0;
401 }
1c79356b
A
402
403 /*
404 * Write segment out. Try as hard as possible to
405 * get read access to the data.
406 */
407 if ((prot & VM_PROT_READ) == 0) {
91447636
A
408 mach_vm_protect(map, vmoffset, vmsize, FALSE,
409 prot|VM_PROT_READ);
1c79356b
A
410 }
411 /*
412 * Only actually perform write if we can read.
413 * Note: if we can't read, then we end up with
414 * a hole in the file.
415 */
55e303ae
A
416 if ((maxprot & VM_PROT_READ) == VM_PROT_READ
417 && vbr.user_tag != VM_MEMORY_IOKIT
418 && coredumpok(map,vmoffset)) {
91447636
A
419 vm_map_size_t tmp_vmsize = vmsize;
420 off_t xfer_foffset = foffset;
421
422 //LP64todo - works around vn_rdwr_64() 2G limit
423 while (tmp_vmsize > 0) {
424 vm_map_size_t xfer_vmsize = tmp_vmsize;
425 if (xfer_vmsize > INT_MAX)
426 xfer_vmsize = INT_MAX;
427 error = vn_rdwr_64(UIO_WRITE, vp,
428 vmoffset, xfer_vmsize, xfer_foffset,
2d21ac55
A
429 (IS_64BIT_PROCESS(core_proc) ? UIO_USERSPACE64 : UIO_USERSPACE32),
430 IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
91447636
A
431 tmp_vmsize -= xfer_vmsize;
432 xfer_foffset += xfer_vmsize;
433 }
1c79356b
A
434 }
435
91447636
A
436 hoffset += segment_command_sz;
437 foffset += vmsize;
438 vmoffset += vmsize;
1c79356b
A
439 segment_count--;
440 }
441
91447636
A
442 /*
443 * If there are remaining segments which have not been written
444 * out because break in the loop above, then they were not counted
445 * because they exceed the real address space of the executable
446 * type: remove them from the header's count. This is OK, since
447 * we are allowed to have a sparse area following the segments.
448 */
449 if (is_64) {
450 mh64->ncmds -= segment_count;
2d21ac55 451 mh64->sizeofcmds -= segment_count * segment_command_sz;
91447636
A
452 } else {
453 mh->ncmds -= segment_count;
2d21ac55 454 mh->sizeofcmds -= segment_count * segment_command_sz;
91447636
A
455 }
456
1c79356b
A
457 tir1.header = header;
458 tir1.hoffset = hoffset;
459 tir1.flavors = flavors;
460 tir1.tstate_size = tstate_size;
461 task_act_iterate_wth_args(task, collectth_state,&tir1);
462
1c79356b
A
463 /*
464 * Write out the Mach header at the beginning of the
91447636 465 * file. OK to use a 32 bit write for this.
1c79356b
A
466 */
467 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
2d21ac55 468 UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
1c79356b
A
469 kmem_free(kernel_map, header, header_size);
470out:
2d21ac55
A
471 error1 = vnode_close(vp, FWRITE, ctx);
472out2:
473 if (alloced_name != NULL)
474 FREE(alloced_name, M_TEMP);
1c79356b
A
475 if (error == 0)
476 error = error1;
91447636 477
55e303ae 478 return (error);
1c79356b 479}