xref: /xnu-8796.101.5/bsd/kern/kern_core.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
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.h>
63 #include <vm/vm_protos.h> /* last */
64 #include <vm/vm_map.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 		panic("coredump() called against proc that is not current_proc: %p", core_proc);
363 	}
364 
365 	if (do_coredump == 0 ||         /* Not dumping at all */
366 	    ((sugid_coredump == 0) &&   /* Not dumping SUID/SGID binaries */
367 	    ((kauth_cred_getsvuid(cred) != kauth_cred_getruid(cred)) ||
368 	    (kauth_cred_getsvgid(cred) != kauth_cred_getrgid(cred))))) {
369 		error = EFAULT;
370 		goto out2;
371 	}
372 
373 #if CONFIG_MACF
374 	error = mac_proc_check_dump_core(core_proc);
375 	if (error != 0) {
376 		goto out2;
377 	}
378 #endif
379 
380 	if (IS_64BIT_PROCESS(core_proc)) {
381 		is_64 = 1;
382 		mach_header_sz = sizeof(struct mach_header_64);
383 		segment_command_sz = sizeof(struct segment_command_64);
384 	}
385 
386 	mapsize = get_vmmap_size(map);
387 
388 	custom_location_entitlement = IOCurrentTaskGetEntitlement(COREDUMP_CUSTOM_LOCATION_ENTITLEMENT);
389 	if (custom_location_entitlement != NULL) {
390 		custom_location_entitlement_len = strlen(custom_location_entitlement);
391 		const char * dirname;
392 		if (proc_is_driver(core_proc)) {
393 			dirname = defaultdrivercorefiledir;
394 		} else {
395 			dirname = defaultcorefiledir;
396 		}
397 		size_t dirname_len = strlen(dirname);
398 		size_t printed_len;
399 
400 		/* new format is dirname + "/" + string from entitlement */
401 		alloced_format_len = dirname_len + 1 + custom_location_entitlement_len;
402 		alloced_format = kalloc_data(alloced_format_len + 1, Z_ZERO | Z_WAITOK | Z_NOFAIL);
403 		printed_len = snprintf(alloced_format, alloced_format_len + 1, "%s/%s", dirname, custom_location_entitlement);
404 		assert(printed_len == alloced_format_len);
405 
406 		format = alloced_format;
407 		coredump_flags |= COREDUMP_IGNORE_ULIMIT;
408 	} else {
409 		if (proc_is_driver(core_proc)) {
410 			format = drivercorefilename;
411 		} else {
412 			format = corefilename;
413 		}
414 	}
415 
416 	if (((coredump_flags & COREDUMP_IGNORE_ULIMIT) == 0) &&
417 	    (mapsize >= proc_limitgetcur(core_proc, RLIMIT_CORE))) {
418 		error = EFAULT;
419 		goto out2;
420 	}
421 
422 	/* log coredump failures from here */
423 	coredump_attempted = true;
424 
425 	(void) task_suspend_internal(task);
426 
427 	alloced_name = zalloc_flags(ZV_NAMEI, Z_NOWAIT | Z_ZERO);
428 
429 	/* create name according to sysctl'able format string */
430 	/* if name creation fails, fall back to historical behaviour... */
431 	if (alloced_name == NULL ||
432 	    proc_core_name(format, core_proc->p_comm, kauth_cred_getuid(cred),
433 	    proc_getpid(core_proc), alloced_name, MAXPATHLEN)) {
434 		snprintf(stack_name, sizeof(stack_name),
435 		    "/cores/core.%d", proc_getpid(core_proc));
436 		name = stack_name;
437 	} else {
438 		name = alloced_name;
439 	}
440 
441 	COREDUMPLOG("writing core to %s", name);
442 	if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, ctx))) {
443 		COREDUMPLOG("failed to open core dump file %s: error %d", name, error);
444 		goto out2;
445 	}
446 
447 	vap = kalloc_type(struct vnode_attr, Z_WAITOK | Z_ZERO);
448 	VATTR_INIT(vap);
449 	VATTR_WANTED(vap, va_nlink);
450 	/* Don't dump to non-regular files or files with links. */
451 	if (vp->v_type != VREG ||
452 	    vnode_getattr(vp, vap, ctx) || vap->va_nlink != 1) {
453 		COREDUMPLOG("failed to write core to non-regular file");
454 		error = EFAULT;
455 		goto out;
456 	}
457 
458 	VATTR_INIT(vap);        /* better to do it here than waste more stack in vnode_setsize */
459 	VATTR_SET(vap, va_data_size, 0);
460 	if (core_proc == initproc) {
461 		VATTR_SET(vap, va_dataprotect_class, PROTECTION_CLASS_D);
462 	}
463 	vnode_setattr(vp, vap, ctx);
464 	core_proc->p_acflag |= ACORE;
465 
466 	COREDUMPLOG("map size: %lu", mapsize);
467 	if ((reserve_mb > 0) &&
468 	    ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) {
469 		COREDUMPLOG("insufficient free space (free=%d MB, needed=%lu MB, reserve=%d MB)", freespace_mb(vp), (mapsize >> 20), reserve_mb);
470 		error = ENOSPC;
471 		goto out;
472 	}
473 
474 	/*
475 	 *	If the task is modified while dumping the file
476 	 *	(e.g., changes in threads or VM, the resulting
477 	 *	file will not necessarily be correct.
478 	 */
479 
480 	thread_count = get_task_numacts(task);
481 	segment_count = get_vmmap_entries(map); /* XXX */
482 	tir1.flavor_count = sizeof(thread_flavor_array) / sizeof(mythread_state_flavor_t);
483 	bcopy(thread_flavor_array, flavors, sizeof(thread_flavor_array));
484 	tstate_size = 0;
485 	for (i = 0; i < tir1.flavor_count; i++) {
486 		tstate_size += sizeof(mythread_state_flavor_t) +
487 		    (flavors[i].count * sizeof(int));
488 	}
489 
490 	{
491 		size_t lhs;
492 		size_t rhs;
493 
494 		/* lhs = segment_count * segment_command_sz */
495 		if (os_mul_overflow(segment_count, segment_command_sz, &lhs)) {
496 			COREDUMPLOG("error: segment size overflow: segment_count=%lu, segment_command_sz=%lu", segment_count, segment_command_sz);
497 			error = ENOMEM;
498 			goto out;
499 		}
500 
501 		/* rhs = (tstate_size + sizeof(struct thread_command)) * thread_count */
502 		if (os_add_and_mul_overflow(tstate_size, sizeof(struct thread_command), thread_count, &rhs)) {
503 			COREDUMPLOG("error: thread state size overflow: tstate_size=%lu, thread_count=%lu", tstate_size, thread_count);
504 			error = ENOMEM;
505 			goto out;
506 		}
507 
508 		/* command_size = lhs + rhs */
509 		if (os_add_overflow(lhs, rhs, &command_size)) {
510 			COREDUMPLOG("error: command size overflow: lhs=%lu, rhs=%lu", lhs, rhs);
511 			error = ENOMEM;
512 			goto out;
513 		}
514 
515 		/* Add notes payload. */
516 		if (os_mul_overflow(notes_count, sizeof(struct note_command), &notes_size)) {
517 			COREDUMPLOG("error: note command size overflow: note=%lu", i);
518 			error = ENOMEM;
519 			goto out;
520 		}
521 
522 		if (os_add_overflow(command_size, notes_size, &command_size)) {
523 			COREDUMPLOG("error: notes overflow: notes_size=%lu", notes_size);
524 			error = ENOMEM;
525 			goto out;
526 		}
527 	}
528 
529 	if (os_add_overflow(command_size, mach_header_sz, &header_size)) {
530 		COREDUMPLOG("error: header size overflow: command_size=%lu, mach_header_sz=%lu", command_size, mach_header_sz);
531 		error = ENOMEM;
532 		goto out;
533 	}
534 
535 	if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size,
536 	    KMA_DATA | KMA_ZERO, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
537 		COREDUMPLOG("error: failed to allocate memory for header (size=%lu)", header_size);
538 		error = ENOMEM;
539 		goto out;
540 	}
541 
542 	/*
543 	 *	Set up Mach-O header.
544 	 */
545 	if (is_64) {
546 		mh64 = (struct mach_header_64 *)header;
547 		mh64->magic = MH_MAGIC_64;
548 		mh64->cputype = process_cpu_type(core_proc);
549 		mh64->cpusubtype = process_cpu_subtype(core_proc);
550 		mh64->filetype = MH_CORE;
551 		mh64->ncmds = (uint32_t)(segment_count + notes_count + thread_count);
552 		mh64->sizeofcmds = (uint32_t)command_size;
553 	} else {
554 		mh = (struct mach_header *)header;
555 		mh->magic = MH_MAGIC;
556 		mh->cputype = process_cpu_type(core_proc);
557 		mh->cpusubtype = process_cpu_subtype(core_proc);
558 		mh->filetype = MH_CORE;
559 		mh->ncmds = (uint32_t)(segment_count + notes_count + thread_count);
560 		mh->sizeofcmds = (uint32_t)command_size;
561 	}
562 
563 	hoffset = mach_header_sz;       /* offset into header */
564 	foffset = round_page(header_size);      /* offset into file */
565 	vmoffset = MACH_VM_MIN_ADDRESS;         /* offset into VM */
566 	COREDUMPLOG("mach header size: %zu", header_size);
567 
568 	/*
569 	 * We use to check for an error, here, now we try and get
570 	 * as much as we can
571 	 */
572 	COREDUMPLOG("dumping %zu segments", segment_count);
573 	while (segment_count > 0) {
574 		struct segment_command          *sc;
575 		struct segment_command_64       *sc64;
576 
577 		/*
578 		 *	Get region information for next region.
579 		 */
580 
581 		while (1) {
582 			vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
583 			if ((kret = mach_vm_region_recurse(map,
584 			    &vmoffset, &vmsize, &nesting_depth,
585 			    (vm_region_recurse_info_t)&vbr,
586 			    &vbrcount)) != KERN_SUCCESS) {
587 				break;
588 			}
589 			/*
590 			 * If we get a valid mapping back, but we're dumping
591 			 * a 32 bit process,  and it's over the allowable
592 			 * address space of a 32 bit process, it's the same
593 			 * as if mach_vm_region_recurse() failed.
594 			 */
595 			if (!(is_64) &&
596 			    (vmoffset + vmsize > VM_MAX_ADDRESS)) {
597 				kret = KERN_INVALID_ADDRESS;
598 				COREDUMPLOG("exceeded allowable region for 32-bit process");
599 				break;
600 			}
601 			if (vbr.is_submap) {
602 				nesting_depth++;
603 				continue;
604 			} else {
605 				break;
606 			}
607 		}
608 		if (kret != KERN_SUCCESS) {
609 			COREDUMPLOG("ending segment dump, kret=%d", kret);
610 			break;
611 		}
612 
613 		prot = vbr.protection;
614 		maxprot = vbr.max_protection;
615 
616 		if ((prot | maxprot) == VM_PROT_NONE) {
617 			/*
618 			 * Elide unreadable (likely reserved) segments
619 			 */
620 			COREDUMPLOG("eliding unreadable segment %llx->%llx", vmoffset, vmoffset + vmsize);
621 			vmoffset += vmsize;
622 			continue;
623 		}
624 
625 		/*
626 		 * Try as hard as possible to get read access to the data.
627 		 */
628 		if ((prot & VM_PROT_READ) == 0) {
629 			mach_vm_protect(map, vmoffset, vmsize, FALSE,
630 			    prot | VM_PROT_READ);
631 		}
632 
633 		/*
634 		 * But only try and perform the write if we can read it.
635 		 */
636 		int64_t fsize = ((maxprot & VM_PROT_READ) == VM_PROT_READ
637 		    && (include_iokit_memory || vbr.user_tag != VM_MEMORY_IOKIT)
638 		    && coredumpok(map, vmoffset)) ? vmsize : 0;
639 
640 		if (fsize) {
641 			int64_t resid = 0;
642 			const enum uio_seg sflg = IS_64BIT_PROCESS(core_proc) ?
643 			    UIO_USERSPACE64 : UIO_USERSPACE32;
644 
645 			error = vn_rdwr_64(UIO_WRITE, vp, vmoffset, fsize,
646 			    foffset, sflg, IO_NODELOCKED | IO_UNIT,
647 			    cred, &resid, core_proc);
648 
649 			if (error) {
650 				/*
651 				 * Mark segment as empty
652 				 */
653 				fsize = 0;
654 				COREDUMPLOG("failed to write segment %llx->%llx: error %d", vmoffset, vmoffset + vmsize, error);
655 			} else if (resid) {
656 				/*
657 				 * Partial write. Extend the file size so
658 				 * that the segment command contains a valid
659 				 * range of offsets, possibly creating a hole.
660 				 */
661 				VATTR_INIT(vap);
662 				VATTR_SET(vap, va_data_size, foffset + fsize);
663 				vnode_setattr(vp, vap, ctx);
664 				COREDUMPLOG("partially wrote segment %llx->%llx, resid %lld", vmoffset, vmoffset + vmsize, resid);
665 			}
666 		} else {
667 			COREDUMPLOG("skipping unreadable segment %llx->%llx", vmoffset, vmoffset + vmsize);
668 		}
669 
670 		/*
671 		 *	Fill in segment command structure.
672 		 */
673 
674 		if (is_64) {
675 			sc64 = (struct segment_command_64 *)(header + hoffset);
676 			sc64->cmd = LC_SEGMENT_64;
677 			sc64->cmdsize = sizeof(struct segment_command_64);
678 			/* segment name is zeroed by kmem_alloc */
679 			sc64->segname[0] = 0;
680 			sc64->vmaddr = vmoffset;
681 			sc64->vmsize = vmsize;
682 			sc64->fileoff = foffset;
683 			sc64->filesize = fsize;
684 			sc64->maxprot = maxprot;
685 			sc64->initprot = prot;
686 			sc64->nsects = 0;
687 			sc64->flags = 0;
688 		} else {
689 			sc = (struct segment_command *) (header + hoffset);
690 			sc->cmd = LC_SEGMENT;
691 			sc->cmdsize = sizeof(struct segment_command);
692 			/* segment name is zeroed by kmem_alloc */
693 			sc->segname[0] = 0;
694 			sc->vmaddr = CAST_DOWN_EXPLICIT(uint32_t, vmoffset);
695 			sc->vmsize = CAST_DOWN_EXPLICIT(uint32_t, vmsize);
696 			sc->fileoff = CAST_DOWN_EXPLICIT(uint32_t, foffset); /* will never truncate */
697 			sc->filesize = CAST_DOWN_EXPLICIT(uint32_t, fsize); /* will never truncate */
698 			sc->maxprot = maxprot;
699 			sc->initprot = prot;
700 			sc->nsects = 0;
701 			sc->flags = 0;
702 		}
703 
704 		hoffset += segment_command_sz;
705 		foffset += fsize;
706 		vmoffset += vmsize;
707 		segment_count--;
708 	}
709 	COREDUMPLOG("max file offset: %lld", foffset);
710 
711 	/*
712 	 * If there are remaining segments which have not been written
713 	 * out because break in the loop above, then they were not counted
714 	 * because they exceed the real address space of the executable
715 	 * type: remove them from the header's count.  This is OK, since
716 	 * we are allowed to have a sparse area following the segments.
717 	 */
718 	if (is_64) {
719 		mh64->ncmds -= segment_count;
720 		mh64->sizeofcmds -= segment_count * segment_command_sz;
721 	} else {
722 		mh->ncmds -= segment_count;
723 		mh->sizeofcmds -= segment_count * segment_command_sz;
724 	}
725 
726 	/* Add LC_NOTES */
727 	COREDUMPLOG("dumping %zu notes", notes_count);
728 	if (dump_notes(core_proc, header, hoffset, vp, foffset) != 0) {
729 		error = EFAULT;
730 		goto out;
731 	}
732 
733 	tir1.header = header;
734 	tir1.hoffset = hoffset + notes_size;
735 	tir1.flavors = flavors;
736 	tir1.tstate_size = tstate_size;
737 	COREDUMPLOG("dumping %zu threads", thread_count);
738 	task_act_iterate_wth_args(task, collectth_state, &tir1);
739 
740 	/*
741 	 *	Write out the Mach header at the beginning of the
742 	 *	file.  OK to use a 32 bit write for this.
743 	 */
744 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, (int)MIN(header_size, INT_MAX), (off_t)0,
745 	    UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) 0, core_proc);
746 	if (error != KERN_SUCCESS) {
747 		COREDUMPLOG("failed to write mach header: error %d", error);
748 	}
749 	kmem_free(kernel_map, header, header_size);
750 
751 	if ((coredump_flags & COREDUMP_FULLFSYNC) && error == 0) {
752 		error = VNOP_IOCTL(vp, F_FULLFSYNC, (caddr_t)NULL, 0, ctx);
753 		if (error != KERN_SUCCESS) {
754 			COREDUMPLOG("failed to FULLFSYNC core: error %d", error);
755 		}
756 	}
757 out:
758 	if (vap) {
759 		kfree_type(struct vnode_attr, vap);
760 	}
761 	error1 = vnode_close(vp, FWRITE, ctx);
762 	if (error1 != KERN_SUCCESS) {
763 		COREDUMPLOG("failed to close core file: error %d", error1);
764 	}
765 out2:
766 #if CONFIG_AUDIT
767 	audit_proc_coredump(core_proc, name, error);
768 #endif
769 	if (alloced_name != NULL) {
770 		zfree(ZV_NAMEI, alloced_name);
771 	}
772 	if (alloced_format != NULL) {
773 		kfree_data(alloced_format, alloced_format_len + 1);
774 	}
775 	if (custom_location_entitlement != NULL) {
776 		kfree_data(custom_location_entitlement, custom_location_entitlement_len + 1);
777 	}
778 	if (error == 0) {
779 		error = error1;
780 	}
781 
782 	if (coredump_attempted) {
783 		if (error != 0) {
784 			COREDUMPLOG("core dump failed: error %d\n", error);
785 		} else {
786 			COREDUMPLOG("core dump succeeded");
787 		}
788 	}
789 
790 	return error;
791 }
792 
793 #else /* CONFIG_COREDUMP */
794 
795 /* When core dumps aren't needed, no need to compile this file at all */
796 
797 #error assertion failed: this section is not compiled
798 
799 #endif /* CONFIG_COREDUMP */
800