]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_core.c
088cb7cdc8ac66d9df20ee382f4f68d45e65b90c
[apple/xnu.git] / bsd / kern / kern_core.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
31 *
32 * File: bsd/kern/kern_core.c
33 *
34 * This file contains machine independent code for performing core dumps.
35 *
36 */
37
38 #include <mach/vm_param.h>
39 #include <mach/thread_status.h>
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/signalvar.h>
44 #include <sys/resourcevar.h>
45 #include <sys/namei.h>
46 #include <sys/vnode_internal.h>
47 #include <sys/proc_internal.h>
48 #include <sys/kauth.h>
49 #include <sys/timeb.h>
50 #include <sys/times.h>
51 #include <sys/acct.h>
52 #include <sys/file_internal.h>
53 #include <sys/uio.h>
54 #include <sys/kernel.h>
55 #include <sys/stat.h>
56
57 #include <mach-o/loader.h>
58 #include <mach/vm_region.h>
59 #include <mach/vm_statistics.h>
60
61 #include <vm/vm_kern.h>
62 #include <vm/vm_protos.h> /* last */
63 #include <vm/vm_map.h> /* current_map() */
64 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
65 #include <mach/task.h> /* task_suspend() */
66 #include <kern/task.h> /* get_task_numacts() */
67
68 typedef struct {
69 int flavor; /* the number for this flavor */
70 int count; /* count of ints in this flavor */
71 } mythread_state_flavor_t;
72
73 #if defined (__ppc__)
74
75 mythread_state_flavor_t thread_flavor_array[]={
76 {PPC_THREAD_STATE , PPC_THREAD_STATE_COUNT},
77 {PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT},
78 {PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT},
79 {PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT}
80 };
81 int mynum_flavors=4;
82 #elif defined (__i386__)
83 mythread_state_flavor_t thread_flavor_array [] = {
84 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
85 {i386_THREAD_FPSTATE, i386_THREAD_FPSTATE_COUNT},
86 {i386_THREAD_EXCEPTSTATE, i386_THREAD_EXCEPTSTATE_COUNT},
87 {i386_THREAD_CTHREADSTATE, i386_THREAD_CTHREADSTATE_COUNT},
88 {i386_NEW_THREAD_STATE, i386_NEW_THREAD_STATE_COUNT},
89 {i386_FLOAT_STATE, i386_FLOAT_STATE_COUNT},
90 {i386_ISA_PORT_MAP_STATE, i386_ISA_PORT_MAP_STATE_COUNT},
91 {i386_V86_ASSIST_STATE, i386_V86_ASSIST_STATE_COUNT},
92 {THREAD_SYSCALL_STATE, i386_THREAD_SYSCALL_STATE_COUNT}
93 };
94 int mynum_flavors=9;
95
96 #else
97 #error architecture not supported
98 #endif
99
100
101 typedef struct {
102 vm_offset_t header;
103 int hoffset;
104 mythread_state_flavor_t *flavors;
105 int tstate_size;
106 } tir_t;
107
108 /* XXX should be static */
109 void collectth_state(thread_t th_act, void *tirp);
110
111 /* XXX not in a Mach header anywhere */
112 kern_return_t thread_getstatus(register thread_t act, int flavor,
113 thread_state_t tstate, mach_msg_type_number_t *count);
114 void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
115
116
117 __private_extern__ int do_coredump = 1; /* default: dump cores */
118 __private_extern__ int sugid_coredump = 0; /* default: but not SGUID binaries */
119
120 void
121 collectth_state(thread_t th_act, void *tirp)
122 {
123 vm_offset_t header;
124 int hoffset, i ;
125 mythread_state_flavor_t *flavors;
126 struct thread_command *tc;
127 tir_t *t = (tir_t *)tirp;
128
129 /*
130 * Fill in thread command structure.
131 */
132 header = t->header;
133 hoffset = t->hoffset;
134 flavors = t->flavors;
135
136 tc = (struct thread_command *) (header + hoffset);
137 tc->cmd = LC_THREAD;
138 tc->cmdsize = sizeof(struct thread_command)
139 + t->tstate_size;
140 hoffset += sizeof(struct thread_command);
141 /*
142 * Follow with a struct thread_state_flavor and
143 * the appropriate thread state struct for each
144 * thread state flavor.
145 */
146 for (i = 0; i < mynum_flavors; i++) {
147 *(mythread_state_flavor_t *)(header+hoffset) =
148 flavors[i];
149 hoffset += sizeof(mythread_state_flavor_t);
150 thread_getstatus(th_act, flavors[i].flavor,
151 (thread_state_t)(header+hoffset),
152 &flavors[i].count);
153 hoffset += flavors[i].count*sizeof(int);
154 }
155
156 t->hoffset = hoffset;
157 }
158
159 /*
160 * Create a core image on the file "core".
161 */
162 #define MAX_TSTATE_FLAVORS 10
163 int
164 coredump(struct proc *p)
165 {
166 int error=0;
167 kauth_cred_t cred = kauth_cred_get();
168 struct vnode_attr va;
169 struct vfs_context context;
170 vm_map_t map;
171 int thread_count, segment_count;
172 int command_size, header_size, tstate_size;
173 int hoffset;
174 off_t foffset;
175 vm_map_offset_t vmoffset;
176 vm_offset_t header;
177 vm_map_size_t vmsize;
178 vm_prot_t prot;
179 vm_prot_t maxprot;
180 vm_inherit_t inherit;
181 int error1;
182 task_t task;
183 char core_name[MAXCOMLEN+6];
184 char *name;
185 mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
186 vm_size_t mapsize;
187 int i;
188 int nesting_depth = 0;
189 kern_return_t kret;
190 struct vm_region_submap_info_64 vbr;
191 int vbrcount=0;
192 tir_t tir1;
193 struct vnode * vp;
194 struct mach_header *mh;
195 struct mach_header_64 *mh64;
196 int is_64 = 0;
197 size_t mach_header_sz = sizeof(struct mach_header);
198 size_t segment_command_sz = sizeof(struct segment_command);
199
200 if (do_coredump == 0 || /* Not dumping at all */
201 ( (sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */
202 ( (cred->cr_svuid != cred->cr_ruid) ||
203 (cred->cr_svgid != cred->cr_rgid)))) {
204
205 return (EFAULT);
206 }
207
208 if (IS_64BIT_PROCESS(p)) {
209 is_64 = 1;
210 mach_header_sz = sizeof(struct mach_header_64);
211 segment_command_sz = sizeof(struct segment_command_64);
212 }
213
214 task = current_task();
215 map = current_map();
216 mapsize = get_vmmap_size(map);
217
218 if (mapsize >= p->p_rlimit[RLIMIT_CORE].rlim_cur)
219 return (EFAULT);
220 (void) task_suspend(task);
221
222 /* create name according to sysctl'able format string */
223 name = proc_core_name(p->p_comm, kauth_cred_getuid(cred), p->p_pid);
224
225 /* if name creation fails, fall back to historical behaviour... */
226 if (name == NULL) {
227 sprintf(core_name, "/cores/core.%d", p->p_pid);
228 name = core_name;
229 }
230 context.vc_proc = p;
231 context.vc_ucred = cred;
232
233 if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, &context)))
234 return (error);
235
236 VATTR_INIT(&va);
237 VATTR_WANTED(&va, va_nlink);
238 /* Don't dump to non-regular files or files with links. */
239 if (vp->v_type != VREG ||
240 vnode_getattr(vp, &va, &context) || va.va_nlink != 1) {
241 error = EFAULT;
242 goto out;
243 }
244
245 VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */
246 VATTR_SET(&va, va_data_size, 0);
247 vnode_setattr(vp, &va, &context);
248 p->p_acflag |= ACORE;
249
250 /*
251 * If the task is modified while dumping the file
252 * (e.g., changes in threads or VM, the resulting
253 * file will not necessarily be correct.
254 */
255
256 thread_count = get_task_numacts(task);
257 segment_count = get_vmmap_entries(map); /* XXX */
258 bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array));
259 tstate_size = 0;
260 for (i = 0; i < mynum_flavors; i++)
261 tstate_size += sizeof(mythread_state_flavor_t) +
262 (flavors[i].count * sizeof(int));
263
264 command_size = segment_count * segment_command_sz +
265 thread_count*sizeof(struct thread_command) +
266 tstate_size*thread_count;
267
268 header_size = command_size + mach_header_sz;
269
270 (void) kmem_alloc(kernel_map,
271 (vm_offset_t *)&header,
272 (vm_size_t)header_size);
273
274 /*
275 * Set up Mach-O header.
276 */
277 if (is_64) {
278 mh64 = (struct mach_header_64 *)header;
279 mh64->magic = MH_MAGIC_64;
280 mh64->cputype = cpu_type();
281 mh64->cpusubtype = cpu_subtype();
282 mh64->filetype = MH_CORE;
283 mh64->ncmds = segment_count + thread_count;
284 mh64->sizeofcmds = command_size;
285 mh64->reserved = 0; /* 8 byte alignment */
286 } else {
287 mh = (struct mach_header *)header;
288 mh->magic = MH_MAGIC;
289 mh->cputype = cpu_type();
290 mh->cpusubtype = cpu_subtype();
291 mh->filetype = MH_CORE;
292 mh->ncmds = segment_count + thread_count;
293 mh->sizeofcmds = command_size;
294 }
295
296 hoffset = mach_header_sz; /* offset into header */
297 foffset = round_page(header_size); /* offset into file */
298 vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */
299
300 /*
301 * We use to check for an error, here, now we try and get
302 * as much as we can
303 */
304 while (segment_count > 0) {
305 struct segment_command *sc;
306 struct segment_command_64 *sc64;
307
308 /*
309 * Get region information for next region.
310 */
311
312 while (1) {
313 vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
314 if((kret = mach_vm_region_recurse(map,
315 &vmoffset, &vmsize, &nesting_depth,
316 (vm_region_recurse_info_t)&vbr,
317 &vbrcount)) != KERN_SUCCESS) {
318 break;
319 }
320 /*
321 * If we get a valid mapping back, but we're dumping
322 * a 32 bit process, and it's over the allowable
323 * address space of a 32 bit process, it's the same
324 * as if mach_vm_region_recurse() failed.
325 */
326 if (!(is_64) &&
327 (vmoffset + vmsize > VM_MAX_ADDRESS)) {
328 kret = KERN_INVALID_ADDRESS;
329 break;
330 }
331 if(vbr.is_submap) {
332 nesting_depth++;
333 continue;
334 } else {
335 break;
336 }
337 }
338 if(kret != KERN_SUCCESS)
339 break;
340
341 prot = vbr.protection;
342 maxprot = vbr.max_protection;
343 inherit = vbr.inheritance;
344 /*
345 * Fill in segment command structure.
346 */
347 if (is_64) {
348 sc64 = (struct segment_command_64 *)(header + hoffset);
349 sc64->cmd = LC_SEGMENT_64;
350 sc64->cmdsize = sizeof(struct segment_command_64);
351 /* segment name is zeroed by kmem_alloc */
352 sc64->segname[0] = 0;
353 sc64->vmaddr = vmoffset;
354 sc64->vmsize = vmsize;
355 sc64->fileoff = foffset;
356 sc64->filesize = vmsize;
357 sc64->maxprot = maxprot;
358 sc64->initprot = prot;
359 sc64->nsects = 0;
360 } else {
361 sc = (struct segment_command *) (header + hoffset);
362 sc->cmd = LC_SEGMENT;
363 sc->cmdsize = sizeof(struct segment_command);
364 /* segment name is zeroed by kmem_alloc */
365 sc->segname[0] = 0;
366 sc->vmaddr = CAST_DOWN(vm_offset_t,vmoffset);
367 sc->vmsize = CAST_DOWN(vm_size_t,vmsize);
368 sc->fileoff = CAST_DOWN(uint32_t,foffset);
369 sc->filesize = CAST_DOWN(uint32_t,vmsize);
370 sc->maxprot = maxprot;
371 sc->initprot = prot;
372 sc->nsects = 0;
373 }
374
375 /*
376 * Write segment out. Try as hard as possible to
377 * get read access to the data.
378 */
379 if ((prot & VM_PROT_READ) == 0) {
380 mach_vm_protect(map, vmoffset, vmsize, FALSE,
381 prot|VM_PROT_READ);
382 }
383 /*
384 * Only actually perform write if we can read.
385 * Note: if we can't read, then we end up with
386 * a hole in the file.
387 */
388 if ((maxprot & VM_PROT_READ) == VM_PROT_READ
389 && vbr.user_tag != VM_MEMORY_IOKIT
390 && coredumpok(map,vmoffset)) {
391 vm_map_size_t tmp_vmsize = vmsize;
392 off_t xfer_foffset = foffset;
393
394 //LP64todo - works around vn_rdwr_64() 2G limit
395 while (tmp_vmsize > 0) {
396 vm_map_size_t xfer_vmsize = tmp_vmsize;
397 if (xfer_vmsize > INT_MAX)
398 xfer_vmsize = INT_MAX;
399 error = vn_rdwr_64(UIO_WRITE, vp,
400 vmoffset, xfer_vmsize, xfer_foffset,
401 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
402 IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
403 tmp_vmsize -= xfer_vmsize;
404 xfer_foffset += xfer_vmsize;
405 }
406 }
407
408 hoffset += segment_command_sz;
409 foffset += vmsize;
410 vmoffset += vmsize;
411 segment_count--;
412 }
413
414 /*
415 * If there are remaining segments which have not been written
416 * out because break in the loop above, then they were not counted
417 * because they exceed the real address space of the executable
418 * type: remove them from the header's count. This is OK, since
419 * we are allowed to have a sparse area following the segments.
420 */
421 if (is_64) {
422 mh64->ncmds -= segment_count;
423 } else {
424 mh->ncmds -= segment_count;
425 }
426
427 tir1.header = header;
428 tir1.hoffset = hoffset;
429 tir1.flavors = flavors;
430 tir1.tstate_size = tstate_size;
431 task_act_iterate_wth_args(task, collectth_state,&tir1);
432
433 /*
434 * Write out the Mach header at the beginning of the
435 * file. OK to use a 32 bit write for this.
436 */
437 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
438 UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
439 kmem_free(kernel_map, header, header_size);
440 out:
441 error1 = vnode_close(vp, FWRITE, &context);
442 if (error == 0)
443 error = error1;
444
445 return (error);
446 }