xref: /xnu-12377.1.9/bsd/kern/kern_core.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 /*
2  * Copyright (c) 2000-2025 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_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 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.
14  *
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
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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  *
34  */
35 #if CONFIG_COREDUMP || CONFIG_UCOREDUMP
36 
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>
50 #include <sys/acct.h>
51 #include <sys/file_internal.h>
52 #include <sys/uio.h>
53 #include <sys/kernel.h>
54 #include <sys/stat.h>
55 
56 #include <mach-o/loader.h>
57 #include <mach/vm_region.h>
58 #include <mach/vm_statistics.h>
59 
60 #include <IOKit/IOBSD.h>
61 
62 #include <vm/vm_kern_xnu.h>
63 #include <vm/vm_protos.h> /* last */
64 #include <vm/vm_map_xnu.h>          /* current_map() */
65 #include <vm/pmap.h>            /* pmap_user_va_bits() */
66 #include <mach/mach_vm.h>       /* mach_vm_region_recurse() */
67 #include <mach/task.h>          /* task_suspend() */
68 #include <kern/task.h>          /* get_task_numacts() */
69 
70 #include <security/audit/audit.h>
71 
72 #if CONFIG_MACF
73 #include <security/mac_framework.h>
74 #endif /* CONFIG_MACF */
75 
76 
77 #include <kdp/core_notes.h>
78 
79 extern int freespace_mb(vnode_t vp);
80 
81 /* XXX not in a Mach header anywhere */
82 kern_return_t thread_getstatus(thread_t act, int flavor,
83     thread_state_t tstate, mach_msg_type_number_t *count);
84 void task_act_iterate_wth_args(task_t, void (*)(thread_t, void *), void *);
85 
86 #ifdef SECURE_KERNEL
87 __XNU_PRIVATE_EXTERN int do_coredump = 0;       /* default: don't dump cores */
88 #else
89 __XNU_PRIVATE_EXTERN int do_coredump = 1;       /* default: dump cores */
90 #endif /* SECURE_KERNEL */
91 __XNU_PRIVATE_EXTERN int sugid_coredump = 0; /* default: but not SGUID binaries */
92 
93 #if CONFIG_UCOREDUMP
94 __XNU_PRIVATE_EXTERN int do_ucoredump = 0;   /* default: kernel does dumps */
95 #endif
96 
97 /*
98  * is_coredump_eligible
99  *
100  * Determine if a core should even be dumped at all (by any mechanism)
101  *
102  * Does NOT include disk permission or space constraints
103  *
104  * core_proc		Process to dump core [*] must be current proc!
105  *
106  * Return:	0	Success
107  *		!0	Failure errno
108  */
109 int
is_coredump_eligible(proc_t core_proc)110 is_coredump_eligible(proc_t core_proc)
111 {
112 	if (current_proc() != core_proc && (
113 		    core_proc->p_exit_reason &&
114 		    core_proc->p_exit_reason->osr_namespace == OS_REASON_JETSAM)) {
115 		return EPERM;
116 	}
117 	if (current_proc() != core_proc) {
118 		panic("coredump for proc that is not current: %p)", core_proc);
119 	}
120 
121 	vfs_context_t ctx = vfs_context_current();
122 	kauth_cred_t cred = vfs_context_ucred(ctx);
123 
124 	if (do_coredump == 0 ||         /* Not dumping at all */
125 	    ((sugid_coredump == 0) &&   /* Not dumping SUID/SGID binaries */
126 	    ((kauth_cred_getsvuid(cred) != kauth_cred_getruid(cred)) ||
127 	    (kauth_cred_getsvgid(cred) != kauth_cred_getrgid(cred))))) {
128 		return EPERM;
129 	}
130 
131 #if CONFIG_MACF
132 	const int error = mac_proc_check_dump_core(core_proc);
133 	if (error != 0) {
134 		return error;
135 	}
136 #endif
137 	return 0;
138 }
139 
140 #else /* CONFIG_COREDUMP || CONFIG_UCOREDUMP */
141 
142 /* When core dumps aren't needed, no need to compile this file at all */
143 
144 #error assertion failed: this section is not compiled
145 
146 #endif /* CONFIG_COREDUMP || CONFIG_UCOREDUMP */
147 
148 #if CONFIG_COREDUMP
149 
150 #define COREDUMP_CUSTOM_LOCATION_ENTITLEMENT "com.apple.private.custom-coredump-location"
151 
152 typedef struct {
153 	int     flavor;                 /* the number for this flavor */
154 	mach_msg_type_number_t  count;        /* count of ints in this flavor */
155 } mythread_state_flavor_t;
156 
157 #if defined (__i386__) || defined (__x86_64__)
158 mythread_state_flavor_t thread_flavor_array[] = {
159 	{x86_THREAD_STATE, x86_THREAD_STATE_COUNT},
160 	{x86_FLOAT_STATE, x86_FLOAT_STATE_COUNT},
161 	{x86_EXCEPTION_STATE, x86_EXCEPTION_STATE_COUNT},
162 };
163 int mynum_flavors = 3;
164 #elif defined (__arm64__)
165 mythread_state_flavor_t thread_flavor_array[] = {
166 	{ARM_THREAD_STATE64, ARM_THREAD_STATE64_COUNT},
167 	/* ARM64_TODO: VFP */
168 	{ARM_EXCEPTION_STATE64, ARM_EXCEPTION_STATE64_COUNT}
169 };
170 int mynum_flavors = 2;
171 #else
172 #error architecture not supported
173 #endif
174 
175 
176 typedef struct {
177 	vm_offset_t header;
178 	size_t hoffset;
179 	mythread_state_flavor_t *flavors;
180 	size_t tstate_size;
181 	size_t flavor_count;
182 } tir_t;
183 
184 /* cpu_type returns only the most generic indication of the current CPU. */
185 /* in a core we want to know the kind of process. */
186 
187 cpu_type_t
process_cpu_type(proc_t core_proc)188 process_cpu_type(proc_t core_proc)
189 {
190 	cpu_type_t what_we_think;
191 #if defined (__i386__) || defined (__x86_64__)
192 	if (IS_64BIT_PROCESS(core_proc)) {
193 		what_we_think = CPU_TYPE_X86_64;
194 	} else {
195 		what_we_think = CPU_TYPE_I386;
196 	}
197 #elif defined(__arm64__)
198 	if (IS_64BIT_PROCESS(core_proc)) {
199 		what_we_think = CPU_TYPE_ARM64;
200 	} else {
201 		what_we_think = CPU_TYPE_ARM;
202 	}
203 #endif
204 
205 	return what_we_think;
206 }
207 
208 cpu_type_t
process_cpu_subtype(proc_t core_proc)209 process_cpu_subtype(proc_t core_proc)
210 {
211 	cpu_type_t what_we_think;
212 #if defined (__i386__) || defined (__x86_64__)
213 	if (IS_64BIT_PROCESS(core_proc)) {
214 		what_we_think = CPU_SUBTYPE_X86_64_ALL;
215 	} else {
216 		what_we_think = CPU_SUBTYPE_I386_ALL;
217 	}
218 #elif defined(__arm64__)
219 	if (IS_64BIT_PROCESS(core_proc)) {
220 		what_we_think = CPU_SUBTYPE_ARM64_ALL;
221 	} else {
222 		what_we_think = CPU_SUBTYPE_ARM_ALL;
223 	}
224 #endif
225 	return what_we_think;
226 }
227 
228 static void
collectth_state(thread_t th_act,void * tirp)229 collectth_state(thread_t th_act, void *tirp)
230 {
231 	vm_offset_t     header;
232 	size_t  hoffset, i;
233 	mythread_state_flavor_t *flavors;
234 	struct thread_command   *tc;
235 	tir_t *t = (tir_t *)tirp;
236 
237 	/*
238 	 *	Fill in thread command structure.
239 	 */
240 	header = t->header;
241 	hoffset = t->hoffset;
242 	flavors = t->flavors;
243 
244 	tc = (struct thread_command *) (header + hoffset);
245 	tc->cmd = LC_THREAD;
246 	tc->cmdsize = (uint32_t)(sizeof(struct thread_command)
247 	    + t->tstate_size);
248 	hoffset += sizeof(struct thread_command);
249 	/*
250 	 * Follow with a struct thread_state_flavor and
251 	 * the appropriate thread state struct for each
252 	 * thread state flavor.
253 	 */
254 	for (i = 0; i < t->flavor_count; i++) {
255 		*(mythread_state_flavor_t *)(header + hoffset) =
256 		    flavors[i];
257 		hoffset += sizeof(mythread_state_flavor_t);
258 		thread_getstatus(th_act, flavors[i].flavor,
259 		    (thread_state_t)(header + hoffset),
260 		    &flavors[i].count);
261 		hoffset += flavors[i].count * sizeof(int);
262 	}
263 
264 	t->hoffset = hoffset;
265 }
266 
267 #if DEVELOPMENT || DEBUG
268 #define COREDUMPLOG(fmt, args...) printf("coredump (%s, pid %d): " fmt "\n", core_proc->p_comm, proc_getpid(core_proc), ## args)
269 #else
270 #define COREDUMPLOG(fmt, args...)
271 #endif
272 
273 /*
274  * LC_NOTE support for userspace coredumps.
275  */
276 
277 typedef int (write_note_cb_t)(struct vnode *vp, off_t foffset);
278 
279 static int
note_addrable_bits(struct vnode * vp,off_t foffset)280 note_addrable_bits(struct vnode *vp, off_t foffset)
281 {
282 	task_t t = current_task();
283 	vfs_context_t ctx = vfs_context_current();
284 	kauth_cred_t cred = vfs_context_ucred(ctx);
285 
286 	addrable_bits_note_t note = {
287 		.version = ADDRABLE_BITS_VER,
288 		.addressing_bits = pmap_user_va_bits(get_task_pmap(t)),
289 		.unused = 0
290 	};
291 
292 	return vn_rdwr_64(UIO_WRITE, vp, (vm_offset_t)&note, sizeof(note), foffset, UIO_SYSSPACE,
293 	           IO_NODELOCKED | IO_UNIT, cred, 0, current_proc());
294 }
295 
296 /*
297  * note handling
298  */
299 
300 struct core_note {
301 	size_t          cn_size;
302 	const char      *cn_owner;
303 	write_note_cb_t *cn_write_cb;
304 } const core_notes[] = {
305 	{
306 		.cn_size = sizeof(addrable_bits_note_t),
307 		.cn_owner = ADDRABLE_BITS_DATA_OWNER,
308 		.cn_write_cb = note_addrable_bits,
309 	}
310 };
311 
312 const size_t notes_count = sizeof(core_notes) / sizeof(struct core_note);
313 
314 /*
315  * LC_NOTE commands are allocated as a part of Mach-O header and are written to
316  * disk at the end of coredump. LC_NOTE's payload has to be written in callbacks here.
317  */
318 static int
dump_notes(proc_t __unused core_proc,vm_offset_t header,size_t hoffset,struct vnode * vp,off_t foffset)319 dump_notes(proc_t __unused core_proc, vm_offset_t header, size_t hoffset, struct vnode *vp, off_t foffset)
320 {
321 	for (size_t i = 0; i < notes_count; i++) {
322 		int error = 0;
323 
324 		if (core_notes[i].cn_write_cb == NULL) {
325 			continue;
326 		}
327 
328 		/* Generate LC_NOTE command. */
329 		struct note_command *nc = (struct note_command *)(header + hoffset);
330 
331 		nc->cmd = LC_NOTE;
332 		nc->cmdsize = sizeof(struct note_command);
333 		nc->offset = foffset;
334 		nc->size = core_notes[i].cn_size;
335 		strlcpy(nc->data_owner, core_notes[i].cn_owner, sizeof(nc->data_owner));
336 
337 		hoffset += sizeof(struct note_command);
338 
339 		/* Add note's payload. */
340 		error = core_notes[i].cn_write_cb(vp, foffset);
341 		if (error != KERN_SUCCESS) {
342 			COREDUMPLOG("failed to write LC_NOTE %s: error %d", core_notes[i].cn_owner, error);
343 			return error;
344 		}
345 
346 		foffset += core_notes[i].cn_size;
347 	}
348 
349 	return 0;
350 }
351 
352 /*
353  * coredump
354  *
355  * Description:	Create a core image on the file "core" for the process
356  *		indicated
357  *
358  * Parameters:	core_proc			Process to dump core [*]
359  *		reserve_mb			If non-zero, leave filesystem with
360  *						at least this much free space.
361  *		coredump_flags	Extra options (ignore rlimit, run fsync)
362  *
363  * Returns:	0				Success
364  *		!0				Failure errno
365  *
366  * IMPORTANT:	This function can only be called on the current process, due
367  *		to assumptions below; see variable declaration section for
368  *		details.
369  */
370 #define MAX_TSTATE_FLAVORS      10
371 int
coredump(proc_t core_proc,uint32_t reserve_mb,int coredump_flags)372 coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags)
373 {
374 /* Begin assumptions that limit us to only the current process */
375 	vfs_context_t ctx = vfs_context_current();
376 	vm_map_t        map = current_map();
377 	task_t          task = current_task();
378 /* End assumptions */
379 	kauth_cred_t cred = vfs_context_ucred(ctx);
380 	int error = 0;
381 	struct vnode_attr *vap = NULL;
382 	size_t          thread_count, segment_count;
383 	size_t          command_size, header_size, tstate_size;
384 	size_t          hoffset;
385 	off_t           foffset;
386 	mach_vm_offset_t vmoffset;
387 	vm_offset_t     header;
388 	mach_vm_size_t  vmsize;
389 	vm_prot_t       prot;
390 	vm_prot_t       maxprot;
391 	int             error1 = 0;
392 	char            stack_name[MAXCOMLEN + 6];
393 	char            *alloced_name = NULL;
394 	char            *name = NULL;
395 	mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
396 	vm_size_t       mapsize;
397 	size_t          i;
398 	uint32_t nesting_depth = 0;
399 	kern_return_t   kret;
400 	struct vm_region_submap_info_64 vbr;
401 	mach_msg_type_number_t vbrcount = 0;
402 	tir_t tir1;
403 	struct vnode * vp;
404 	struct mach_header      *mh = NULL;        /* protected by is_64 */
405 	struct mach_header_64   *mh64 = NULL;        /* protected by is_64 */
406 	int             is_64 = 0;
407 	size_t          mach_header_sz = sizeof(struct mach_header);
408 	size_t          segment_command_sz = sizeof(struct segment_command);
409 	size_t          notes_size = 0;
410 	const char     *format = NULL;
411 	char           *custom_location_entitlement = NULL;
412 	size_t          custom_location_entitlement_len = 0;
413 	char           *alloced_format = NULL;
414 	size_t          alloced_format_len = 0;
415 	bool            include_iokit_memory = task_is_driver(task);
416 	bool            coredump_attempted = false;
417 
418 	if ((error = is_coredump_eligible(core_proc)) != 0) {
419 		goto out2;
420 	}
421 
422 	if (IS_64BIT_PROCESS(core_proc)) {
423 		is_64 = 1;
424 		mach_header_sz = sizeof(struct mach_header_64);
425 		segment_command_sz = sizeof(struct segment_command_64);
426 	}
427 
428 	mapsize = get_vmmap_size(map);
429 
430 	custom_location_entitlement = IOCurrentTaskGetEntitlement(COREDUMP_CUSTOM_LOCATION_ENTITLEMENT);
431 	if (custom_location_entitlement != NULL) {
432 		custom_location_entitlement_len = strlen(custom_location_entitlement);
433 		const char * dirname;
434 		if (proc_is_driver(core_proc)) {
435 			dirname = defaultdrivercorefiledir;
436 		} else {
437 			dirname = defaultcorefiledir;
438 		}
439 		size_t dirname_len = strlen(dirname);
440 		size_t printed_len;
441 
442 		/* new format is dirname + "/" + string from entitlement */
443 		alloced_format_len = dirname_len + 1 + custom_location_entitlement_len;
444 		alloced_format = kalloc_data(alloced_format_len + 1, Z_ZERO | Z_WAITOK | Z_NOFAIL);
445 		printed_len = snprintf(alloced_format, alloced_format_len + 1, "%s/%s", dirname, custom_location_entitlement);
446 		assert(printed_len == alloced_format_len);
447 
448 		format = alloced_format;
449 	} else {
450 		if (proc_is_driver(core_proc)) {
451 			format = drivercorefilename;
452 		} else {
453 			format = corefilename;
454 		}
455 	}
456 
457 	if (((coredump_flags & COREDUMP_IGNORE_ULIMIT) == 0) &&
458 	    (mapsize >= proc_limitgetcur(core_proc, RLIMIT_CORE))) {
459 		error = EFAULT;
460 		goto out2;
461 	}
462 
463 	/* log coredump failures from here */
464 	coredump_attempted = true;
465 
466 	(void) task_suspend_internal(task);
467 
468 
469 	alloced_name = zalloc_flags(ZV_NAMEI, Z_NOWAIT | Z_ZERO);
470 
471 	/* create name according to sysctl'able format string */
472 	/* if name creation fails, fall back to historical behaviour... */
473 	if (alloced_name == NULL ||
474 	    proc_core_name(format, core_proc->p_comm, kauth_cred_getuid(cred),
475 	    proc_getpid(core_proc), alloced_name, MAXPATHLEN)) {
476 		snprintf(stack_name, sizeof(stack_name),
477 		    "/cores/core.%d", proc_getpid(core_proc));
478 		name = stack_name;
479 	} else {
480 		name = alloced_name;
481 	}
482 
483 	COREDUMPLOG("writing core to %s", name);
484 	if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, ctx))) {
485 		COREDUMPLOG("failed to open core dump file %s: error %d", name, error);
486 		goto out2;
487 	}
488 
489 	vap = kalloc_type(struct vnode_attr, Z_WAITOK | Z_ZERO);
490 	VATTR_INIT(vap);
491 	VATTR_WANTED(vap, va_nlink);
492 	/* Don't dump to non-regular files or files with links. */
493 	if (vp->v_type != VREG ||
494 	    vnode_getattr(vp, vap, ctx) || vap->va_nlink != 1) {
495 		COREDUMPLOG("failed to write core to non-regular file");
496 		error = EFAULT;
497 		goto out;
498 	}
499 
500 	VATTR_INIT(vap);         /* better to do it here than waste more stack in vnode_setsize */
501 	VATTR_SET(vap, va_data_size, 0);
502 	if (core_proc == initproc) {
503 		VATTR_SET(vap, va_dataprotect_class, PROTECTION_CLASS_D);
504 	}
505 	vnode_setattr(vp, vap, ctx);
506 	core_proc->p_acflag |= ACORE;
507 
508 	COREDUMPLOG("map size: %lu", mapsize);
509 	if ((reserve_mb > 0) &&
510 	    ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) {
511 		COREDUMPLOG("insufficient free space (free=%d MB, needed=%lu MB, reserve=%d MB)", freespace_mb(vp), (mapsize >> 20), reserve_mb);
512 		error = ENOSPC;
513 		goto out;
514 	}
515 
516 	/*
517 	 *	If the task is modified while dumping the file
518 	 *	(e.g., changes in threads or VM, the resulting
519 	 *	file will not necessarily be correct.
520 	 */
521 
522 	thread_count = get_task_numacts(task);
523 	segment_count = get_vmmap_entries(map);         /* XXX */
524 	tir1.flavor_count = sizeof(thread_flavor_array) / sizeof(mythread_state_flavor_t);
525 	bcopy(thread_flavor_array, flavors, sizeof(thread_flavor_array));
526 	tstate_size = 0;
527 	for (i = 0; i < tir1.flavor_count; i++) {
528 		tstate_size += sizeof(mythread_state_flavor_t) +
529 		    (flavors[i].count * sizeof(int));
530 	}
531 
532 	{
533 		size_t lhs;
534 		size_t rhs;
535 
536 		/* lhs = segment_count * segment_command_sz */
537 		if (os_mul_overflow(segment_count, segment_command_sz, &lhs)) {
538 			COREDUMPLOG("error: segment size overflow: segment_count=%lu, segment_command_sz=%lu", segment_count, segment_command_sz);
539 			error = ENOMEM;
540 			goto out;
541 		}
542 
543 		/* rhs = (tstate_size + sizeof(struct thread_command)) * thread_count */
544 		if (os_add_and_mul_overflow(tstate_size, sizeof(struct thread_command), thread_count, &rhs)) {
545 			COREDUMPLOG("error: thread state size overflow: tstate_size=%lu, thread_count=%lu", tstate_size, thread_count);
546 			error = ENOMEM;
547 			goto out;
548 		}
549 
550 		/* command_size = lhs + rhs */
551 		if (os_add_overflow(lhs, rhs, &command_size)) {
552 			COREDUMPLOG("error: command size overflow: lhs=%lu, rhs=%lu", lhs, rhs);
553 			error = ENOMEM;
554 			goto out;
555 		}
556 
557 		/* Add notes payload. */
558 		if (os_mul_overflow(notes_count, sizeof(struct note_command), &notes_size)) {
559 			COREDUMPLOG("error: note command size overflow: note=%lu", i);
560 			error = ENOMEM;
561 			goto out;
562 		}
563 
564 		if (os_add_overflow(command_size, notes_size, &command_size)) {
565 			COREDUMPLOG("error: notes overflow: notes_size=%lu", notes_size);
566 			error = ENOMEM;
567 			goto out;
568 		}
569 	}
570 
571 	if (os_add_overflow(command_size, mach_header_sz, &header_size)) {
572 		COREDUMPLOG("error: header size overflow: command_size=%lu, mach_header_sz=%lu", command_size, mach_header_sz);
573 		error = ENOMEM;
574 		goto out;
575 	}
576 
577 	if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size,
578 	    KMA_DATA | KMA_ZERO, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
579 		COREDUMPLOG("error: failed to allocate memory for header (size=%lu)", header_size);
580 		error = ENOMEM;
581 		goto out;
582 	}
583 
584 	/*
585 	 *	Set up Mach-O header.
586 	 */
587 	if (is_64) {
588 		mh64 = (struct mach_header_64 *)header;
589 		mh64->magic = MH_MAGIC_64;
590 		mh64->cputype = process_cpu_type(core_proc);
591 		mh64->cpusubtype = process_cpu_subtype(core_proc);
592 		mh64->filetype = MH_CORE;
593 		mh64->ncmds = (uint32_t)(segment_count + notes_count + thread_count);
594 		mh64->sizeofcmds = (uint32_t)command_size;
595 	} else {
596 		mh = (struct mach_header *)header;
597 		mh->magic = MH_MAGIC;
598 		mh->cputype = process_cpu_type(core_proc);
599 		mh->cpusubtype = process_cpu_subtype(core_proc);
600 		mh->filetype = MH_CORE;
601 		mh->ncmds = (uint32_t)(segment_count + notes_count + thread_count);
602 		mh->sizeofcmds = (uint32_t)command_size;
603 	}
604 
605 	hoffset = mach_header_sz;         /* offset into header */
606 	foffset = round_page(header_size);         /* offset into file */
607 	vmoffset = MACH_VM_MIN_ADDRESS;         /* offset into VM */
608 	COREDUMPLOG("mach header size: %zu", header_size);
609 
610 	/*
611 	 * We use to check for an error, here, now we try and get
612 	 * as much as we can
613 	 */
614 	COREDUMPLOG("dumping %zu segments", segment_count);
615 	while (segment_count > 0) {
616 		struct segment_command          *sc;
617 		struct segment_command_64       *sc64;
618 
619 		/*
620 		 *	Get region information for next region.
621 		 */
622 
623 		while (1) {
624 			vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
625 			if ((kret = mach_vm_region_recurse(map,
626 			    &vmoffset, &vmsize, &nesting_depth,
627 			    (vm_region_recurse_info_t)&vbr,
628 			    &vbrcount)) != KERN_SUCCESS) {
629 				break;
630 			}
631 			/*
632 			 * If we get a valid mapping back, but we're dumping
633 			 * a 32 bit process,  and it's over the allowable
634 			 * address space of a 32 bit process, it's the same
635 			 * as if mach_vm_region_recurse() failed.
636 			 */
637 			if (!(is_64) &&
638 			    (vmoffset + vmsize > VM_MAX_ADDRESS)) {
639 				kret = KERN_INVALID_ADDRESS;
640 				COREDUMPLOG("exceeded allowable region for 32-bit process");
641 				break;
642 			}
643 			if (vbr.is_submap) {
644 				nesting_depth++;
645 				continue;
646 			} else {
647 				break;
648 			}
649 		}
650 		if (kret != KERN_SUCCESS) {
651 			COREDUMPLOG("ending segment dump, kret=%d", kret);
652 			break;
653 		}
654 
655 		prot = vbr.protection;
656 		maxprot = vbr.max_protection;
657 
658 		if ((prot | maxprot) == VM_PROT_NONE) {
659 			/*
660 			 * Elide unreadable (likely reserved) segments
661 			 */
662 			COREDUMPLOG("eliding unreadable segment %llx->%llx", vmoffset, vmoffset + vmsize);
663 			vmoffset += vmsize;
664 			continue;
665 		}
666 
667 		/*
668 		 * Try as hard as possible to get read access to the data.
669 		 */
670 		if ((prot & VM_PROT_READ) == 0) {
671 			mach_vm_protect(map, vmoffset, vmsize, FALSE,
672 			    prot | VM_PROT_READ);
673 		}
674 
675 		/*
676 		 * But only try and perform the write if we can read it.
677 		 */
678 		int64_t fsize = ((maxprot & VM_PROT_READ) == VM_PROT_READ
679 		    && (include_iokit_memory || vbr.user_tag != VM_MEMORY_IOKIT)
680 		    && coredumpok(map, vmoffset)) ? vmsize : 0;
681 
682 		if (fsize) {
683 			int64_t resid = 0;
684 			const enum uio_seg sflg = IS_64BIT_PROCESS(core_proc) ?
685 			    UIO_USERSPACE64 : UIO_USERSPACE32;
686 
687 			error = vn_rdwr_64(UIO_WRITE, vp, vmoffset, fsize,
688 			    foffset, sflg, IO_NODELOCKED | IO_UNIT,
689 			    cred, &resid, core_proc);
690 
691 			if (error) {
692 				/*
693 				 * Mark segment as empty
694 				 */
695 				fsize = 0;
696 				COREDUMPLOG("failed to write segment %llx->%llx: error %d", vmoffset, vmoffset + vmsize, error);
697 			} else if (resid) {
698 				/*
699 				 * Partial write. Extend the file size so
700 				 * that the segment command contains a valid
701 				 * range of offsets, possibly creating a hole.
702 				 */
703 				VATTR_INIT(vap);
704 				VATTR_SET(vap, va_data_size, foffset + fsize);
705 				vnode_setattr(vp, vap, ctx);
706 				COREDUMPLOG("partially wrote segment %llx->%llx, resid %lld", vmoffset, vmoffset + vmsize, resid);
707 			}
708 		} else {
709 			COREDUMPLOG("skipping unreadable segment %llx->%llx", vmoffset, vmoffset + vmsize);
710 		}
711 
712 		/*
713 		 *	Fill in segment command structure.
714 		 */
715 
716 		if (is_64) {
717 			sc64 = (struct segment_command_64 *)(header + hoffset);
718 			sc64->cmd = LC_SEGMENT_64;
719 			sc64->cmdsize = sizeof(struct segment_command_64);
720 			/* segment name is zeroed by kmem_alloc */
721 			sc64->segname[0] = 0;
722 			sc64->vmaddr = vmoffset;
723 			sc64->vmsize = vmsize;
724 			sc64->fileoff = foffset;
725 			sc64->filesize = fsize;
726 			sc64->maxprot = maxprot;
727 			sc64->initprot = prot;
728 			sc64->nsects = 0;
729 			sc64->flags = 0;
730 		} else {
731 			sc = (struct segment_command *) (header + hoffset);
732 			sc->cmd = LC_SEGMENT;
733 			sc->cmdsize = sizeof(struct segment_command);
734 			/* segment name is zeroed by kmem_alloc */
735 			sc->segname[0] = 0;
736 			sc->vmaddr = CAST_DOWN_EXPLICIT(uint32_t, vmoffset);
737 			sc->vmsize = CAST_DOWN_EXPLICIT(uint32_t, vmsize);
738 			sc->fileoff = CAST_DOWN_EXPLICIT(uint32_t, foffset);         /* will never truncate */
739 			sc->filesize = CAST_DOWN_EXPLICIT(uint32_t, fsize);         /* will never truncate */
740 			sc->maxprot = maxprot;
741 			sc->initprot = prot;
742 			sc->nsects = 0;
743 			sc->flags = 0;
744 		}
745 
746 		hoffset += segment_command_sz;
747 		foffset += fsize;
748 		vmoffset += vmsize;
749 		segment_count--;
750 	}
751 	COREDUMPLOG("max file offset: %lld", foffset);
752 
753 	/*
754 	 * If there are remaining segments which have not been written
755 	 * out because break in the loop above, then they were not counted
756 	 * because they exceed the real address space of the executable
757 	 * type: remove them from the header's count.  This is OK, since
758 	 * we are allowed to have a sparse area following the segments.
759 	 */
760 	if (is_64) {
761 		mh64->ncmds -= segment_count;
762 		mh64->sizeofcmds -= segment_count * segment_command_sz;
763 	} else {
764 		mh->ncmds -= segment_count;
765 		mh->sizeofcmds -= segment_count * segment_command_sz;
766 	}
767 
768 	/* Add LC_NOTES */
769 	COREDUMPLOG("dumping %zu notes", notes_count);
770 	if (dump_notes(core_proc, header, hoffset, vp, foffset) != 0) {
771 		error = EFAULT;
772 		goto out;
773 	}
774 
775 	tir1.header = header;
776 	tir1.hoffset = hoffset + notes_size;
777 	tir1.flavors = flavors;
778 	tir1.tstate_size = tstate_size;
779 	COREDUMPLOG("dumping %zu threads", thread_count);
780 	task_act_iterate_wth_args(task, collectth_state, &tir1);
781 
782 	/*
783 	 *	Write out the Mach header at the beginning of the
784 	 *	file.  OK to use a 32 bit write for this.
785 	 */
786 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, (int)MIN(header_size, INT_MAX), (off_t)0,
787 	    UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) 0, core_proc);
788 	if (error != KERN_SUCCESS) {
789 		COREDUMPLOG("failed to write mach header: error %d", error);
790 	}
791 	kmem_free(kernel_map, header, header_size);
792 
793 	if ((coredump_flags & COREDUMP_FULLFSYNC) && error == 0) {
794 		error = VNOP_IOCTL(vp, F_FULLFSYNC, (caddr_t)NULL, 0, ctx);
795 		if (error != KERN_SUCCESS) {
796 			COREDUMPLOG("failed to FULLFSYNC core: error %d", error);
797 		}
798 	}
799 out:
800 	if (vap) {
801 		kfree_type(struct vnode_attr, vap);
802 	}
803 	error1 = vnode_close(vp, FWRITE, ctx);
804 	if (error1 != KERN_SUCCESS) {
805 		COREDUMPLOG("failed to close core file: error %d", error1);
806 	}
807 out2:
808 #if CONFIG_AUDIT
809 	audit_proc_coredump(core_proc, name, error);
810 #endif
811 	if (alloced_name != NULL) {
812 		zfree(ZV_NAMEI, alloced_name);
813 	}
814 	if (alloced_format != NULL) {
815 		kfree_data(alloced_format, alloced_format_len + 1);
816 	}
817 	if (custom_location_entitlement != NULL) {
818 		kfree_data(custom_location_entitlement, custom_location_entitlement_len + 1);
819 	}
820 	if (error == 0) {
821 		error = error1;
822 	}
823 
824 	if (coredump_attempted) {
825 		if (error != 0) {
826 			COREDUMPLOG("core dump failed: error %d\n", error);
827 		} else {
828 			COREDUMPLOG("core dump succeeded");
829 		}
830 	}
831 
832 	return error;
833 }
834 
835 #endif /* CONFIG_COREDUMP */
836