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