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