xref: /xnu-8020.140.41/osfmk/ipc/ipc_kmsg.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * Copyright (c) 2000-2020 Apple 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 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58  * support for mandatory and extensible security protections.  This notice
59  * is included in support of clause 2.2 (b) of the Apple Public License,
60  * Version 2.0.
61  * Copyright (c) 2005 SPARTA, Inc.
62  */
63 /*
64  */
65 /*
66  *	File:	ipc/ipc_kmsg.c
67  *	Author:	Rich Draves
68  *	Date:	1989
69  *
70  *	Operations on kernel messages.
71  */
72 
73 
74 #include <mach/mach_types.h>
75 #include <mach/boolean.h>
76 #include <mach/kern_return.h>
77 #include <mach/message.h>
78 #include <mach/port.h>
79 #include <mach/vm_map.h>
80 #include <mach/mach_vm.h>
81 #include <mach/vm_statistics.h>
82 
83 #include <kern/kern_types.h>
84 #include <kern/assert.h>
85 #include <kern/debug.h>
86 #include <kern/ipc_kobject.h>
87 #include <kern/kalloc.h>
88 #include <kern/zalloc.h>
89 #include <kern/processor.h>
90 #include <kern/thread.h>
91 #include <kern/thread_group.h>
92 #include <kern/sched_prim.h>
93 #include <kern/misc_protos.h>
94 #include <kern/cpu_data.h>
95 #include <kern/policy_internal.h>
96 #include <kern/mach_filter.h>
97 
98 #include <pthread/priority_private.h>
99 
100 #include <machine/limits.h>
101 
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_kern.h>
105 
106 #include <ipc/port.h>
107 #include <ipc/ipc_types.h>
108 #include <ipc/ipc_entry.h>
109 #include <ipc/ipc_kmsg.h>
110 #include <ipc/ipc_notify.h>
111 #include <ipc/ipc_object.h>
112 #include <ipc/ipc_space.h>
113 #include <ipc/ipc_port.h>
114 #include <ipc/ipc_right.h>
115 #include <ipc/ipc_hash.h>
116 #include <ipc/ipc_table.h>
117 #include <ipc/ipc_importance.h>
118 #include <ipc/ipc_service_port.h>
119 
120 #if MACH_FLIPC
121 #include <kern/mach_node.h>
122 #include <ipc/flipc.h>
123 #endif
124 
125 #include <os/overflow.h>
126 
127 #include <security/mac_mach_internal.h>
128 
129 #include <device/device_server.h>
130 
131 #include <string.h>
132 
133 #if DEBUG
134 #define DEBUG_MSGS_K64 1
135 #endif
136 
137 #include <sys/kdebug.h>
138 #include <libkern/OSAtomic.h>
139 
140 #include <libkern/crypto/sha2.h>
141 
142 #include <ptrauth.h>
143 #if __has_feature(ptrauth_calls)
144 #include <libkern/ptrauth_utils.h>
145 #endif
146 
147 /*
148  * In kernel, complex mach msg have a simpler representation than userspace:
149  *
150  * <header>
151  * <desc-count>
152  * <descriptors> * desc-count
153  * <body>
154  *
155  * And the descriptors are of a fake type `mach_msg_descriptor_t`,
156  * that is large enough to accommodate for any possible representation.
157  *
158  * The `type` field of any desciptor is always at the same offset,
159  * and the smallest possible descriptor is of size MACH_MSG_DESC_MIN_SIZE.
160  *
161  * Note:
162  * - KERN_DESC_SIZE is 16 on all kernels
163  * - MACH_MSG_DESC_MIN_SIZE is 12 on all kernels
164  */
165 
166 #define KERNEL_DESC_SIZE             sizeof(mach_msg_descriptor_t)
167 #define MACH_MSG_DESC_MIN_SIZE       sizeof(mach_msg_type_descriptor_t)
168 
169 #define USER_HEADER_SIZE_DELTA \
170 	((mach_msg_size_t)(sizeof(mach_msg_header_t) - sizeof(mach_msg_user_header_t)))
171 
172 #define USER_DESC_MAX_DELTA \
173 	(KERNEL_DESC_SIZE - MACH_MSG_DESC_MIN_SIZE)
174 
175 #define mach_validate_desc_type(t) \
176 	static_assert(MACH_MSG_DESC_MIN_SIZE <= sizeof(t) && \
177 	sizeof(t) <= sizeof(mach_msg_descriptor_t))
178 
179 mach_validate_desc_type(mach_msg_descriptor_t);
180 mach_validate_desc_type(mach_msg_port_descriptor_t);
181 mach_validate_desc_type(mach_msg_user_port_descriptor_t);
182 mach_validate_desc_type(mach_msg_type_descriptor_t);
183 mach_validate_desc_type(mach_msg_ool_descriptor32_t);
184 mach_validate_desc_type(mach_msg_ool_descriptor64_t);
185 mach_validate_desc_type(mach_msg_ool_ports_descriptor32_t);
186 mach_validate_desc_type(mach_msg_ool_ports_descriptor64_t);
187 mach_validate_desc_type(mach_msg_guarded_port_descriptor32_t);
188 mach_validate_desc_type(mach_msg_guarded_port_descriptor64_t);
189 
190 
191 static inline mach_msg_size_t
ipc_kmsg_user_desc_size(mach_msg_descriptor_type_t type,bool is_task_64bit)192 ipc_kmsg_user_desc_size(
193 	mach_msg_descriptor_type_t type,
194 	bool                       is_task_64bit)
195 {
196 	if (is_task_64bit) {
197 		switch (type) {
198 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
199 		case MACH_MSG_OOL_DESCRIPTOR:
200 			return sizeof(mach_msg_ool_descriptor64_t);
201 		case MACH_MSG_OOL_PORTS_DESCRIPTOR:
202 			return sizeof(mach_msg_ool_ports_descriptor64_t);
203 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
204 			return sizeof(mach_msg_guarded_port_descriptor64_t);
205 		default: /* MACH_MSG_PORT_DESCRIPTOR */
206 			return sizeof(mach_msg_user_port_descriptor_t);
207 		}
208 	} else {
209 		switch (type) {
210 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
211 		case MACH_MSG_OOL_DESCRIPTOR:
212 			return sizeof(mach_msg_ool_descriptor32_t);
213 		case MACH_MSG_OOL_PORTS_DESCRIPTOR:
214 			return sizeof(mach_msg_ool_ports_descriptor32_t);
215 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
216 			return sizeof(mach_msg_guarded_port_descriptor32_t);
217 		default: /* MACH_MSG_PORT_DESCRIPTOR */
218 			return sizeof(mach_msg_user_port_descriptor_t);
219 		}
220 	}
221 }
222 
223 #if __has_feature(ptrauth_calls)
224 typedef uintptr_t ikm_sig_scratch_t;
225 
226 static void
ikm_init_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)227 ikm_init_sig(
228 	__unused ipc_kmsg_t kmsg,
229 	ikm_sig_scratch_t *scratchp)
230 {
231 	*scratchp = OS_PTRAUTH_DISCRIMINATOR("kmsg.ikm_signature");
232 }
233 
234 static void
ikm_chunk_sig(ipc_kmsg_t kmsg,void * data,size_t len,ikm_sig_scratch_t * scratchp)235 ikm_chunk_sig(
236 	ipc_kmsg_t kmsg,
237 	void *data,
238 	size_t len,
239 	ikm_sig_scratch_t *scratchp)
240 {
241 	int ptrauth_flags;
242 	void *trailerp;
243 
244 	/*
245 	 * if we happen to be doing the trailer chunk,
246 	 * diversify with the ptrauth-ed trailer pointer -
247 	 * as that is unchanging for the kmsg
248 	 */
249 	trailerp = (void *)
250 	    ((vm_offset_t)kmsg->ikm_header +
251 	    mach_round_msg(kmsg->ikm_header->msgh_size));
252 
253 	ptrauth_flags = (data == trailerp) ? PTRAUTH_ADDR_DIVERSIFY : 0;
254 	*scratchp = ptrauth_utils_sign_blob_generic(data, len, *scratchp, ptrauth_flags);
255 }
256 
257 static uintptr_t
ikm_finalize_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)258 ikm_finalize_sig(
259 	__unused ipc_kmsg_t kmsg,
260 	ikm_sig_scratch_t *scratchp)
261 {
262 	return *scratchp;
263 }
264 
265 #elif defined(CRYPTO_SHA2) && !defined(__x86_64__) && !defined(__arm__)
266 
267 typedef SHA256_CTX ikm_sig_scratch_t;
268 
269 static void
ikm_init_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)270 ikm_init_sig(
271 	__unused ipc_kmsg_t kmsg,
272 	ikm_sig_scratch_t *scratchp)
273 {
274 	SHA256_Init(scratchp);
275 	SHA256_Update(scratchp, &vm_kernel_addrhash_salt_ext, sizeof(uint64_t));
276 }
277 
278 static void
ikm_chunk_sig(__unused ipc_kmsg_t kmsg,void * data,size_t len,ikm_sig_scratch_t * scratchp)279 ikm_chunk_sig(
280 	__unused ipc_kmsg_t kmsg,
281 	void *data,
282 	size_t len,
283 	ikm_sig_scratch_t *scratchp)
284 {
285 	SHA256_Update(scratchp, data, len);
286 }
287 
288 static uintptr_t
ikm_finalize_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)289 ikm_finalize_sig(
290 	__unused ipc_kmsg_t kmsg,
291 	ikm_sig_scratch_t *scratchp)
292 {
293 	uintptr_t sha_digest[SHA256_DIGEST_LENGTH / sizeof(uintptr_t)];
294 
295 	SHA256_Final((uint8_t *)sha_digest, scratchp);
296 
297 	/*
298 	 * Only use one uintptr_t sized part of result for space and compat reasons.
299 	 * Truncation is better than XOR'ing the chunks together in hopes of higher
300 	 * entropy - because of its lower risk of collisions.
301 	 */
302 	return *sha_digest;
303 }
304 
305 #else
306 /* Stubbed out implementation (for __x86_64__, __arm__ for now) */
307 
308 typedef uintptr_t ikm_sig_scratch_t;
309 
310 static void
ikm_init_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)311 ikm_init_sig(
312 	__unused ipc_kmsg_t kmsg,
313 	ikm_sig_scratch_t *scratchp)
314 {
315 	*scratchp = 0;
316 }
317 
318 static void
ikm_chunk_sig(__unused ipc_kmsg_t kmsg,__unused void * data,__unused size_t len,__unused ikm_sig_scratch_t * scratchp)319 ikm_chunk_sig(
320 	__unused ipc_kmsg_t kmsg,
321 	__unused void *data,
322 	__unused size_t len,
323 	__unused ikm_sig_scratch_t *scratchp)
324 {
325 	return;
326 }
327 
328 static uintptr_t
ikm_finalize_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)329 ikm_finalize_sig(
330 	__unused ipc_kmsg_t kmsg,
331 	ikm_sig_scratch_t *scratchp)
332 {
333 	return *scratchp;
334 }
335 
336 #endif
337 
338 static void
ikm_header_sig(ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)339 ikm_header_sig(
340 	ipc_kmsg_t kmsg,
341 	ikm_sig_scratch_t *scratchp)
342 {
343 	mach_msg_size_t dsc_count;
344 	mach_msg_base_t base;
345 	boolean_t complex;
346 
347 	/* take a snapshot of the message header/body-count */
348 	base.header = *kmsg->ikm_header;
349 	complex = ((base.header.msgh_bits & MACH_MSGH_BITS_COMPLEX) != 0);
350 	if (complex) {
351 		dsc_count = ((mach_msg_body_t *)(kmsg->ikm_header + 1))->msgh_descriptor_count;
352 	} else {
353 		dsc_count = 0;
354 	}
355 	base.body.msgh_descriptor_count = dsc_count;
356 
357 	/* compute sig of a copy of the header with all varying bits masked off */
358 	base.header.msgh_bits &= MACH_MSGH_BITS_USER;
359 	base.header.msgh_bits &= ~MACH_MSGH_BITS_VOUCHER_MASK;
360 	ikm_chunk_sig(kmsg, &base, sizeof(mach_msg_base_t), scratchp);
361 }
362 
363 static void
ikm_trailer_sig(ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)364 ikm_trailer_sig(
365 	ipc_kmsg_t kmsg,
366 	ikm_sig_scratch_t *scratchp)
367 {
368 	mach_msg_max_trailer_t *trailerp;
369 
370 	/* Add sig of the trailer contents */
371 	trailerp = (mach_msg_max_trailer_t *)
372 	    ((vm_offset_t)kmsg->ikm_header +
373 	    mach_round_msg(kmsg->ikm_header->msgh_size));
374 	ikm_chunk_sig(kmsg, trailerp, sizeof(*trailerp), scratchp);
375 }
376 
377 /* Compute the signature for the body bits of a message */
378 static void
ikm_body_sig(ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)379 ikm_body_sig(
380 	ipc_kmsg_t        kmsg,
381 	ikm_sig_scratch_t *scratchp)
382 {
383 	mach_msg_descriptor_t *kern_dsc;
384 	mach_msg_size_t dsc_count;
385 	mach_msg_body_t *body;
386 	mach_msg_size_t i;
387 
388 	if ((kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) == 0) {
389 		return;
390 	}
391 	body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
392 	dsc_count = body->msgh_descriptor_count;
393 
394 	if (dsc_count == 0) {
395 		return;
396 	}
397 
398 	kern_dsc = (mach_msg_descriptor_t *) (body + 1);
399 
400 	/* Compute the signature for the whole descriptor array */
401 	ikm_chunk_sig(kmsg, kern_dsc, sizeof(*kern_dsc) * dsc_count, scratchp);
402 
403 	/* look for descriptor contents that need a signature */
404 	for (i = 0; i < dsc_count; i++) {
405 		switch (kern_dsc[i].type.type) {
406 		case MACH_MSG_PORT_DESCRIPTOR:
407 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
408 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
409 		case MACH_MSG_OOL_DESCRIPTOR:
410 			break;
411 
412 		case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
413 			mach_msg_ool_ports_descriptor_t *ports_dsc;
414 
415 			/* Compute sig for the port/object pointers */
416 			ports_dsc = (mach_msg_ool_ports_descriptor_t *)&kern_dsc[i];
417 			ikm_chunk_sig(kmsg, ports_dsc->address, ports_dsc->count * sizeof(ipc_object_t), scratchp);
418 			break;
419 		}
420 		default: {
421 			panic("ipc_kmsg_body_sig: invalid message descriptor");
422 		}
423 		}
424 	}
425 }
426 
427 static void
ikm_sign(ipc_kmsg_t kmsg)428 ikm_sign(ipc_kmsg_t kmsg)
429 {
430 	ikm_sig_scratch_t scratch;
431 	uintptr_t sig;
432 
433 	zone_require(ipc_kmsg_zone, kmsg);
434 
435 	ikm_init_sig(kmsg, &scratch);
436 
437 	ikm_header_sig(kmsg, &scratch);
438 #if IKM_PARTIAL_SIG
439 	/* save off partial signature for just header */
440 	sig = ikm_finalize_sig(kmsg, &scratch);
441 	kmsg->ikm_header_sig = sig;
442 #endif
443 
444 	ikm_trailer_sig(kmsg, &scratch);
445 #if IKM_PARTIAL_SIG
446 	/* save off partial signature for header+trailer */
447 	sig = ikm_finalize_sig(kmsg, &scratch);
448 	kmsg->ikm_headtrail_sig = sig;
449 #endif
450 
451 	ikm_body_sig(kmsg, &scratch);
452 	sig = ikm_finalize_sig(kmsg, &scratch);
453 	kmsg->ikm_signature = sig;
454 }
455 
456 unsigned int ikm_signature_failures;
457 unsigned int ikm_signature_failure_id;
458 #if (DEVELOPMENT || DEBUG)
459 unsigned int ikm_signature_panic_disable;
460 unsigned int ikm_signature_header_failures;
461 unsigned int ikm_signature_trailer_failures;
462 #endif
463 
464 static void
ikm_validate_sig(ipc_kmsg_t kmsg)465 ikm_validate_sig(
466 	ipc_kmsg_t kmsg)
467 {
468 	ikm_sig_scratch_t scratch;
469 	uintptr_t expected;
470 	uintptr_t sig;
471 	char *str;
472 
473 	zone_require(ipc_kmsg_zone, kmsg);
474 
475 	ikm_init_sig(kmsg, &scratch);
476 
477 	ikm_header_sig(kmsg, &scratch);
478 #if IKM_PARTIAL_SIG
479 	/* Do partial evaluation of just the header signature */
480 	sig = ikm_finalize_sig(kmsg, &scratch);
481 	expected = kmsg->ikm_header_sig;
482 	if (sig != expected) {
483 		ikm_signature_header_failures++;
484 		str = "header";
485 		goto failure;
486 	}
487 #endif
488 
489 	ikm_trailer_sig(kmsg, &scratch);
490 #if IKM_PARTIAL_SIG
491 	/* Do partial evaluation of header+trailer signature */
492 	sig = ikm_finalize_sig(kmsg, &scratch);
493 	expected = kmsg->ikm_headtrail_sig;
494 	if (sig != expected) {
495 		ikm_signature_trailer_failures++;
496 		str = "trailer";
497 		goto failure;
498 	}
499 #endif
500 
501 	ikm_body_sig(kmsg, &scratch);
502 	sig = ikm_finalize_sig(kmsg, &scratch);
503 
504 	expected = kmsg->ikm_signature;
505 	if (sig != expected) {
506 		ikm_signature_failures++;
507 		str = "full";
508 
509 #if IKM_PARTIAL_SIG
510 failure:
511 #endif
512 		{
513 			mach_msg_id_t id = kmsg->ikm_header->msgh_id;
514 
515 			ikm_signature_failure_id = id;
516 #if (DEVELOPMENT || DEBUG)
517 			if (ikm_signature_panic_disable) {
518 				return;
519 			}
520 #endif
521 			panic("ikm_validate_sig: %s signature mismatch: kmsg=0x%p, id=%d, sig=0x%zx (expected 0x%zx)",
522 			    str, kmsg, id, sig, expected);
523 		}
524 	}
525 }
526 
527 #if DEBUG_MSGS_K64
528 extern void ipc_pset_print64(
529 	ipc_pset_t      pset);
530 
531 extern void     ipc_kmsg_print64(
532 	ipc_kmsg_t      kmsg,
533 	const char      *str);
534 
535 extern void     ipc_msg_print64(
536 	mach_msg_header_t       *msgh);
537 
538 extern ipc_port_t ipc_name_to_data64(
539 	task_t                  task,
540 	mach_port_name_t        name);
541 
542 /*
543  * Forward declarations
544  */
545 void ipc_msg_print_untyped64(
546 	mach_msg_body_t         *body);
547 
548 const char * ipc_type_name64(
549 	int             type_name,
550 	boolean_t       received);
551 
552 void ipc_print_type_name64(
553 	int     type_name);
554 
555 const char *
556 msgh_bit_decode64(
557 	mach_msg_bits_t bit);
558 
559 const char *
560 mm_copy_options_string64(
561 	mach_msg_copy_options_t option);
562 
563 void db_print_msg_uid64(mach_msg_header_t *);
564 
565 static void
ipc_msg_body_print64(void * body,int size)566 ipc_msg_body_print64(void *body, int size)
567 {
568 	uint32_t        *word = (uint32_t *) body;
569 	uint32_t        *end  = (uint32_t *)(((uintptr_t) body) + size
570 	    - sizeof(mach_msg_header_t));
571 	int             i;
572 
573 	kprintf("  body(%p-%p):\n    %p: ", body, end, word);
574 	for (;;) {
575 		for (i = 0; i < 8; i++, word++) {
576 			if (word >= end) {
577 				kprintf("\n");
578 				return;
579 			}
580 			kprintf("%08x ", *word);
581 		}
582 		kprintf("\n    %p: ", word);
583 	}
584 }
585 
586 
587 const char *
ipc_type_name64(int type_name,boolean_t received)588 ipc_type_name64(
589 	int             type_name,
590 	boolean_t       received)
591 {
592 	switch (type_name) {
593 	case MACH_MSG_TYPE_PORT_NAME:
594 		return "port_name";
595 
596 	case MACH_MSG_TYPE_MOVE_RECEIVE:
597 		if (received) {
598 			return "port_receive";
599 		} else {
600 			return "move_receive";
601 		}
602 
603 	case MACH_MSG_TYPE_MOVE_SEND:
604 		if (received) {
605 			return "port_send";
606 		} else {
607 			return "move_send";
608 		}
609 
610 	case MACH_MSG_TYPE_MOVE_SEND_ONCE:
611 		if (received) {
612 			return "port_send_once";
613 		} else {
614 			return "move_send_once";
615 		}
616 
617 	case MACH_MSG_TYPE_COPY_SEND:
618 		return "copy_send";
619 
620 	case MACH_MSG_TYPE_MAKE_SEND:
621 		return "make_send";
622 
623 	case MACH_MSG_TYPE_MAKE_SEND_ONCE:
624 		return "make_send_once";
625 
626 	default:
627 		return (char *) 0;
628 	}
629 }
630 
631 void
ipc_print_type_name64(int type_name)632 ipc_print_type_name64(
633 	int     type_name)
634 {
635 	const char *name = ipc_type_name64(type_name, TRUE);
636 	if (name) {
637 		kprintf("%s", name);
638 	} else {
639 		kprintf("type%d", type_name);
640 	}
641 }
642 
643 /*
644  * ipc_kmsg_print64	[ debug ]
645  */
646 void
ipc_kmsg_print64(ipc_kmsg_t kmsg,const char * str)647 ipc_kmsg_print64(
648 	ipc_kmsg_t      kmsg,
649 	const char      *str)
650 {
651 	kprintf("%s kmsg=%p:\n", str, kmsg);
652 	kprintf("  next=%p, prev=%p, size=%d",
653 	    kmsg->ikm_next,
654 	    kmsg->ikm_prev,
655 	    kmsg->ikm_size);
656 	kprintf("\n");
657 	ipc_msg_print64(kmsg->ikm_header);
658 }
659 
660 const char *
msgh_bit_decode64(mach_msg_bits_t bit)661 msgh_bit_decode64(
662 	mach_msg_bits_t bit)
663 {
664 	switch (bit) {
665 	case MACH_MSGH_BITS_COMPLEX:        return "complex";
666 	case MACH_MSGH_BITS_CIRCULAR:       return "circular";
667 	default:                            return (char *) 0;
668 	}
669 }
670 
671 /*
672  * ipc_msg_print64	[ debug ]
673  */
674 void
ipc_msg_print64(mach_msg_header_t * msgh)675 ipc_msg_print64(
676 	mach_msg_header_t       *msgh)
677 {
678 	mach_msg_bits_t mbits;
679 	unsigned int    bit, i;
680 	const char      *bit_name;
681 	int             needs_comma;
682 
683 	mbits = msgh->msgh_bits;
684 	kprintf("  msgh_bits=0x%x: l=0x%x,r=0x%x\n",
685 	    mbits,
686 	    MACH_MSGH_BITS_LOCAL(msgh->msgh_bits),
687 	    MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
688 
689 	mbits = MACH_MSGH_BITS_OTHER(mbits) & MACH_MSGH_BITS_USED;
690 	kprintf("  decoded bits:  ");
691 	needs_comma = 0;
692 	for (i = 0, bit = 1; i < sizeof(mbits) * 8; ++i, bit <<= 1) {
693 		if ((mbits & bit) == 0) {
694 			continue;
695 		}
696 		bit_name = msgh_bit_decode64((mach_msg_bits_t)bit);
697 		if (bit_name) {
698 			kprintf("%s%s", needs_comma ? "," : "", bit_name);
699 		} else {
700 			kprintf("%sunknown(0x%x),", needs_comma ? "," : "", bit);
701 		}
702 		++needs_comma;
703 	}
704 	if (msgh->msgh_bits & ~MACH_MSGH_BITS_USED) {
705 		kprintf("%sunused=0x%x,", needs_comma ? "," : "",
706 		    msgh->msgh_bits & ~MACH_MSGH_BITS_USED);
707 	}
708 	kprintf("\n");
709 
710 	needs_comma = 1;
711 	if (msgh->msgh_remote_port) {
712 		kprintf("  remote=%p(", msgh->msgh_remote_port);
713 		ipc_print_type_name64(MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
714 		kprintf(")");
715 	} else {
716 		kprintf("  remote=null");
717 	}
718 
719 	if (msgh->msgh_local_port) {
720 		kprintf("%slocal=%p(", needs_comma ? "," : "",
721 		    msgh->msgh_local_port);
722 		ipc_print_type_name64(MACH_MSGH_BITS_LOCAL(msgh->msgh_bits));
723 		kprintf(")\n");
724 	} else {
725 		kprintf("local=null\n");
726 	}
727 
728 	kprintf("  msgh_id=%d, size=%d\n",
729 	    msgh->msgh_id,
730 	    msgh->msgh_size);
731 
732 	if (mbits & MACH_MSGH_BITS_COMPLEX) {
733 		ipc_msg_print_untyped64((mach_msg_body_t *) (msgh + 1));
734 	}
735 
736 	ipc_msg_body_print64((void *)(msgh + 1), msgh->msgh_size);
737 }
738 
739 
740 const char *
mm_copy_options_string64(mach_msg_copy_options_t option)741 mm_copy_options_string64(
742 	mach_msg_copy_options_t option)
743 {
744 	const char      *name;
745 
746 	switch (option) {
747 	case MACH_MSG_PHYSICAL_COPY:
748 		name = "PHYSICAL";
749 		break;
750 	case MACH_MSG_VIRTUAL_COPY:
751 		name = "VIRTUAL";
752 		break;
753 	case MACH_MSG_OVERWRITE:
754 		name = "OVERWRITE(DEPRECATED)";
755 		break;
756 	case MACH_MSG_ALLOCATE:
757 		name = "ALLOCATE";
758 		break;
759 	case MACH_MSG_KALLOC_COPY_T:
760 		name = "KALLOC_COPY_T";
761 		break;
762 	default:
763 		name = "unknown";
764 		break;
765 	}
766 	return name;
767 }
768 
769 void
ipc_msg_print_untyped64(mach_msg_body_t * body)770 ipc_msg_print_untyped64(
771 	mach_msg_body_t         *body)
772 {
773 	mach_msg_descriptor_t       *saddr, *send;
774 	mach_msg_descriptor_type_t  type;
775 
776 	kprintf("  %d descriptors: \n", body->msgh_descriptor_count);
777 
778 	saddr = (mach_msg_descriptor_t *) (body + 1);
779 	send = saddr + body->msgh_descriptor_count;
780 
781 	for (; saddr < send; saddr++) {
782 		type = saddr->type.type;
783 
784 		switch (type) {
785 		case MACH_MSG_PORT_DESCRIPTOR: {
786 			mach_msg_port_descriptor_t *dsc;
787 
788 			dsc = &saddr->port;
789 			kprintf("    PORT name = %p disp = ", dsc->name);
790 			ipc_print_type_name64(dsc->disposition);
791 			kprintf("\n");
792 			break;
793 		}
794 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
795 		case MACH_MSG_OOL_DESCRIPTOR: {
796 			mach_msg_ool_descriptor_t *dsc;
797 
798 			dsc = (mach_msg_ool_descriptor_t *) &saddr->out_of_line;
799 			kprintf("    OOL%s addr = %p size = 0x%x copy = %s %s\n",
800 			    type == MACH_MSG_OOL_DESCRIPTOR ? "" : " VOLATILE",
801 			    dsc->address, dsc->size,
802 			    mm_copy_options_string64(dsc->copy),
803 			    dsc->deallocate ? "DEALLOC" : "");
804 			break;
805 		}
806 		case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
807 			mach_msg_ool_ports_descriptor_t *dsc;
808 
809 			dsc = (mach_msg_ool_ports_descriptor_t *) &saddr->ool_ports;
810 
811 			kprintf("    OOL_PORTS addr = %p count = 0x%x ",
812 			    dsc->address, dsc->count);
813 			kprintf("disp = ");
814 			ipc_print_type_name64(dsc->disposition);
815 			kprintf(" copy = %s %s\n",
816 			    mm_copy_options_string64(dsc->copy),
817 			    dsc->deallocate ? "DEALLOC" : "");
818 			break;
819 		}
820 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
821 			mach_msg_guarded_port_descriptor_t *dsc;
822 
823 			dsc = (mach_msg_guarded_port_descriptor_t *)&saddr->guarded_port;
824 			kprintf("    GUARDED_PORT name = %p flags = 0x%x disp = ", dsc->name, dsc->flags);
825 			ipc_print_type_name64(dsc->disposition);
826 			kprintf("\n");
827 			break;
828 		}
829 		default: {
830 			kprintf("    UNKNOWN DESCRIPTOR 0x%x\n", type);
831 			break;
832 		}
833 		}
834 	}
835 }
836 
837 #define DEBUG_IPC_KMSG_PRINT(kmsg, string)       \
838 	__unreachable_ok_push   \
839 	if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {    \
840 	        ipc_kmsg_print64(kmsg, string); \
841 	}       \
842 	__unreachable_ok_pop
843 
844 #define DEBUG_IPC_MSG_BODY_PRINT(body, size)     \
845 	__unreachable_ok_push   \
846 	if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {    \
847 	        ipc_msg_body_print64(body,size);\
848 	}       \
849 	__unreachable_ok_pop
850 #else /* !DEBUG_MSGS_K64 */
851 #define DEBUG_IPC_KMSG_PRINT(kmsg, string)
852 #define DEBUG_IPC_MSG_BODY_PRINT(body, size)
853 #endif  /* !DEBUG_MSGS_K64 */
854 
855 extern vm_map_t         ipc_kernel_copy_map;
856 extern vm_size_t        ipc_kmsg_max_space;
857 extern const vm_size_t  ipc_kmsg_max_vm_space;
858 extern const vm_size_t  ipc_kmsg_max_body_space;
859 extern const vm_size_t  msg_ool_size_small;
860 
861 #define MSG_OOL_SIZE_SMALL      msg_ool_size_small
862 
863 #define KMSG_TRACE_FLAG_TRACED     0x000001
864 #define KMSG_TRACE_FLAG_COMPLEX    0x000002
865 #define KMSG_TRACE_FLAG_OOLMEM     0x000004
866 #define KMSG_TRACE_FLAG_VCPY       0x000008
867 #define KMSG_TRACE_FLAG_PCPY       0x000010
868 #define KMSG_TRACE_FLAG_SND64      0x000020
869 #define KMSG_TRACE_FLAG_RAISEIMP   0x000040
870 #define KMSG_TRACE_FLAG_APP_SRC    0x000080
871 #define KMSG_TRACE_FLAG_APP_DST    0x000100
872 #define KMSG_TRACE_FLAG_DAEMON_SRC 0x000200
873 #define KMSG_TRACE_FLAG_DAEMON_DST 0x000400
874 #define KMSG_TRACE_FLAG_DST_NDFLTQ 0x000800
875 #define KMSG_TRACE_FLAG_SRC_NDFLTQ 0x001000
876 #define KMSG_TRACE_FLAG_DST_SONCE  0x002000
877 #define KMSG_TRACE_FLAG_SRC_SONCE  0x004000
878 #define KMSG_TRACE_FLAG_CHECKIN    0x008000
879 #define KMSG_TRACE_FLAG_ONEWAY     0x010000
880 #define KMSG_TRACE_FLAG_IOKIT      0x020000
881 #define KMSG_TRACE_FLAG_SNDRCV     0x040000
882 #define KMSG_TRACE_FLAG_DSTQFULL   0x080000
883 #define KMSG_TRACE_FLAG_VOUCHER    0x100000
884 #define KMSG_TRACE_FLAG_TIMER      0x200000
885 #define KMSG_TRACE_FLAG_SEMA       0x400000
886 #define KMSG_TRACE_FLAG_DTMPOWNER  0x800000
887 #define KMSG_TRACE_FLAG_GUARDED_DESC 0x1000000
888 
889 #define KMSG_TRACE_FLAGS_MASK      0x1ffffff
890 #define KMSG_TRACE_FLAGS_SHIFT     8
891 
892 #define KMSG_TRACE_PORTS_MASK      0xff
893 #define KMSG_TRACE_PORTS_SHIFT     0
894 
895 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
896 #include <stdint.h>
897 
898 void
ipc_kmsg_trace_send(ipc_kmsg_t kmsg,mach_msg_option_t option)899 ipc_kmsg_trace_send(ipc_kmsg_t kmsg,
900     mach_msg_option_t option)
901 {
902 	task_t send_task = TASK_NULL;
903 	ipc_port_t dst_port, src_port;
904 	boolean_t is_task_64bit;
905 	mach_msg_header_t *msg;
906 	mach_msg_trailer_t *trailer;
907 
908 	int kotype = 0;
909 	uint32_t msg_size = 0;
910 	uint64_t msg_flags = KMSG_TRACE_FLAG_TRACED;
911 	uint32_t num_ports = 0;
912 	uint32_t send_pid, dst_pid;
913 
914 	/*
915 	 * check to see not only if ktracing is enabled, but if we will
916 	 * _actually_ emit the KMSG_INFO tracepoint. This saves us a
917 	 * significant amount of processing (and a port lock hold) in
918 	 * the non-tracing case.
919 	 */
920 	if (__probable((kdebug_enable & KDEBUG_TRACE) == 0)) {
921 		return;
922 	}
923 	if (!kdebug_debugid_enabled(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO))) {
924 		return;
925 	}
926 
927 	msg = kmsg->ikm_header;
928 
929 	dst_port = msg->msgh_remote_port;
930 	if (!IPC_PORT_VALID(dst_port)) {
931 		return;
932 	}
933 
934 	/*
935 	 * Message properties / options
936 	 */
937 	if ((option & (MACH_SEND_MSG | MACH_RCV_MSG)) == (MACH_SEND_MSG | MACH_RCV_MSG)) {
938 		msg_flags |= KMSG_TRACE_FLAG_SNDRCV;
939 	}
940 
941 	if (msg->msgh_id >= is_iokit_subsystem.start &&
942 	    msg->msgh_id < is_iokit_subsystem.end + 100) {
943 		msg_flags |= KMSG_TRACE_FLAG_IOKIT;
944 	}
945 	/* magic XPC checkin message id (XPC_MESSAGE_ID_CHECKIN) from libxpc */
946 	else if (msg->msgh_id == 0x77303074u /* w00t */) {
947 		msg_flags |= KMSG_TRACE_FLAG_CHECKIN;
948 	}
949 
950 	if (msg->msgh_bits & MACH_MSGH_BITS_RAISEIMP) {
951 		msg_flags |= KMSG_TRACE_FLAG_RAISEIMP;
952 	}
953 
954 	if (unsafe_convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg))) {
955 		msg_flags |= KMSG_TRACE_FLAG_VOUCHER;
956 	}
957 
958 	/*
959 	 * Sending task / port
960 	 */
961 	send_task = current_task();
962 	send_pid = task_pid(send_task);
963 
964 	if (send_pid != 0) {
965 		if (task_is_daemon(send_task)) {
966 			msg_flags |= KMSG_TRACE_FLAG_DAEMON_SRC;
967 		} else if (task_is_app(send_task)) {
968 			msg_flags |= KMSG_TRACE_FLAG_APP_SRC;
969 		}
970 	}
971 
972 	is_task_64bit = (send_task->map->max_offset > VM_MAX_ADDRESS);
973 	if (is_task_64bit) {
974 		msg_flags |= KMSG_TRACE_FLAG_SND64;
975 	}
976 
977 	src_port = msg->msgh_local_port;
978 	if (src_port) {
979 		if (src_port->ip_messages.imq_qlimit != MACH_PORT_QLIMIT_DEFAULT) {
980 			msg_flags |= KMSG_TRACE_FLAG_SRC_NDFLTQ;
981 		}
982 		switch (MACH_MSGH_BITS_LOCAL(msg->msgh_bits)) {
983 		case MACH_MSG_TYPE_MOVE_SEND_ONCE:
984 			msg_flags |= KMSG_TRACE_FLAG_SRC_SONCE;
985 			break;
986 		default:
987 			break;
988 		}
989 	} else {
990 		msg_flags |= KMSG_TRACE_FLAG_ONEWAY;
991 	}
992 
993 
994 	/*
995 	 * Destination task / port
996 	 */
997 	ip_mq_lock(dst_port);
998 	if (!ip_active(dst_port)) {
999 		/* dst port is being torn down */
1000 		dst_pid = (uint32_t)0xfffffff0;
1001 	} else if (dst_port->ip_tempowner) {
1002 		msg_flags |= KMSG_TRACE_FLAG_DTMPOWNER;
1003 		if (IIT_NULL != ip_get_imp_task(dst_port)) {
1004 			dst_pid = task_pid(dst_port->ip_imp_task->iit_task);
1005 		} else {
1006 			dst_pid = (uint32_t)0xfffffff1;
1007 		}
1008 	} else if (!ip_in_a_space(dst_port)) {
1009 		/* dst_port is otherwise in-transit */
1010 		dst_pid = (uint32_t)0xfffffff2;
1011 	} else {
1012 		if (ip_in_space(dst_port, ipc_space_kernel)) {
1013 			dst_pid = 0;
1014 		} else {
1015 			ipc_space_t dst_space;
1016 			dst_space = ip_get_receiver(dst_port);
1017 			if (dst_space && is_active(dst_space)) {
1018 				dst_pid = task_pid(dst_space->is_task);
1019 				if (task_is_daemon(dst_space->is_task)) {
1020 					msg_flags |= KMSG_TRACE_FLAG_DAEMON_DST;
1021 				} else if (task_is_app(dst_space->is_task)) {
1022 					msg_flags |= KMSG_TRACE_FLAG_APP_DST;
1023 				}
1024 			} else {
1025 				/* receiving task is being torn down */
1026 				dst_pid = (uint32_t)0xfffffff3;
1027 			}
1028 		}
1029 	}
1030 
1031 	if (dst_port->ip_messages.imq_qlimit != MACH_PORT_QLIMIT_DEFAULT) {
1032 		msg_flags |= KMSG_TRACE_FLAG_DST_NDFLTQ;
1033 	}
1034 	if (imq_full(&dst_port->ip_messages)) {
1035 		msg_flags |= KMSG_TRACE_FLAG_DSTQFULL;
1036 	}
1037 
1038 	kotype = ip_kotype(dst_port);
1039 
1040 	ip_mq_unlock(dst_port);
1041 
1042 	switch (kotype) {
1043 	case IKOT_SEMAPHORE:
1044 		msg_flags |= KMSG_TRACE_FLAG_SEMA;
1045 		break;
1046 	case IKOT_TIMER:
1047 	case IKOT_CLOCK:
1048 		msg_flags |= KMSG_TRACE_FLAG_TIMER;
1049 		break;
1050 	case IKOT_MASTER_DEVICE:
1051 	case IKOT_IOKIT_CONNECT:
1052 	case IKOT_IOKIT_OBJECT:
1053 	case IKOT_IOKIT_IDENT:
1054 	case IKOT_UEXT_OBJECT:
1055 		msg_flags |= KMSG_TRACE_FLAG_IOKIT;
1056 		break;
1057 	default:
1058 		break;
1059 	}
1060 
1061 	switch (MACH_MSGH_BITS_REMOTE(msg->msgh_bits)) {
1062 	case MACH_MSG_TYPE_PORT_SEND_ONCE:
1063 		msg_flags |= KMSG_TRACE_FLAG_DST_SONCE;
1064 		break;
1065 	default:
1066 		break;
1067 	}
1068 
1069 
1070 	/*
1071 	 * Message size / content
1072 	 */
1073 	msg_size = msg->msgh_size - sizeof(mach_msg_header_t);
1074 
1075 	if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
1076 		mach_msg_body_t *msg_body;
1077 		mach_msg_descriptor_t *kern_dsc;
1078 		mach_msg_size_t dsc_count;
1079 
1080 		msg_flags |= KMSG_TRACE_FLAG_COMPLEX;
1081 
1082 		msg_body = (mach_msg_body_t *)(kmsg->ikm_header + 1);
1083 		dsc_count = msg_body->msgh_descriptor_count;
1084 		kern_dsc = (mach_msg_descriptor_t *)(msg_body + 1);
1085 
1086 		for (mach_msg_size_t i = 0; i < dsc_count; i++) {
1087 			switch (kern_dsc[i].type.type) {
1088 			case MACH_MSG_PORT_DESCRIPTOR:
1089 				num_ports++;
1090 				break;
1091 			case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
1092 			case MACH_MSG_OOL_DESCRIPTOR: {
1093 				mach_msg_ool_descriptor_t *dsc;
1094 				dsc = (mach_msg_ool_descriptor_t *)&kern_dsc[i];
1095 				msg_flags |= KMSG_TRACE_FLAG_OOLMEM;
1096 				msg_size += dsc->size;
1097 				if (dsc->size > MSG_OOL_SIZE_SMALL &&
1098 				    (dsc->copy == MACH_MSG_PHYSICAL_COPY) &&
1099 				    !dsc->deallocate) {
1100 					msg_flags |= KMSG_TRACE_FLAG_PCPY;
1101 				} else if (dsc->size <= MSG_OOL_SIZE_SMALL) {
1102 					msg_flags |= KMSG_TRACE_FLAG_PCPY;
1103 				} else {
1104 					msg_flags |= KMSG_TRACE_FLAG_VCPY;
1105 				}
1106 			} break;
1107 			case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
1108 				mach_msg_ool_ports_descriptor_t *dsc;
1109 				dsc = (mach_msg_ool_ports_descriptor_t *)&kern_dsc[i];
1110 				num_ports += dsc->count;
1111 			} break;
1112 			case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
1113 				num_ports++;
1114 				msg_flags |= KMSG_TRACE_FLAG_GUARDED_DESC;
1115 				break;
1116 			default:
1117 				break;
1118 			}
1119 			msg_size -= ipc_kmsg_user_desc_size(kern_dsc[i].type.type, is_task_64bit);
1120 		}
1121 	}
1122 
1123 	/*
1124 	 * Trailer contents
1125 	 */
1126 	trailer = (mach_msg_trailer_t *)((vm_offset_t)msg +
1127 	    (vm_offset_t)mach_round_msg(msg->msgh_size));
1128 	if (trailer->msgh_trailer_size <= sizeof(mach_msg_security_trailer_t)) {
1129 		mach_msg_security_trailer_t *strailer;
1130 		strailer = (mach_msg_security_trailer_t *)trailer;
1131 		/*
1132 		 * verify the sender PID: replies from the kernel often look
1133 		 * like self-talk because the sending port is not reset.
1134 		 */
1135 		if (memcmp(&strailer->msgh_sender,
1136 		    &KERNEL_SECURITY_TOKEN,
1137 		    sizeof(KERNEL_SECURITY_TOKEN)) == 0) {
1138 			send_pid = 0;
1139 			msg_flags &= ~(KMSG_TRACE_FLAG_APP_SRC | KMSG_TRACE_FLAG_DAEMON_SRC);
1140 		}
1141 	}
1142 
1143 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END,
1144 	    (uintptr_t)send_pid,
1145 	    (uintptr_t)dst_pid,
1146 	    (uintptr_t)msg_size,
1147 	    (uintptr_t)(
1148 		    ((msg_flags & KMSG_TRACE_FLAGS_MASK) << KMSG_TRACE_FLAGS_SHIFT) |
1149 		    ((num_ports & KMSG_TRACE_PORTS_MASK) << KMSG_TRACE_PORTS_SHIFT)
1150 		    )
1151 	    );
1152 }
1153 #endif
1154 
1155 /* zone for cached ipc_kmsg_t structures */
1156 ZONE_DEFINE(ipc_kmsg_zone, "ipc kmsgs", IKM_SAVED_KMSG_SIZE,
1157     ZC_CACHING | ZC_ZFREE_CLEARMEM);
1158 static TUNABLE(bool, enforce_strict_reply, "ipc_strict_reply", false);
1159 
1160 /*
1161  * Forward declarations
1162  */
1163 
1164 static void ipc_kmsg_clean(
1165 	ipc_kmsg_t      kmsg);
1166 
1167 static void
1168 ipc_kmsg_link_reply_context_locked(
1169 	ipc_port_t reply_port,
1170 	ipc_port_t voucher_port);
1171 
1172 static kern_return_t
1173 ipc_kmsg_validate_reply_port_locked(
1174 	ipc_port_t reply_port,
1175 	mach_msg_option_t options);
1176 
1177 static mach_msg_return_t
1178 ipc_kmsg_validate_reply_context_locked(
1179 	mach_msg_option_t option,
1180 	ipc_port_t dest_port,
1181 	ipc_voucher_t voucher,
1182 	mach_port_name_t voucher_name);
1183 
1184 /* we can't include the BSD <sys/persona.h> header here... */
1185 #ifndef PERSONA_ID_NONE
1186 #define PERSONA_ID_NONE ((uint32_t)-1)
1187 #endif
1188 
1189 /*
1190  *	We keep a per-processor cache of kernel message buffers.
1191  *	The cache saves the overhead/locking of using kalloc/kfree.
1192  *	The per-processor cache seems to miss less than a per-thread cache,
1193  *	and it also uses less memory.  Access to the cache doesn't
1194  *	require locking.
1195  */
1196 
1197 /*
1198  *	Routine:	ikm_set_header
1199  *	Purpose:
1200  *		Set the header (and data) pointers for a message. If the
1201  *		message is small, the data pointer is NULL and all the
1202  *		data resides within the fixed
1203  *		the cache, that is best.  Otherwise, allocate a new one.
1204  *	Conditions:
1205  *		Nothing locked.
1206  */
1207 static void
ikm_set_header(ipc_kmsg_t kmsg,void * data,mach_msg_size_t size)1208 ikm_set_header(
1209 	ipc_kmsg_t kmsg,
1210 	void *data,
1211 	mach_msg_size_t size)
1212 {
1213 	mach_msg_size_t mtsize = size + MAX_TRAILER_SIZE;
1214 	if (data) {
1215 		kmsg->ikm_data = data;
1216 		kmsg->ikm_header = (mach_msg_header_t *)((uintptr_t)data + kmsg->ikm_size - mtsize);
1217 	} else {
1218 		assert(kmsg->ikm_size == IKM_SAVED_MSG_SIZE);
1219 		kmsg->ikm_header = (mach_msg_header_t *)(vm_offset_t)
1220 		    (kmsg->ikm_inline_data + kmsg->ikm_size - mtsize);
1221 	}
1222 }
1223 
1224 /*
1225  *	Routine:	ipc_kmsg_alloc
1226  *	Purpose:
1227  *		Allocate a kernel message structure.  If we can get one from
1228  *		the cache, that is best.  Otherwise, allocate a new one.
1229  *
1230  *		The passed in size doesn't take the trailer or descriptor
1231  *		inflation into account, but already accounts for the mach
1232  *		message header expansion.
1233  *
1234  *	Conditions:
1235  *		Nothing locked.
1236  */
1237 ipc_kmsg_t
ipc_kmsg_alloc(mach_msg_size_t size,mach_msg_size_t user_descs,ipc_kmsg_alloc_flags_t flags)1238 ipc_kmsg_alloc(
1239 	mach_msg_size_t         size,
1240 	mach_msg_size_t         user_descs,
1241 	ipc_kmsg_alloc_flags_t  flags)
1242 {
1243 	mach_msg_size_t max_size, max_delta;
1244 	ipc_kmsg_t kmsg;
1245 	void *data;
1246 
1247 	/*
1248 	 * In kernel descriptors, are of the same size (KERNEL_DESC_SIZE),
1249 	 * but in userspace, depending on 64-bitness, descriptors might be
1250 	 * smaller.
1251 	 *
1252 	 * When handling a userspace message however, we know how many
1253 	 * descriptors have been declared, and we pad for the maximum expansion.
1254 	 *
1255 	 * The expansion space is left in front of the header,
1256 	 * because it is easier to pull the header and descriptors
1257 	 * forward as we process them than it is to push all the
1258 	 * data backwards.
1259 	 */
1260 	if (flags & IPC_KMSG_ALLOC_KERNEL) {
1261 		assert(user_descs == 0);
1262 		max_delta = 0;
1263 	} else if (os_mul_overflow(user_descs, USER_DESC_MAX_DELTA, &max_delta)) {
1264 		return IKM_NULL;
1265 	}
1266 
1267 	if (os_add3_overflow(size, MAX_TRAILER_SIZE, max_delta, &max_size)) {
1268 		return IKM_NULL;
1269 	}
1270 
1271 	if (max_size <= IKM_SAVED_MSG_SIZE) {
1272 		/* Fits in the fast kmsg zone */
1273 		data = NULL;
1274 		max_size = IKM_SAVED_MSG_SIZE;
1275 	} else if (flags & IPC_KMSG_ALLOC_SAVED) {
1276 		panic("size too large for the fast kmsg zone (%d)", size);
1277 	} else {
1278 		zalloc_flags_t alloc_flags = Z_WAITOK;
1279 		if (flags & IPC_KMSG_ALLOC_ZERO) {
1280 			alloc_flags |= Z_ZERO;
1281 		}
1282 		if (flags & IPC_KMSG_ALLOC_NOFAIL) {
1283 			alloc_flags |= Z_NOFAIL;
1284 		}
1285 		data = kalloc_data(max_size, alloc_flags);
1286 		if (data == NULL) {
1287 			return IKM_NULL;
1288 		}
1289 	}
1290 
1291 	kmsg = zalloc_flags(ipc_kmsg_zone, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1292 	kmsg->ikm_size = max_size;
1293 	ikm_set_header(kmsg, data, size);
1294 	return kmsg;
1295 }
1296 
1297 /* re-export for IOKit's c++ */
1298 extern ipc_kmsg_t ipc_kmsg_alloc_uext_reply(mach_msg_size_t);
1299 
1300 ipc_kmsg_t
ipc_kmsg_alloc_uext_reply(mach_msg_size_t size)1301 ipc_kmsg_alloc_uext_reply(
1302 	mach_msg_size_t         size)
1303 {
1304 	return ipc_kmsg_alloc(size, 0,
1305 	           IPC_KMSG_ALLOC_KERNEL | IPC_KMSG_ALLOC_ZERO | IPC_KMSG_ALLOC_NOFAIL);
1306 }
1307 
1308 
1309 /*
1310  *	Routine:	ipc_kmsg_free
1311  *	Purpose:
1312  *		Free a kernel message buffer.  If the kms is preallocated
1313  *		to a port, just "put it back (marked unused)."  We have to
1314  *		do this with the port locked.  The port may have its hold
1315  *		on our message released.  In that case, we have to just
1316  *		revert the message to a traditional one and free it normally.
1317  *	Conditions:
1318  *		Nothing locked.
1319  */
1320 
1321 void
ipc_kmsg_free(ipc_kmsg_t kmsg)1322 ipc_kmsg_free(
1323 	ipc_kmsg_t      kmsg)
1324 {
1325 	mach_msg_size_t size = kmsg->ikm_size;
1326 	ipc_port_t port;
1327 	void *data;
1328 
1329 	assert(!IP_VALID(ipc_kmsg_get_voucher_port(kmsg)));
1330 
1331 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_FREE) | DBG_FUNC_NONE,
1332 	    VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
1333 	    0, 0, 0, 0);
1334 
1335 	if (size == IKM_SAVED_MSG_SIZE) {
1336 		data = kmsg->ikm_inline_data;
1337 	} else {
1338 		data = kmsg->ikm_data;
1339 	}
1340 	if ((void *)kmsg->ikm_header < data ||
1341 	    (void *)kmsg->ikm_header >= (void *)((uintptr_t)data + size)) {
1342 		panic("ipc_kmsg_free");
1343 	}
1344 
1345 	/*
1346 	 * Check to see if the message is bound to the port.
1347 	 * If so, mark it not in use.
1348 	 */
1349 	if (size == IKM_SAVED_MSG_SIZE) {
1350 		port = ikm_prealloc_inuse_port(kmsg);
1351 		if (port != IP_NULL) {
1352 			ip_mq_lock(port);
1353 			ikm_prealloc_clear_inuse(kmsg, port);
1354 			assert(port->ip_premsg == kmsg);
1355 			assert(IP_PREALLOC(port));
1356 			ip_mq_unlock(port);
1357 			ip_release(port); /* May be last reference */
1358 			return;
1359 		}
1360 	} else {
1361 		kfree_data(data, size);
1362 	}
1363 	zfree(ipc_kmsg_zone, kmsg);
1364 }
1365 
1366 
1367 /*
1368  *	Routine:	ipc_kmsg_enqueue
1369  *	Purpose:
1370  *		Enqueue a kmsg.
1371  */
1372 
1373 void
ipc_kmsg_enqueue(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg)1374 ipc_kmsg_enqueue(
1375 	ipc_kmsg_queue_t        queue,
1376 	ipc_kmsg_t              kmsg)
1377 {
1378 	ipc_kmsg_t first = queue->ikmq_base;
1379 	ipc_kmsg_t last;
1380 
1381 	if (first == IKM_NULL) {
1382 		queue->ikmq_base = kmsg;
1383 		kmsg->ikm_next = kmsg;
1384 		kmsg->ikm_prev = kmsg;
1385 	} else {
1386 		last = first->ikm_prev;
1387 		kmsg->ikm_next = first;
1388 		kmsg->ikm_prev = last;
1389 		first->ikm_prev = kmsg;
1390 		last->ikm_next = kmsg;
1391 	}
1392 }
1393 
1394 /*
1395  *	Routine:	ipc_kmsg_enqueue_qos
1396  *	Purpose:
1397  *		Enqueue a kmsg, propagating qos
1398  *		overrides towards the head of the queue.
1399  *
1400  *	Returns:
1401  *		whether the head of the queue had
1402  *		it's override-qos adjusted because
1403  *		of this insertion.
1404  */
1405 
1406 boolean_t
ipc_kmsg_enqueue_qos(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg)1407 ipc_kmsg_enqueue_qos(
1408 	ipc_kmsg_queue_t        queue,
1409 	ipc_kmsg_t              kmsg)
1410 {
1411 	ipc_kmsg_t first = queue->ikmq_base;
1412 	ipc_kmsg_t prev;
1413 	mach_msg_qos_t qos_ovr;
1414 
1415 	if (first == IKM_NULL) {
1416 		/* insert a first message */
1417 		queue->ikmq_base = kmsg;
1418 		kmsg->ikm_next = kmsg;
1419 		kmsg->ikm_prev = kmsg;
1420 		return TRUE;
1421 	}
1422 
1423 	/* insert at the tail */
1424 	prev = first->ikm_prev;
1425 	kmsg->ikm_next = first;
1426 	kmsg->ikm_prev = prev;
1427 	first->ikm_prev = kmsg;
1428 	prev->ikm_next = kmsg;
1429 
1430 	/* apply QoS overrides towards the head */
1431 	qos_ovr = kmsg->ikm_qos_override;
1432 	while (prev != kmsg &&
1433 	    qos_ovr > prev->ikm_qos_override) {
1434 		prev->ikm_qos_override = qos_ovr;
1435 		prev = prev->ikm_prev;
1436 	}
1437 
1438 	/* did we adjust everything? */
1439 	return prev == kmsg;
1440 }
1441 
1442 /*
1443  *	Routine:	ipc_kmsg_override_qos
1444  *	Purpose:
1445  *		Update the override for a given kmsg already
1446  *		enqueued, propagating qos override adjustments
1447  *		towards	the head of the queue.
1448  *
1449  *	Returns:
1450  *		whether the head of the queue had
1451  *		it's override-qos adjusted because
1452  *		of this insertion.
1453  */
1454 
1455 boolean_t
ipc_kmsg_override_qos(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg,mach_msg_qos_t qos_ovr)1456 ipc_kmsg_override_qos(
1457 	ipc_kmsg_queue_t    queue,
1458 	ipc_kmsg_t          kmsg,
1459 	mach_msg_qos_t      qos_ovr)
1460 {
1461 	ipc_kmsg_t first = queue->ikmq_base;
1462 	ipc_kmsg_t cur = kmsg;
1463 
1464 	/* apply QoS overrides towards the head */
1465 	while (qos_ovr > cur->ikm_qos_override) {
1466 		cur->ikm_qos_override = qos_ovr;
1467 		if (cur == first) {
1468 			return TRUE;
1469 		}
1470 		cur = cur->ikm_prev;
1471 	}
1472 	return FALSE;
1473 }
1474 
1475 /*
1476  *	Routine:	ipc_kmsg_dequeue
1477  *	Purpose:
1478  *		Dequeue and return a kmsg.
1479  */
1480 
1481 ipc_kmsg_t
ipc_kmsg_dequeue(ipc_kmsg_queue_t queue)1482 ipc_kmsg_dequeue(
1483 	ipc_kmsg_queue_t        queue)
1484 {
1485 	ipc_kmsg_t first;
1486 
1487 	first = ipc_kmsg_queue_first(queue);
1488 
1489 	if (first != IKM_NULL) {
1490 		ipc_kmsg_rmqueue(queue, first);
1491 	}
1492 
1493 	return first;
1494 }
1495 
1496 /*
1497  *	Routine:	ipc_kmsg_rmqueue
1498  *	Purpose:
1499  *		Pull a kmsg out of a queue.
1500  */
1501 
1502 void
ipc_kmsg_rmqueue(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg)1503 ipc_kmsg_rmqueue(
1504 	ipc_kmsg_queue_t        queue,
1505 	ipc_kmsg_t              kmsg)
1506 {
1507 	ipc_kmsg_t next, prev;
1508 
1509 	assert(queue->ikmq_base != IKM_NULL);
1510 
1511 	next = kmsg->ikm_next;
1512 	prev = kmsg->ikm_prev;
1513 
1514 	if (next == kmsg) {
1515 		assert(prev == kmsg);
1516 		assert(queue->ikmq_base == kmsg);
1517 
1518 		queue->ikmq_base = IKM_NULL;
1519 	} else {
1520 		if (__improbable(next->ikm_prev != kmsg || prev->ikm_next != kmsg)) {
1521 			panic("ipc_kmsg_rmqueue: inconsistent prev/next pointers. "
1522 			    "(prev->next: %p, next->prev: %p, kmsg: %p)",
1523 			    prev->ikm_next, next->ikm_prev, kmsg);
1524 		}
1525 
1526 		if (queue->ikmq_base == kmsg) {
1527 			queue->ikmq_base = next;
1528 		}
1529 
1530 		next->ikm_prev = prev;
1531 		prev->ikm_next = next;
1532 	}
1533 }
1534 
1535 /*
1536  *	Routine:	ipc_kmsg_queue_next
1537  *	Purpose:
1538  *		Return the kmsg following the given kmsg.
1539  *		(Or IKM_NULL if it is the last one in the queue.)
1540  */
1541 
1542 ipc_kmsg_t
ipc_kmsg_queue_next(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg)1543 ipc_kmsg_queue_next(
1544 	ipc_kmsg_queue_t        queue,
1545 	ipc_kmsg_t              kmsg)
1546 {
1547 	ipc_kmsg_t next;
1548 
1549 	assert(queue->ikmq_base != IKM_NULL);
1550 
1551 	next = kmsg->ikm_next;
1552 	if (queue->ikmq_base == next) {
1553 		next = IKM_NULL;
1554 	}
1555 
1556 	return next;
1557 }
1558 
1559 /*
1560  *	Routine:	ipc_kmsg_destroy
1561  *	Purpose:
1562  *		Destroys a kernel message.  Releases all rights,
1563  *		references, and memory held by the message.
1564  *		Frees the message.
1565  *	Conditions:
1566  *		No locks held.
1567  */
1568 
1569 void
ipc_kmsg_destroy(ipc_kmsg_t kmsg,ipc_kmsg_destroy_flags_t flags)1570 ipc_kmsg_destroy(
1571 	ipc_kmsg_t                     kmsg,
1572 	ipc_kmsg_destroy_flags_t       flags)
1573 {
1574 	/* sign the msg if it has not been signed */
1575 	boolean_t sign_msg = (flags & IPC_KMSG_DESTROY_NOT_SIGNED);
1576 
1577 	if (flags & IPC_KMSG_DESTROY_SKIP_REMOTE) {
1578 		kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
1579 		/* re-sign the msg since content changed */
1580 		sign_msg = true;
1581 	}
1582 
1583 	if (flags & IPC_KMSG_DESTROY_SKIP_LOCAL) {
1584 		kmsg->ikm_header->msgh_local_port = MACH_PORT_NULL;
1585 		/* re-sign the msg since content changed */
1586 		sign_msg = true;
1587 	}
1588 
1589 	if (sign_msg) {
1590 		ikm_sign(kmsg);
1591 	}
1592 
1593 	/*
1594 	 *	Destroying a message can cause more messages to be destroyed.
1595 	 *	Curtail recursion by putting messages on the deferred
1596 	 *	destruction queue.  If this was the first message on the
1597 	 *	queue, this instance must process the full queue.
1598 	 */
1599 	if (ipc_kmsg_delayed_destroy(kmsg)) {
1600 		ipc_kmsg_reap_delayed();
1601 	}
1602 }
1603 
1604 /*
1605  *	Routine:	ipc_kmsg_delayed_destroy
1606  *	Purpose:
1607  *		Enqueues a kernel message for deferred destruction.
1608  *	Returns:
1609  *		Boolean indicator that the caller is responsible to reap
1610  *		deferred messages.
1611  */
1612 
1613 boolean_t
ipc_kmsg_delayed_destroy(ipc_kmsg_t kmsg)1614 ipc_kmsg_delayed_destroy(
1615 	ipc_kmsg_t kmsg)
1616 {
1617 	ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
1618 	boolean_t first = ipc_kmsg_queue_empty(queue);
1619 
1620 	ipc_kmsg_enqueue(queue, kmsg);
1621 	return first;
1622 }
1623 
1624 /*
1625  *	Routine:	ipc_kmsg_delayed_destroy_queue
1626  *	Purpose:
1627  *		Enqueues a queue of kernel messages for deferred destruction.
1628  *	Returns:
1629  *		Boolean indicator that the caller is responsible to reap
1630  *		deferred messages.
1631  */
1632 
1633 boolean_t
ipc_kmsg_delayed_destroy_queue(ipc_kmsg_queue_t queue)1634 ipc_kmsg_delayed_destroy_queue(
1635 	ipc_kmsg_queue_t        queue)
1636 {
1637 	ipc_kmsg_queue_t d_queue = &(current_thread()->ith_messages);
1638 	ipc_kmsg_t d_first, d_last;
1639 	ipc_kmsg_t first, last;
1640 
1641 	first = queue->ikmq_base;
1642 	if (first == IKM_NULL) {
1643 		return FALSE;
1644 	}
1645 	queue->ikmq_base = IKM_NULL;
1646 
1647 	d_first = d_queue->ikmq_base;
1648 	if (d_first == IKM_NULL) {
1649 		d_queue->ikmq_base = first;
1650 		return TRUE;
1651 	}
1652 
1653 	last = first->ikm_prev;
1654 	d_last = d_first->ikm_prev;
1655 
1656 	d_last->ikm_next = first;
1657 	first->ikm_prev = d_last;
1658 
1659 	d_first->ikm_prev = last;
1660 	last->ikm_next = d_first;
1661 
1662 	return FALSE;
1663 }
1664 
1665 /*
1666  *	Routine:	ipc_kmsg_reap_delayed
1667  *	Purpose:
1668  *		Destroys messages from the per-thread
1669  *		deferred reaping queue.
1670  *	Conditions:
1671  *		No locks held. kmsgs on queue must be signed.
1672  */
1673 
1674 void
ipc_kmsg_reap_delayed(void)1675 ipc_kmsg_reap_delayed(void)
1676 {
1677 	ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
1678 	ipc_kmsg_t kmsg;
1679 
1680 	/*
1681 	 * must leave kmsg in queue while cleaning it to assure
1682 	 * no nested calls recurse into here.
1683 	 */
1684 	while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
1685 		/*
1686 		 * Kmsgs queued for delayed destruction either come from
1687 		 * ipc_kmsg_destroy() or ipc_kmsg_delayed_destroy_queue(),
1688 		 * where we handover all kmsgs enqueued on port to destruction
1689 		 * queue in O(1). In either case, all kmsgs must have been
1690 		 * signed.
1691 		 *
1692 		 * For each unreceived msg, validate its signature before freeing.
1693 		 */
1694 		ikm_validate_sig(kmsg);
1695 
1696 		ipc_kmsg_clean(kmsg);
1697 		ipc_kmsg_rmqueue(queue, kmsg);
1698 		ipc_kmsg_free(kmsg);
1699 	}
1700 }
1701 
1702 /*
1703  *	Routine:	ipc_kmsg_clean_body
1704  *	Purpose:
1705  *		Cleans the body of a kernel message.
1706  *		Releases all rights, references, and memory.
1707  *
1708  *	Conditions:
1709  *		No locks held.
1710  */
1711 static void
ipc_kmsg_clean_body(__unused ipc_kmsg_t kmsg,mach_msg_type_number_t number,mach_msg_descriptor_t * saddr)1712 ipc_kmsg_clean_body(
1713 	__unused ipc_kmsg_t     kmsg,
1714 	mach_msg_type_number_t  number,
1715 	mach_msg_descriptor_t   *saddr)
1716 {
1717 	mach_msg_type_number_t      i;
1718 
1719 	if (number == 0) {
1720 		return;
1721 	}
1722 
1723 	for (i = 0; i < number; i++, saddr++) {
1724 		switch (saddr->type.type) {
1725 		case MACH_MSG_PORT_DESCRIPTOR: {
1726 			mach_msg_port_descriptor_t *dsc;
1727 
1728 			dsc = &saddr->port;
1729 
1730 			/*
1731 			 * Destroy port rights carried in the message
1732 			 */
1733 			if (!IP_VALID(dsc->name)) {
1734 				continue;
1735 			}
1736 			ipc_object_destroy(ip_to_object(dsc->name), dsc->disposition);
1737 			break;
1738 		}
1739 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
1740 		case MACH_MSG_OOL_DESCRIPTOR: {
1741 			mach_msg_ool_descriptor_t *dsc;
1742 
1743 			dsc = (mach_msg_ool_descriptor_t *)&saddr->out_of_line;
1744 
1745 			/*
1746 			 * Destroy memory carried in the message
1747 			 */
1748 			if (dsc->size == 0) {
1749 				assert(dsc->address == (void *) 0);
1750 			} else {
1751 				vm_map_copy_discard((vm_map_copy_t) dsc->address);
1752 			}
1753 			break;
1754 		}
1755 		case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
1756 			ipc_object_t                    *objects;
1757 			mach_msg_type_number_t          j;
1758 			mach_msg_ool_ports_descriptor_t *dsc;
1759 
1760 			dsc = (mach_msg_ool_ports_descriptor_t  *)&saddr->ool_ports;
1761 			objects = (ipc_object_t *) dsc->address;
1762 
1763 			if (dsc->count == 0) {
1764 				break;
1765 			}
1766 
1767 			assert(objects != (ipc_object_t *) 0);
1768 
1769 			/* destroy port rights carried in the message */
1770 
1771 			for (j = 0; j < dsc->count; j++) {
1772 				ipc_object_t object = objects[j];
1773 
1774 				if (!IO_VALID(object)) {
1775 					continue;
1776 				}
1777 
1778 				ipc_object_destroy(object, dsc->disposition);
1779 			}
1780 
1781 			/* destroy memory carried in the message */
1782 
1783 			assert(dsc->count != 0);
1784 
1785 			kfree_type(mach_port_t, dsc->count, dsc->address);
1786 			break;
1787 		}
1788 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
1789 			mach_msg_guarded_port_descriptor_t *dsc = (typeof(dsc)) & saddr->guarded_port;
1790 
1791 			/*
1792 			 * Destroy port rights carried in the message
1793 			 */
1794 			if (!IP_VALID(dsc->name)) {
1795 				continue;
1796 			}
1797 			ipc_object_destroy(ip_to_object(dsc->name), dsc->disposition);
1798 			break;
1799 		}
1800 		default:
1801 			panic("invalid descriptor type: (%p: %d)",
1802 			    saddr, saddr->type.type);
1803 		}
1804 	}
1805 }
1806 
1807 /*
1808  *	Routine:	ipc_kmsg_clean_partial
1809  *	Purpose:
1810  *		Cleans a partially-acquired kernel message.
1811  *		number is the index of the type descriptor
1812  *		in the body of the message that contained the error.
1813  *		If dolast, the memory and port rights in this last
1814  *		type spec are also cleaned.  In that case, number
1815  *		specifies the number of port rights to clean.
1816  *	Conditions:
1817  *		Nothing locked.
1818  */
1819 
1820 static void
ipc_kmsg_clean_partial(ipc_kmsg_t kmsg,mach_msg_type_number_t number,mach_msg_descriptor_t * desc,vm_offset_t paddr,vm_size_t length)1821 ipc_kmsg_clean_partial(
1822 	ipc_kmsg_t              kmsg,
1823 	mach_msg_type_number_t  number,
1824 	mach_msg_descriptor_t   *desc,
1825 	vm_offset_t             paddr,
1826 	vm_size_t               length)
1827 {
1828 	ipc_object_t object;
1829 	mach_msg_bits_t mbits = kmsg->ikm_header->msgh_bits;
1830 
1831 	/* deal with importance chain while we still have dest and voucher references */
1832 	ipc_importance_clean(kmsg);
1833 
1834 	object = ip_to_object(kmsg->ikm_header->msgh_remote_port);
1835 	assert(IO_VALID(object));
1836 	ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
1837 
1838 	object = ip_to_object(kmsg->ikm_header->msgh_local_port);
1839 	if (IO_VALID(object)) {
1840 		ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
1841 	}
1842 
1843 	object = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
1844 	if (IO_VALID(object)) {
1845 		assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
1846 		ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
1847 		ipc_kmsg_clear_voucher_port(kmsg);
1848 	}
1849 
1850 	if (paddr) {
1851 		kmem_free(ipc_kernel_copy_map, paddr, length);
1852 	}
1853 
1854 	ipc_kmsg_clean_body(kmsg, number, desc);
1855 }
1856 
1857 /*
1858  *	Routine:	ipc_kmsg_clean
1859  *	Purpose:
1860  *		Cleans a kernel message.  Releases all rights,
1861  *		references, and memory held by the message.
1862  *	Conditions:
1863  *		No locks held.
1864  */
1865 
1866 static void
ipc_kmsg_clean(ipc_kmsg_t kmsg)1867 ipc_kmsg_clean(
1868 	ipc_kmsg_t      kmsg)
1869 {
1870 	ipc_object_t object;
1871 	mach_msg_bits_t mbits;
1872 
1873 	/* deal with importance chain while we still have dest and voucher references */
1874 	ipc_importance_clean(kmsg);
1875 
1876 	mbits = kmsg->ikm_header->msgh_bits;
1877 	object = ip_to_object(kmsg->ikm_header->msgh_remote_port);
1878 	if (IO_VALID(object)) {
1879 		ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
1880 	}
1881 
1882 	object = ip_to_object(kmsg->ikm_header->msgh_local_port);
1883 	if (IO_VALID(object)) {
1884 		ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
1885 	}
1886 
1887 	object = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
1888 	if (IO_VALID(object)) {
1889 		assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
1890 		ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
1891 		ipc_kmsg_clear_voucher_port(kmsg);
1892 	}
1893 
1894 	if (mbits & MACH_MSGH_BITS_COMPLEX) {
1895 		mach_msg_body_t *body;
1896 
1897 		body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
1898 		ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
1899 		    (mach_msg_descriptor_t *)(body + 1));
1900 	}
1901 }
1902 
1903 /*
1904  *	Routine:	ipc_kmsg_set_prealloc
1905  *	Purpose:
1906  *		Assign a kmsg as a preallocated message buffer to a port.
1907  *	Conditions:
1908  *		port locked.
1909  */
1910 
1911 void
ipc_kmsg_set_prealloc(ipc_kmsg_t kmsg,ipc_port_t port)1912 ipc_kmsg_set_prealloc(
1913 	ipc_kmsg_t              kmsg,
1914 	ipc_port_t              port)
1915 {
1916 	assert(kmsg->ikm_prealloc == IP_NULL);
1917 
1918 	kmsg->ikm_prealloc = IP_NULL;
1919 
1920 	assert(port_send_turnstile(port) == TURNSTILE_NULL);
1921 	kmsg->ikm_turnstile = TURNSTILE_NULL;
1922 	IP_SET_PREALLOC(port, kmsg);
1923 }
1924 
1925 /*
1926  *	Routine:	ipc_kmsg_get_from_user
1927  *	Purpose:
1928  *		Allocates a kernel message buffer.
1929  *		Copies a user message to the message buffer.
1930  *	Conditions:
1931  *		Nothing locked.
1932  *	Returns:
1933  *		MACH_MSG_SUCCESS	Acquired a message buffer.
1934  *		MACH_SEND_MSG_TOO_SMALL	Message smaller than a header.
1935  *		MACH_SEND_MSG_TOO_SMALL	Message size not long-word multiple.
1936  *		MACH_SEND_TOO_LARGE	Message too large to ever be sent.
1937  *		MACH_SEND_NO_BUFFER	Couldn't allocate a message buffer.
1938  *		MACH_SEND_INVALID_DATA	Couldn't copy message data.
1939  */
1940 
1941 mach_msg_return_t
ipc_kmsg_get_from_user(mach_vm_address_t msg_addr,mach_msg_size_t size,ipc_kmsg_t * kmsgp)1942 ipc_kmsg_get_from_user(
1943 	mach_vm_address_t       msg_addr,
1944 	mach_msg_size_t         size,
1945 	ipc_kmsg_t              *kmsgp)
1946 {
1947 	ipc_kmsg_t                  kmsg;
1948 	mach_msg_user_base_t        user_base;
1949 	mach_msg_size_t             len_copied;
1950 	mach_msg_size_t             descriptors;
1951 
1952 	if ((size < sizeof(mach_msg_user_header_t)) || (size & 3)) {
1953 		return MACH_SEND_MSG_TOO_SMALL;
1954 	}
1955 
1956 	if (size > ipc_kmsg_max_body_space) {
1957 		return MACH_SEND_TOO_LARGE;
1958 	}
1959 
1960 	if (size == sizeof(mach_msg_user_header_t)) {
1961 		len_copied = sizeof(mach_msg_user_header_t);
1962 	} else {
1963 		len_copied = sizeof(mach_msg_user_base_t);
1964 	}
1965 
1966 	user_base.body.msgh_descriptor_count = descriptors = 0;
1967 	if (copyinmsg(msg_addr, (char *)&user_base, len_copied)) {
1968 		return MACH_SEND_INVALID_DATA;
1969 	}
1970 
1971 	/*
1972 	 * If the message claims to be complex, it must at least
1973 	 * have the length of a "base" message (header + dsc_count).
1974 	 */
1975 	if (user_base.header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
1976 		if (len_copied < sizeof(mach_msg_user_base_t)) {
1977 			return MACH_SEND_MSG_TOO_SMALL;
1978 		}
1979 		descriptors = user_base.body.msgh_descriptor_count;
1980 		if (descriptors > (size - len_copied) / MACH_MSG_DESC_MIN_SIZE) {
1981 			return MACH_SEND_MSG_TOO_SMALL;
1982 		}
1983 	}
1984 
1985 #if defined(__LP64__)
1986 	size += USER_HEADER_SIZE_DELTA;
1987 #endif
1988 	/* unreachable if !DEBUG */
1989 	if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
1990 		__unreachable_ok_push
1991 		for (uint32_t j = 0; j < sizeof(user_base.header); j++) {
1992 			kprintf("%02x\n", ((unsigned char*)&user_base.header)[j]);
1993 		}
1994 		__unreachable_ok_pop
1995 	}
1996 
1997 	kmsg = ipc_kmsg_alloc(size, descriptors, IPC_KMSG_ALLOC_USER);
1998 	if (kmsg == IKM_NULL) {
1999 		return MACH_SEND_NO_BUFFER;
2000 	}
2001 
2002 	kmsg->ikm_header->msgh_size             = size;
2003 	kmsg->ikm_header->msgh_bits             = user_base.header.msgh_bits;
2004 	kmsg->ikm_header->msgh_remote_port      = CAST_MACH_NAME_TO_PORT(user_base.header.msgh_remote_port);
2005 	kmsg->ikm_header->msgh_local_port       = CAST_MACH_NAME_TO_PORT(user_base.header.msgh_local_port);
2006 	kmsg->ikm_header->msgh_voucher_port     = user_base.header.msgh_voucher_port;
2007 	kmsg->ikm_header->msgh_id               = user_base.header.msgh_id;
2008 
2009 	DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_get_from_user header:\n"
2010 	    "  size:		0x%.8x\n"
2011 	    "  bits:		0x%.8x\n"
2012 	    "  remote_port:	%p\n"
2013 	    "  local_port:	%p\n"
2014 	    "  voucher_port:	0x%.8x\n"
2015 	    "  id:		%.8d\n",
2016 	    kmsg->ikm_header->msgh_size,
2017 	    kmsg->ikm_header->msgh_bits,
2018 	    kmsg->ikm_header->msgh_remote_port,
2019 	    kmsg->ikm_header->msgh_local_port,
2020 	    kmsg->ikm_header->msgh_voucher_port,
2021 	    kmsg->ikm_header->msgh_id);
2022 
2023 	if (size >= sizeof(mach_msg_base_t)) {
2024 		mach_msg_base_t *kbase = ((mach_msg_base_t *)kmsg->ikm_header);
2025 
2026 		kbase->body.msgh_descriptor_count =
2027 		    user_base.body.msgh_descriptor_count;
2028 	}
2029 
2030 	if (size > sizeof(mach_msg_base_t)) {
2031 		if (copyinmsg(msg_addr + sizeof(mach_msg_user_base_t),
2032 		    (char *)kmsg->ikm_header + sizeof(mach_msg_base_t),
2033 		    size - sizeof(mach_msg_base_t))) {
2034 			ipc_kmsg_free(kmsg);
2035 			return MACH_SEND_INVALID_DATA;
2036 		}
2037 	}
2038 
2039 	/* unreachable if !DEBUG */
2040 	if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
2041 		__unreachable_ok_push
2042 		kprintf("body: size: %lu\n", (size - sizeof(mach_msg_header_t)));
2043 		for (uint32_t i = 0; i * 4 < (size - sizeof(mach_msg_header_t)); i++) {
2044 			kprintf("%.4x\n", ((uint32_t *)(kmsg->ikm_header + 1))[i]);
2045 		}
2046 		__unreachable_ok_pop
2047 	}
2048 	DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_get_from_user()");
2049 
2050 	*kmsgp = kmsg;
2051 	return MACH_MSG_SUCCESS;
2052 }
2053 
2054 /*
2055  *	Routine:	ipc_kmsg_get_from_kernel
2056  *	Purpose:
2057  *		First checks for a preallocated message
2058  *		reserved for kernel clients.  If not found or size is too large -
2059  *		allocates a new kernel message buffer.
2060  *		Copies a kernel message to the message buffer.
2061  *		Only resource errors are allowed.
2062  *	Conditions:
2063  *		Nothing locked.
2064  *		Ports in header are ipc_port_t.
2065  *	Returns:
2066  *		MACH_MSG_SUCCESS	Acquired a message buffer.
2067  *		MACH_SEND_NO_BUFFER	Couldn't allocate a message buffer.
2068  */
2069 
2070 mach_msg_return_t
ipc_kmsg_get_from_kernel(mach_msg_header_t * msg,mach_msg_size_t size,ipc_kmsg_t * kmsgp)2071 ipc_kmsg_get_from_kernel(
2072 	mach_msg_header_t       *msg,
2073 	mach_msg_size_t         size, /* can be larger than prealloc space */
2074 	ipc_kmsg_t              *kmsgp)
2075 {
2076 	ipc_kmsg_t      kmsg;
2077 	ipc_port_t      dest_port;
2078 
2079 	assert(size >= sizeof(mach_msg_header_t));
2080 	assert((size & 3) == 0);
2081 
2082 	dest_port = msg->msgh_remote_port;
2083 
2084 	/*
2085 	 * See if the port has a pre-allocated kmsg for kernel
2086 	 * clients.  These are set up for those kernel clients
2087 	 * which cannot afford to wait.
2088 	 */
2089 	if (IP_VALID(dest_port) && IP_PREALLOC(dest_port)) {
2090 		ip_mq_lock(dest_port);
2091 		if (!ip_active(dest_port)) {
2092 			ip_mq_unlock(dest_port);
2093 			return MACH_SEND_NO_BUFFER;
2094 		}
2095 		assert(IP_PREALLOC(dest_port));
2096 		kmsg = dest_port->ip_premsg;
2097 		if (ikm_prealloc_inuse(kmsg)) {
2098 			ip_mq_unlock(dest_port);
2099 			return MACH_SEND_NO_BUFFER;
2100 		}
2101 		assert(kmsg->ikm_size == IKM_SAVED_MSG_SIZE);
2102 		if (size + MAX_TRAILER_SIZE > kmsg->ikm_size) {
2103 			ip_mq_unlock(dest_port);
2104 			return MACH_SEND_TOO_LARGE;
2105 		}
2106 		ikm_prealloc_set_inuse(kmsg, dest_port);
2107 		ikm_set_header(kmsg, NULL, size);
2108 		ip_mq_unlock(dest_port);
2109 	} else {
2110 		kmsg = ipc_kmsg_alloc(size, 0, IPC_KMSG_ALLOC_KERNEL);
2111 		if (kmsg == IKM_NULL) {
2112 			return MACH_SEND_NO_BUFFER;
2113 		}
2114 	}
2115 
2116 	memcpy(kmsg->ikm_header, msg, size);
2117 	kmsg->ikm_header->msgh_size = size;
2118 
2119 	*kmsgp = kmsg;
2120 	return MACH_MSG_SUCCESS;
2121 }
2122 
2123 /*
2124  *	Routine:	ipc_kmsg_send
2125  *	Purpose:
2126  *		Send a message.  The message holds a reference
2127  *		for the destination port in the msgh_remote_port field.
2128  *
2129  *		If unsuccessful, the caller still has possession of
2130  *		the message and must do something with it.  If successful,
2131  *		the message is queued, given to a receiver, destroyed,
2132  *		or handled directly by the kernel via mach_msg.
2133  *	Conditions:
2134  *		Nothing locked.
2135  *	Returns:
2136  *		MACH_MSG_SUCCESS	The message was accepted.
2137  *		MACH_SEND_TIMED_OUT	Caller still has message.
2138  *		MACH_SEND_INTERRUPTED	Caller still has message.
2139  *		MACH_SEND_INVALID_DEST	Caller still has message.
2140  */
2141 mach_msg_return_t
ipc_kmsg_send(ipc_kmsg_t kmsg,mach_msg_option_t option,mach_msg_timeout_t send_timeout)2142 ipc_kmsg_send(
2143 	ipc_kmsg_t              kmsg,
2144 	mach_msg_option_t       option,
2145 	mach_msg_timeout_t      send_timeout)
2146 {
2147 	ipc_port_t port;
2148 	thread_t th = current_thread();
2149 	mach_msg_return_t error = MACH_MSG_SUCCESS;
2150 	boolean_t kernel_reply = FALSE;
2151 
2152 	/* Check if honor qlimit flag is set on thread. */
2153 	if ((th->options & TH_OPT_HONOR_QLIMIT) == TH_OPT_HONOR_QLIMIT) {
2154 		/* Remove the MACH_SEND_ALWAYS flag to honor queue limit. */
2155 		option &= (~MACH_SEND_ALWAYS);
2156 		/* Add the timeout flag since the message queue might be full. */
2157 		option |= MACH_SEND_TIMEOUT;
2158 		th->options &= (~TH_OPT_HONOR_QLIMIT);
2159 	}
2160 
2161 #if IMPORTANCE_INHERITANCE
2162 	bool did_importance = false;
2163 #if IMPORTANCE_TRACE
2164 	mach_msg_id_t imp_msgh_id = -1;
2165 	int           sender_pid  = -1;
2166 #endif /* IMPORTANCE_TRACE */
2167 #endif /* IMPORTANCE_INHERITANCE */
2168 
2169 	/* don't allow the creation of a circular loop */
2170 	if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
2171 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_ALL);
2172 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_MSGH_BITS_CIRCULAR);
2173 		return MACH_MSG_SUCCESS;
2174 	}
2175 
2176 	ipc_voucher_send_preprocessing(kmsg);
2177 
2178 	port = kmsg->ikm_header->msgh_remote_port;
2179 	assert(IP_VALID(port));
2180 	ip_mq_lock(port);
2181 
2182 	/*
2183 	 * If the destination has been guarded with a reply context, and the
2184 	 * sender is consuming a send-once right, then assume this is a reply
2185 	 * to an RPC and we need to validate that this sender is currently in
2186 	 * the correct context.
2187 	 */
2188 	if (enforce_strict_reply && port->ip_reply_context != 0 &&
2189 	    ((option & MACH_SEND_KERNEL) == 0) &&
2190 	    MACH_MSGH_BITS_REMOTE(kmsg->ikm_header->msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE) {
2191 		error = ipc_kmsg_validate_reply_context_locked(option, port, th->ith_voucher, th->ith_voucher_name);
2192 		if (error != MACH_MSG_SUCCESS) {
2193 			ip_mq_unlock(port);
2194 			return error;
2195 		}
2196 	}
2197 
2198 #if IMPORTANCE_INHERITANCE
2199 retry:
2200 #endif /* IMPORTANCE_INHERITANCE */
2201 	/*
2202 	 *	Can't deliver to a dead port.
2203 	 *	However, we can pretend it got sent
2204 	 *	and was then immediately destroyed.
2205 	 */
2206 	if (!ip_active(port)) {
2207 		ip_mq_unlock(port);
2208 #if MACH_FLIPC
2209 		if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2210 			flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2211 		}
2212 #endif
2213 		if (did_importance) {
2214 			/*
2215 			 * We're going to pretend we delivered this message
2216 			 * successfully, and just eat the kmsg. However, the
2217 			 * kmsg is actually visible via the importance_task!
2218 			 * We need to cleanup this linkage before we destroy
2219 			 * the message, and more importantly before we set the
2220 			 * msgh_remote_port to NULL. See: 34302571
2221 			 */
2222 			ipc_importance_clean(kmsg);
2223 		}
2224 		ip_release(port);  /* JMM - Future: release right, not just ref */
2225 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_SKIP_REMOTE);
2226 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_INVALID_DEST);
2227 		return MACH_MSG_SUCCESS;
2228 	}
2229 
2230 	if (ip_in_space(port, ipc_space_kernel)) {
2231 		require_ip_active(port);
2232 		port->ip_messages.imq_seqno++;
2233 		ip_mq_unlock(port);
2234 
2235 		counter_inc(&current_task()->messages_sent);
2236 
2237 		/*
2238 		 * Call the server routine, and get the reply message to send.
2239 		 */
2240 		kmsg = ipc_kobject_server(port, kmsg, option);
2241 		if (kmsg == IKM_NULL) {
2242 			return MACH_MSG_SUCCESS;
2243 		}
2244 
2245 		/* sign the reply message */
2246 		ipc_kmsg_init_trailer(kmsg, kmsg->ikm_header->msgh_size, TASK_NULL);
2247 		ikm_sign(kmsg);
2248 
2249 		/* restart the KMSG_INFO tracing for the reply message */
2250 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
2251 		port = kmsg->ikm_header->msgh_remote_port;
2252 		assert(IP_VALID(port));
2253 		ip_mq_lock(port);
2254 		/* fall thru with reply - same options */
2255 		kernel_reply = TRUE;
2256 		if (!ip_active(port)) {
2257 			error = MACH_SEND_INVALID_DEST;
2258 		}
2259 	}
2260 
2261 #if IMPORTANCE_INHERITANCE
2262 	/*
2263 	 * Need to see if this message needs importance donation and/or
2264 	 * propagation.  That routine can drop the port lock temporarily.
2265 	 * If it does we'll have to revalidate the destination.
2266 	 */
2267 	if (!did_importance) {
2268 		did_importance = true;
2269 		if (ipc_importance_send(kmsg, option)) {
2270 			goto retry;
2271 		}
2272 	}
2273 #endif /* IMPORTANCE_INHERITANCE */
2274 
2275 	if (error != MACH_MSG_SUCCESS) {
2276 		ip_mq_unlock(port);
2277 	} else {
2278 		/*
2279 		 * We have a valid message and a valid reference on the port.
2280 		 * call mqueue_send() on its message queue.
2281 		 */
2282 		ipc_special_reply_port_msg_sent(port);
2283 
2284 		error = ipc_mqueue_send_locked(&port->ip_messages, kmsg, option,
2285 		    send_timeout);
2286 		/* port unlocked */
2287 	}
2288 
2289 #if IMPORTANCE_INHERITANCE
2290 	if (did_importance) {
2291 		__unused int importance_cleared = 0;
2292 		switch (error) {
2293 		case MACH_SEND_TIMED_OUT:
2294 		case MACH_SEND_NO_BUFFER:
2295 		case MACH_SEND_INTERRUPTED:
2296 		case MACH_SEND_INVALID_DEST:
2297 			/*
2298 			 * We still have the kmsg and its
2299 			 * reference on the port.  But we
2300 			 * have to back out the importance
2301 			 * boost.
2302 			 *
2303 			 * The port could have changed hands,
2304 			 * be inflight to another destination,
2305 			 * etc...  But in those cases our
2306 			 * back-out will find the new owner
2307 			 * (and all the operations that
2308 			 * transferred the right should have
2309 			 * applied their own boost adjustments
2310 			 * to the old owner(s)).
2311 			 */
2312 			importance_cleared = 1;
2313 			ipc_importance_clean(kmsg);
2314 			break;
2315 
2316 		case MACH_MSG_SUCCESS:
2317 		default:
2318 			break;
2319 		}
2320 #if IMPORTANCE_TRACE
2321 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_MSG, IMP_MSG_SEND)) | DBG_FUNC_END,
2322 		    task_pid(current_task()), sender_pid, imp_msgh_id, importance_cleared, 0);
2323 #endif /* IMPORTANCE_TRACE */
2324 	}
2325 #endif /* IMPORTANCE_INHERITANCE */
2326 
2327 	/*
2328 	 * If the port has been destroyed while we wait, treat the message
2329 	 * as a successful delivery (like we do for an inactive port).
2330 	 */
2331 	if (error == MACH_SEND_INVALID_DEST) {
2332 #if MACH_FLIPC
2333 		if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2334 			flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2335 		}
2336 #endif
2337 		ip_release(port); /* JMM - Future: release right, not just ref */
2338 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_SKIP_REMOTE);
2339 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_INVALID_DEST);
2340 		return MACH_MSG_SUCCESS;
2341 	}
2342 
2343 	if (error != MACH_MSG_SUCCESS && kernel_reply) {
2344 		/*
2345 		 * Kernel reply messages that fail can't be allowed to
2346 		 * pseudo-receive on error conditions. We need to just treat
2347 		 * the message as a successful delivery.
2348 		 */
2349 #if MACH_FLIPC
2350 		if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2351 			flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2352 		}
2353 #endif
2354 		ip_release(port); /* JMM - Future: release right, not just ref */
2355 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_SKIP_REMOTE);
2356 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, error);
2357 		return MACH_MSG_SUCCESS;
2358 	}
2359 	return error;
2360 }
2361 
2362 /*
2363  *	Routine:	ipc_kmsg_put_to_user
2364  *	Purpose:
2365  *		Copies a message buffer to a user message.
2366  *		Copies only the specified number of bytes.
2367  *		Frees the message buffer.
2368  *	Conditions:
2369  *		Nothing locked.  The message buffer must have clean
2370  *		header fields.
2371  *	Returns:
2372  *		MACH_MSG_SUCCESS	Copied data out of message buffer.
2373  *		MACH_RCV_INVALID_DATA	Couldn't copy to user message.
2374  */
2375 
2376 mach_msg_return_t
ipc_kmsg_put_to_user(ipc_kmsg_t kmsg,mach_msg_option_t option,mach_vm_address_t rcv_addr,mach_msg_size_t rcv_size,mach_msg_size_t trailer_size,mach_msg_size_t * sizep)2377 ipc_kmsg_put_to_user(
2378 	ipc_kmsg_t              kmsg,
2379 	mach_msg_option_t       option,
2380 	mach_vm_address_t       rcv_addr,
2381 	mach_msg_size_t         rcv_size,
2382 	mach_msg_size_t         trailer_size,
2383 	mach_msg_size_t         *sizep)
2384 {
2385 	mach_msg_size_t size = kmsg->ikm_header->msgh_size + trailer_size;
2386 	mach_msg_return_t mr;
2387 
2388 	DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_put_to_user()");
2389 
2390 
2391 	DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_put_to_user header:\n"
2392 	    "  size:		0x%.8x\n"
2393 	    "  bits:		0x%.8x\n"
2394 	    "  remote_port:	%p\n"
2395 	    "  local_port:	%p\n"
2396 	    "  voucher_port:	0x%.8x\n"
2397 	    "  id:		%.8d\n",
2398 	    kmsg->ikm_header->msgh_size,
2399 	    kmsg->ikm_header->msgh_bits,
2400 	    kmsg->ikm_header->msgh_remote_port,
2401 	    kmsg->ikm_header->msgh_local_port,
2402 	    kmsg->ikm_header->msgh_voucher_port,
2403 	    kmsg->ikm_header->msgh_id);
2404 
2405 	assert(current_task() != kernel_task);
2406 
2407 #if defined(__LP64__)
2408 	{
2409 		mach_msg_user_header_t *user_header =
2410 		    (mach_msg_user_header_t *)((vm_offset_t)(kmsg->ikm_header) + USER_HEADER_SIZE_DELTA);
2411 
2412 		mach_msg_bits_t         bits            = kmsg->ikm_header->msgh_bits;
2413 		mach_msg_size_t         msg_size        = kmsg->ikm_header->msgh_size;
2414 		mach_port_name_t        remote_port     = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_remote_port);
2415 		mach_port_name_t        local_port      = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_local_port);
2416 		mach_port_name_t        voucher_port    = kmsg->ikm_header->msgh_voucher_port;
2417 		mach_msg_id_t           id              = kmsg->ikm_header->msgh_id;
2418 
2419 		user_header->msgh_id                    = id;
2420 		user_header->msgh_local_port            = local_port;
2421 		user_header->msgh_remote_port           = remote_port;
2422 		user_header->msgh_voucher_port          = voucher_port;
2423 		user_header->msgh_size                  = msg_size - USER_HEADER_SIZE_DELTA;
2424 		user_header->msgh_bits                  = bits;
2425 
2426 		size -= USER_HEADER_SIZE_DELTA;
2427 		kmsg->ikm_header = (mach_msg_header_t *)user_header;
2428 	}
2429 #endif
2430 
2431 	/* unreachable if !DEBUG */
2432 	if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
2433 		__unreachable_ok_push
2434 		kprintf("ipc_kmsg_put_to_user header+body: %d\n", (size));
2435 		for (uint32_t i = 0; i * 4 < size; i++) {
2436 			kprintf("%.4x\n", ((uint32_t *)kmsg->ikm_header)[i]);
2437 		}
2438 		kprintf("type: %d\n", ((mach_msg_type_descriptor_t *)(((mach_msg_base_t *)kmsg->ikm_header) + 1))->type);
2439 		__unreachable_ok_pop
2440 	}
2441 
2442 	/*
2443 	 * (81193887) some clients stomp their own stack due to mis-sized
2444 	 * combined send/receives where the receive buffer didn't account
2445 	 * for the trailer size.
2446 	 *
2447 	 * At the very least, avoid smashint their stack.
2448 	 */
2449 	if (size > rcv_size) {
2450 		size = rcv_size;
2451 	}
2452 
2453 	/* Re-Compute target address if using stack-style delivery */
2454 	if (option & MACH_RCV_STACK) {
2455 		rcv_addr += rcv_size - size;
2456 	}
2457 
2458 	if (copyoutmsg((const char *) kmsg->ikm_header, rcv_addr, size)) {
2459 		mr = MACH_RCV_INVALID_DATA;
2460 		size = 0;
2461 	} else {
2462 		mr = MACH_MSG_SUCCESS;
2463 	}
2464 
2465 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
2466 	    (rcv_addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS ||
2467 	    rcv_addr + size >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) ? (uintptr_t)0 : (uintptr_t)rcv_addr,
2468 	    VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
2469 	    1 /* this is on the receive/copyout path */,
2470 	    0,
2471 	    0);
2472 	ipc_kmsg_free(kmsg);
2473 
2474 	if (sizep) {
2475 		*sizep = size;
2476 	}
2477 	return mr;
2478 }
2479 
2480 /*
2481  *	Routine:	ipc_kmsg_put_to_kernel
2482  *	Purpose:
2483  *		Copies a message buffer to a kernel message.
2484  *		Frees the message buffer.
2485  *		No errors allowed.
2486  *	Conditions:
2487  *		Nothing locked.
2488  */
2489 
2490 void
ipc_kmsg_put_to_kernel(mach_msg_header_t * msg,ipc_kmsg_t kmsg,mach_msg_size_t size)2491 ipc_kmsg_put_to_kernel(
2492 	mach_msg_header_t       *msg,
2493 	ipc_kmsg_t              kmsg,
2494 	mach_msg_size_t         size)
2495 {
2496 	(void) memcpy((void *) msg, (const void *) kmsg->ikm_header, size);
2497 
2498 	ipc_kmsg_free(kmsg);
2499 }
2500 
2501 static pthread_priority_compact_t
ipc_get_current_thread_priority(void)2502 ipc_get_current_thread_priority(void)
2503 {
2504 	thread_t thread = current_thread();
2505 	thread_qos_t qos;
2506 	int relpri;
2507 
2508 	qos = thread_get_requested_qos(thread, &relpri);
2509 	if (!qos) {
2510 		qos = thread_user_promotion_qos_for_pri(thread->base_pri);
2511 		relpri = 0;
2512 	}
2513 	return _pthread_priority_make_from_thread_qos(qos, relpri, 0);
2514 }
2515 
2516 static kern_return_t
ipc_kmsg_set_qos(ipc_kmsg_t kmsg,mach_msg_option_t options,mach_msg_priority_t priority)2517 ipc_kmsg_set_qos(
2518 	ipc_kmsg_t kmsg,
2519 	mach_msg_option_t options,
2520 	mach_msg_priority_t priority)
2521 {
2522 	kern_return_t kr;
2523 	ipc_port_t special_reply_port = kmsg->ikm_header->msgh_local_port;
2524 	ipc_port_t dest_port = kmsg->ikm_header->msgh_remote_port;
2525 
2526 	if ((options & MACH_SEND_OVERRIDE) &&
2527 	    !mach_msg_priority_is_pthread_priority(priority)) {
2528 		mach_msg_qos_t qos = mach_msg_priority_qos(priority);
2529 		int relpri = mach_msg_priority_relpri(priority);
2530 		mach_msg_qos_t ovr = mach_msg_priority_overide_qos(priority);
2531 
2532 		kmsg->ikm_ppriority = _pthread_priority_make_from_thread_qos(qos, relpri, 0);
2533 		kmsg->ikm_qos_override = MAX(qos, ovr);
2534 	} else {
2535 #if CONFIG_VOUCHER_DEPRECATED
2536 		kr = ipc_get_pthpriority_from_kmsg_voucher(kmsg, &kmsg->ikm_ppriority);
2537 #else
2538 		kr = KERN_FAILURE;
2539 #endif /* CONFIG_VOUCHER_DEPRECATED */
2540 		if (kr != KERN_SUCCESS) {
2541 			if (options & MACH_SEND_PROPAGATE_QOS) {
2542 				kmsg->ikm_ppriority = ipc_get_current_thread_priority();
2543 			} else {
2544 				kmsg->ikm_ppriority = MACH_MSG_PRIORITY_UNSPECIFIED;
2545 			}
2546 		}
2547 
2548 		if (options & MACH_SEND_OVERRIDE) {
2549 			mach_msg_qos_t qos = _pthread_priority_thread_qos(kmsg->ikm_ppriority);
2550 			mach_msg_qos_t ovr = _pthread_priority_thread_qos(priority);
2551 			kmsg->ikm_qos_override = MAX(qos, ovr);
2552 		} else {
2553 			kmsg->ikm_qos_override = _pthread_priority_thread_qos(kmsg->ikm_ppriority);
2554 		}
2555 	}
2556 
2557 	kr = KERN_SUCCESS;
2558 
2559 	if (IP_VALID(special_reply_port) &&
2560 	    special_reply_port->ip_specialreply &&
2561 	    !ip_is_kobject(dest_port) &&
2562 	    MACH_MSGH_BITS_LOCAL(kmsg->ikm_header->msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE) {
2563 		boolean_t sync_bootstrap_checkin = !!(options & MACH_SEND_SYNC_BOOTSTRAP_CHECKIN);
2564 		/*
2565 		 * Link the destination port to special reply port and make sure that
2566 		 * dest port has a send turnstile, else allocate one.
2567 		 */
2568 		ipc_port_link_special_reply_port(special_reply_port, dest_port, sync_bootstrap_checkin);
2569 	}
2570 	return kr;
2571 }
2572 
2573 static kern_return_t
ipc_kmsg_set_qos_kernel(ipc_kmsg_t kmsg)2574 ipc_kmsg_set_qos_kernel(
2575 	ipc_kmsg_t kmsg)
2576 {
2577 	ipc_port_t dest_port = kmsg->ikm_header->msgh_remote_port;
2578 	kmsg->ikm_qos_override = dest_port->ip_kernel_qos_override;
2579 	kmsg->ikm_ppriority = _pthread_priority_make_from_thread_qos(kmsg->ikm_qos_override, 0, 0);
2580 	return KERN_SUCCESS;
2581 }
2582 
2583 /*
2584  *	Routine:	ipc_kmsg_link_reply_context_locked
2585  *	Purpose:
2586  *		Link any required context from the sending voucher
2587  *		to the reply port. The ipc_kmsg_copyin_from_user function will
2588  *		enforce that the sender calls mach_msg in this context.
2589  *	Conditions:
2590  *		reply port is locked
2591  */
2592 static void
ipc_kmsg_link_reply_context_locked(ipc_port_t reply_port,ipc_port_t voucher_port)2593 ipc_kmsg_link_reply_context_locked(
2594 	ipc_port_t reply_port,
2595 	ipc_port_t voucher_port)
2596 {
2597 	kern_return_t __assert_only kr;
2598 	uint32_t persona_id = 0;
2599 	ipc_voucher_t voucher;
2600 
2601 	ip_mq_lock_held(reply_port);
2602 
2603 	if (!ip_active(reply_port)) {
2604 		return;
2605 	}
2606 
2607 	voucher = convert_port_to_voucher(voucher_port);
2608 
2609 	kr = bank_get_bank_ledger_thread_group_and_persona(voucher, NULL, NULL, &persona_id);
2610 	assert(kr == KERN_SUCCESS);
2611 	ipc_voucher_release(voucher);
2612 
2613 	if (persona_id == 0 || persona_id == PERSONA_ID_NONE) {
2614 		/* there was no persona context to record */
2615 		return;
2616 	}
2617 
2618 	/*
2619 	 * Set the persona_id as the context on the reply port.
2620 	 * This will force the thread that replies to have adopted a voucher
2621 	 * with a matching persona.
2622 	 */
2623 	reply_port->ip_reply_context = persona_id;
2624 
2625 	return;
2626 }
2627 
2628 static kern_return_t
ipc_kmsg_validate_reply_port_locked(ipc_port_t reply_port,mach_msg_option_t options)2629 ipc_kmsg_validate_reply_port_locked(ipc_port_t reply_port, mach_msg_option_t options)
2630 {
2631 	ip_mq_lock_held(reply_port);
2632 
2633 	if (!ip_active(reply_port)) {
2634 		/*
2635 		 * Ideally, we would enforce that the reply receive right is
2636 		 * active, but asynchronous XPC cancellation destroys the
2637 		 * receive right, so we just have to return success here.
2638 		 */
2639 		return KERN_SUCCESS;
2640 	}
2641 
2642 	if (options & MACH_SEND_MSG) {
2643 		/*
2644 		 * If the rely port is active, then it should not be
2645 		 * in-transit, and the receive right should be in the caller's
2646 		 * IPC space.
2647 		 */
2648 		if (!ip_in_space(reply_port, current_task()->itk_space)) {
2649 			return KERN_INVALID_CAPABILITY;
2650 		}
2651 
2652 		/*
2653 		 * A port used as a reply port in an RPC should have exactly 1
2654 		 * extant send-once right which we either just made or are
2655 		 * moving as part of the IPC.
2656 		 */
2657 		if (reply_port->ip_sorights != 1) {
2658 			return KERN_INVALID_CAPABILITY;
2659 		}
2660 		/*
2661 		 * XPC uses an extra send-right to keep the name of the reply
2662 		 * right around through cancellation.  That makes it harder to
2663 		 * enforce a particular semantic kere, so for now, we say that
2664 		 * you can have a maximum of 1 send right (in addition to your
2665 		 * send once right). In the future, it would be great to lock
2666 		 * this down even further.
2667 		 */
2668 		if (reply_port->ip_srights > 1) {
2669 			return KERN_INVALID_CAPABILITY;
2670 		}
2671 
2672 		/*
2673 		 * The sender can also specify that the receive right should
2674 		 * be immovable. Note that this check only applies to
2675 		 * send-only operations. Combined send/receive or rcv-only
2676 		 * operations can specify an immovable receive right by
2677 		 * opt-ing into guarded descriptors (MACH_RCV_GUARDED_DESC)
2678 		 * and using the MACH_MSG_STRICT_REPLY options flag.
2679 		 */
2680 		if (MACH_SEND_REPLY_IS_IMMOVABLE(options)) {
2681 			if (!reply_port->ip_immovable_receive) {
2682 				return KERN_INVALID_CAPABILITY;
2683 			}
2684 		}
2685 	}
2686 
2687 	/*
2688 	 * don't enforce this yet: need a better way of indicating the
2689 	 * receiver wants this...
2690 	 */
2691 #if 0
2692 	if (MACH_RCV_WITH_IMMOVABLE_REPLY(options)) {
2693 		if (!reply_port->ip_immovable_receive) {
2694 			return KERN_INVALID_CAPABILITY;
2695 		}
2696 	}
2697 #endif /* 0  */
2698 
2699 	return KERN_SUCCESS;
2700 }
2701 
2702 /*
2703  *	Routine:	ipc_kmsg_validate_reply_context_locked
2704  *	Purpose:
2705  *		Validate that the current thread is running in the context
2706  *		required by the destination port.
2707  *	Conditions:
2708  *		dest_port is locked
2709  *	Returns:
2710  *		MACH_MSG_SUCCESS on success.
2711  *		On error, an EXC_GUARD exception is also raised.
2712  *		This function *always* resets the port reply context.
2713  */
2714 static mach_msg_return_t
ipc_kmsg_validate_reply_context_locked(mach_msg_option_t option,ipc_port_t dest_port,ipc_voucher_t voucher,mach_port_name_t voucher_name)2715 ipc_kmsg_validate_reply_context_locked(
2716 	mach_msg_option_t option,
2717 	ipc_port_t dest_port,
2718 	ipc_voucher_t voucher,
2719 	mach_port_name_t voucher_name)
2720 {
2721 	uint32_t dest_ctx = dest_port->ip_reply_context;
2722 	dest_port->ip_reply_context = 0;
2723 
2724 	if (!ip_active(dest_port)) {
2725 		return MACH_MSG_SUCCESS;
2726 	}
2727 
2728 	if (voucher == IPC_VOUCHER_NULL || !MACH_PORT_VALID(voucher_name)) {
2729 		if ((option & MACH_SEND_KERNEL) == 0) {
2730 			mach_port_guard_exception(voucher_name, 0,
2731 			    (MPG_FLAGS_STRICT_REPLY_INVALID_VOUCHER | dest_ctx),
2732 			    kGUARD_EXC_STRICT_REPLY);
2733 		}
2734 		return MACH_SEND_INVALID_CONTEXT;
2735 	}
2736 
2737 	kern_return_t __assert_only kr;
2738 	uint32_t persona_id = 0;
2739 	kr = bank_get_bank_ledger_thread_group_and_persona(voucher, NULL, NULL, &persona_id);
2740 	assert(kr == KERN_SUCCESS);
2741 
2742 	if (dest_ctx != persona_id) {
2743 		if ((option & MACH_SEND_KERNEL) == 0) {
2744 			mach_port_guard_exception(voucher_name, 0,
2745 			    (MPG_FLAGS_STRICT_REPLY_MISMATCHED_PERSONA | ((((uint64_t)persona_id << 32) & MPG_FLAGS_STRICT_REPLY_MASK) | dest_ctx)),
2746 			    kGUARD_EXC_STRICT_REPLY);
2747 		}
2748 		return MACH_SEND_INVALID_CONTEXT;
2749 	}
2750 
2751 	return MACH_MSG_SUCCESS;
2752 }
2753 
2754 /*
2755  *	Routine:	ipc_kmsg_copyin_header
2756  *	Purpose:
2757  *		"Copy-in" port rights in the header of a message.
2758  *		Operates atomically; if it doesn't succeed the
2759  *		message header and the space are left untouched.
2760  *		If it does succeed the remote/local port fields
2761  *		contain object pointers instead of port names,
2762  *		and the bits field is updated.  The destination port
2763  *		will be a valid port pointer.
2764  *
2765  *	Conditions:
2766  *		Nothing locked.
2767  *	Returns:
2768  *		MACH_MSG_SUCCESS	Successful copyin.
2769  *		MACH_SEND_INVALID_HEADER
2770  *			Illegal value in the message header bits.
2771  *		MACH_SEND_INVALID_DEST	The space is dead.
2772  *		MACH_SEND_INVALID_DEST	Can't copyin destination port.
2773  *			(Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2774  *		MACH_SEND_INVALID_REPLY	Can't copyin reply port.
2775  *			(Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2776  */
2777 
2778 static mach_msg_return_t
ipc_kmsg_copyin_header(ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_priority_t priority,mach_msg_option_t * optionp)2779 ipc_kmsg_copyin_header(
2780 	ipc_kmsg_t              kmsg,
2781 	ipc_space_t             space,
2782 	mach_msg_priority_t     priority,
2783 	mach_msg_option_t       *optionp)
2784 {
2785 	mach_msg_header_t *msg = kmsg->ikm_header;
2786 	mach_msg_bits_t mbits = msg->msgh_bits & MACH_MSGH_BITS_USER;
2787 	mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(msg->msgh_remote_port);
2788 	mach_port_name_t reply_name = CAST_MACH_PORT_TO_NAME(msg->msgh_local_port);
2789 	mach_port_name_t voucher_name = MACH_PORT_NULL;
2790 	kern_return_t kr;
2791 
2792 	mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
2793 	mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
2794 	mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
2795 	ipc_object_t dest_port = IO_NULL;
2796 	ipc_object_t reply_port = IO_NULL;
2797 	ipc_port_t dest_soright = IP_NULL;
2798 	ipc_port_t dport = IP_NULL;
2799 	ipc_port_t reply_soright = IP_NULL;
2800 	ipc_port_t voucher_soright = IP_NULL;
2801 	ipc_port_t release_port = IP_NULL;
2802 	ipc_port_t voucher_port = IP_NULL;
2803 	ipc_port_t voucher_release_port = IP_NULL;
2804 	ipc_entry_t dest_entry = IE_NULL;
2805 	ipc_entry_t reply_entry = IE_NULL;
2806 	ipc_entry_t voucher_entry = IE_NULL;
2807 
2808 	int assertcnt = 0;
2809 #if IMPORTANCE_INHERITANCE
2810 	boolean_t needboost = FALSE;
2811 #endif /* IMPORTANCE_INHERITANCE */
2812 
2813 	if ((mbits != msg->msgh_bits) ||
2814 	    (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type)) ||
2815 	    ((reply_type == 0) ?
2816 	    (reply_name != MACH_PORT_NULL) :
2817 	    !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type))) {
2818 		return MACH_SEND_INVALID_HEADER;
2819 	}
2820 
2821 	if (!MACH_PORT_VALID(dest_name)) {
2822 		return MACH_SEND_INVALID_DEST;
2823 	}
2824 
2825 	is_write_lock(space);
2826 	if (!is_active(space)) {
2827 		is_write_unlock(space);
2828 		return MACH_SEND_INVALID_DEST;
2829 	}
2830 	/* space locked and active */
2831 
2832 	/*
2833 	 *	If there is a voucher specified, make sure the disposition is
2834 	 *	valid and the entry actually refers to a voucher port.  Don't
2835 	 *	actually copy in until we validate destination and reply.
2836 	 */
2837 	if (voucher_type != MACH_MSGH_BITS_ZERO) {
2838 		voucher_name = msg->msgh_voucher_port;
2839 
2840 		if (voucher_name == MACH_PORT_DEAD ||
2841 		    (voucher_type != MACH_MSG_TYPE_MOVE_SEND &&
2842 		    voucher_type != MACH_MSG_TYPE_COPY_SEND)) {
2843 			is_write_unlock(space);
2844 			if ((*optionp & MACH_SEND_KERNEL) == 0) {
2845 				mach_port_guard_exception(voucher_name, 0, 0, kGUARD_EXC_SEND_INVALID_VOUCHER);
2846 			}
2847 			return MACH_SEND_INVALID_VOUCHER;
2848 		}
2849 
2850 		if (voucher_name != MACH_PORT_NULL) {
2851 			voucher_entry = ipc_entry_lookup(space, voucher_name);
2852 			if (voucher_entry == IE_NULL ||
2853 			    (voucher_entry->ie_bits & MACH_PORT_TYPE_SEND) == 0 ||
2854 			    io_kotype(voucher_entry->ie_object) != IKOT_VOUCHER) {
2855 				is_write_unlock(space);
2856 				if ((*optionp & MACH_SEND_KERNEL) == 0) {
2857 					mach_port_guard_exception(voucher_name, 0, 0, kGUARD_EXC_SEND_INVALID_VOUCHER);
2858 				}
2859 				return MACH_SEND_INVALID_VOUCHER;
2860 			}
2861 		} else {
2862 			voucher_type = MACH_MSG_TYPE_MOVE_SEND;
2863 		}
2864 	}
2865 
2866 	if (enforce_strict_reply && MACH_SEND_WITH_STRICT_REPLY(*optionp) &&
2867 	    (!MACH_PORT_VALID(reply_name) ||
2868 	    ((reply_type != MACH_MSG_TYPE_MAKE_SEND_ONCE) && (reply_type != MACH_MSG_TYPE_MOVE_SEND_ONCE))
2869 	    )) {
2870 		/*
2871 		 * The caller cannot enforce a reply context with an invalid
2872 		 * reply port name, or a non-send_once reply disposition.
2873 		 */
2874 		is_write_unlock(space);
2875 		if ((*optionp & MACH_SEND_KERNEL) == 0) {
2876 			mach_port_guard_exception(reply_name, 0,
2877 			    (MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_DISP | reply_type),
2878 			    kGUARD_EXC_STRICT_REPLY);
2879 		}
2880 		return MACH_SEND_INVALID_REPLY;
2881 	}
2882 
2883 	/*
2884 	 *	Handle combinations of validating destination and reply; along
2885 	 *	with copying in destination, reply, and voucher in an atomic way.
2886 	 */
2887 
2888 	if (dest_name == voucher_name) {
2889 		/*
2890 		 *	If the destination name is the same as the voucher name,
2891 		 *	the voucher_entry must already be known.  Either that or
2892 		 *	the destination name is MACH_PORT_NULL (i.e. invalid).
2893 		 */
2894 		dest_entry = voucher_entry;
2895 		if (dest_entry == IE_NULL) {
2896 			goto invalid_dest;
2897 		}
2898 
2899 		/*
2900 		 *	Make sure a future copyin of the reply port will succeed.
2901 		 *	Once we start copying in the dest/voucher pair, we can't
2902 		 *	back out.
2903 		 */
2904 		if (MACH_PORT_VALID(reply_name)) {
2905 			assert(reply_type != 0); /* because reply_name not null */
2906 
2907 			/* It is just WRONG if dest, voucher, and reply are all the same. */
2908 			if (voucher_name == reply_name) {
2909 				goto invalid_reply;
2910 			}
2911 			reply_entry = ipc_entry_lookup(space, reply_name);
2912 			if (reply_entry == IE_NULL) {
2913 				goto invalid_reply;
2914 			}
2915 			assert(dest_entry != reply_entry); /* names are not equal */
2916 			if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type)) {
2917 				goto invalid_reply;
2918 			}
2919 		}
2920 
2921 		/*
2922 		 *	Do the joint copyin of the dest disposition and
2923 		 *	voucher disposition from the one entry/port.  We
2924 		 *	already validated that the voucher copyin would
2925 		 *	succeed (above).  So, any failure in combining
2926 		 *	the copyins can be blamed on the destination.
2927 		 */
2928 		kr = ipc_right_copyin_two(space, dest_name, dest_entry,
2929 		    dest_type, voucher_type, &dest_port, &dest_soright,
2930 		    &release_port);
2931 		if (kr != KERN_SUCCESS) {
2932 			assert(kr != KERN_INVALID_CAPABILITY);
2933 			goto invalid_dest;
2934 		}
2935 		voucher_port = ip_object_to_port(dest_port);
2936 
2937 		/*
2938 		 * could not have been one of these dispositions,
2939 		 * validated the port was a true kernel voucher port above,
2940 		 * AND was successfully able to copyin both dest and voucher.
2941 		 */
2942 		assert(dest_type != MACH_MSG_TYPE_MAKE_SEND);
2943 		assert(dest_type != MACH_MSG_TYPE_MAKE_SEND_ONCE);
2944 		assert(dest_type != MACH_MSG_TYPE_MOVE_SEND_ONCE);
2945 
2946 		/*
2947 		 *	Perform the delayed reply right copyin (guaranteed success).
2948 		 */
2949 		if (reply_entry != IE_NULL) {
2950 			kr = ipc_right_copyin(space, reply_name, reply_entry,
2951 			    reply_type, IPC_OBJECT_COPYIN_FLAGS_DEADOK,
2952 			    &reply_port, &reply_soright,
2953 			    &release_port, &assertcnt, 0, NULL);
2954 			assert(assertcnt == 0);
2955 			assert(kr == KERN_SUCCESS);
2956 		}
2957 	} else {
2958 		if (dest_name == reply_name) {
2959 			/*
2960 			 *	Destination and reply ports are the same!
2961 			 *	This is very similar to the case where the
2962 			 *	destination and voucher ports were the same
2963 			 *	(except the reply port disposition is not
2964 			 *	previously validated).
2965 			 */
2966 			dest_entry = ipc_entry_lookup(space, dest_name);
2967 			if (dest_entry == IE_NULL) {
2968 				goto invalid_dest;
2969 			}
2970 
2971 			reply_entry = dest_entry;
2972 			assert(reply_type != 0); /* because name not null */
2973 
2974 			/*
2975 			 *	Pre-validate that the reply right can be copied in by itself.
2976 			 *  Fail if reply port is marked as immovable send.
2977 			 */
2978 			if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type)) {
2979 				goto invalid_reply;
2980 			}
2981 
2982 			/*
2983 			 *	Do the joint copyin of the dest disposition and
2984 			 *	reply disposition from the one entry/port.
2985 			 */
2986 			kr = ipc_right_copyin_two(space, dest_name, dest_entry,
2987 			    dest_type, reply_type, &dest_port, &dest_soright,
2988 			    &release_port);
2989 			if (kr == KERN_INVALID_CAPABILITY) {
2990 				goto invalid_reply;
2991 			} else if (kr != KERN_SUCCESS) {
2992 				goto invalid_dest;
2993 			}
2994 			reply_port = dest_port;
2995 		} else {
2996 			/*
2997 			 *	Handle destination and reply independently, as
2998 			 *	they are independent entries (even if the entries
2999 			 *	refer to the same port).
3000 			 *
3001 			 *	This can be the tough case to make atomic.
3002 			 *
3003 			 *	The difficult problem is serializing with port death.
3004 			 *	The bad case is when dest_port dies after its copyin,
3005 			 *	reply_port dies before its copyin, and dest_port dies before
3006 			 *	reply_port.  Then the copyins operated as if dest_port was
3007 			 *	alive and reply_port was dead, which shouldn't have happened
3008 			 *	because they died in the other order.
3009 			 *
3010 			 *	Note that it is easy for a user task to tell if
3011 			 *	a copyin happened before or after a port died.
3012 			 *	If a port dies before copyin, a dead-name notification
3013 			 *	is generated and the dead name's urefs are incremented,
3014 			 *	and if the copyin happens first, a port-deleted
3015 			 *	notification is generated.
3016 			 *
3017 			 *	Even so, avoiding that potentially detectable race is too
3018 			 *	expensive - and no known code cares about it.  So, we just
3019 			 *	do the expedient thing and copy them in one after the other.
3020 			 */
3021 
3022 			dest_entry = ipc_entry_lookup(space, dest_name);
3023 			if (dest_entry == IE_NULL) {
3024 				goto invalid_dest;
3025 			}
3026 			assert(dest_entry != voucher_entry);
3027 
3028 			/*
3029 			 *	Make sure reply port entry is valid before dest copyin.
3030 			 */
3031 			if (MACH_PORT_VALID(reply_name)) {
3032 				if (reply_name == voucher_name) {
3033 					goto invalid_reply;
3034 				}
3035 				reply_entry = ipc_entry_lookup(space, reply_name);
3036 				if (reply_entry == IE_NULL) {
3037 					goto invalid_reply;
3038 				}
3039 				assert(dest_entry != reply_entry); /* names are not equal */
3040 				assert(reply_type != 0); /* because reply_name not null */
3041 
3042 				if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type)) {
3043 					goto invalid_reply;
3044 				}
3045 			}
3046 
3047 			/*
3048 			 *	copyin the destination.
3049 			 */
3050 			kr = ipc_right_copyin(space, dest_name, dest_entry,
3051 			    dest_type, (IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND |
3052 			    IPC_OBJECT_COPYIN_FLAGS_ALLOW_DEAD_SEND_ONCE),
3053 			    &dest_port, &dest_soright,
3054 			    &release_port, &assertcnt, 0, NULL);
3055 			assert(assertcnt == 0);
3056 			if (kr != KERN_SUCCESS) {
3057 				goto invalid_dest;
3058 			}
3059 			assert(IO_VALID(dest_port));
3060 			assert(!IP_VALID(release_port));
3061 
3062 			/*
3063 			 *	Copyin the pre-validated reply right.
3064 			 *	It's OK if the reply right has gone dead in the meantime.
3065 			 */
3066 			if (MACH_PORT_VALID(reply_name)) {
3067 				kr = ipc_right_copyin(space, reply_name, reply_entry,
3068 				    reply_type, IPC_OBJECT_COPYIN_FLAGS_DEADOK,
3069 				    &reply_port, &reply_soright,
3070 				    &release_port, &assertcnt, 0, NULL);
3071 				assert(assertcnt == 0);
3072 				assert(kr == KERN_SUCCESS);
3073 			} else {
3074 				/* convert invalid name to equivalent ipc_object type */
3075 				reply_port = ip_to_object(CAST_MACH_NAME_TO_PORT(reply_name));
3076 			}
3077 		}
3078 
3079 		/*
3080 		 * Finally can copyin the voucher right now that dest and reply
3081 		 * are fully copied in (guaranteed success).
3082 		 */
3083 		if (IE_NULL != voucher_entry) {
3084 			kr = ipc_right_copyin(space, voucher_name, voucher_entry,
3085 			    voucher_type, IPC_OBJECT_COPYIN_FLAGS_NONE,
3086 			    (ipc_object_t *)&voucher_port,
3087 			    &voucher_soright,
3088 			    &voucher_release_port,
3089 			    &assertcnt, 0, NULL);
3090 			assert(assertcnt == 0);
3091 			assert(KERN_SUCCESS == kr);
3092 			assert(IP_VALID(voucher_port));
3093 			require_ip_active(voucher_port);
3094 		}
3095 	}
3096 
3097 	dest_type = ipc_object_copyin_type(dest_type);
3098 	reply_type = ipc_object_copyin_type(reply_type);
3099 
3100 	dport = ip_object_to_port(dest_port);
3101 	/*
3102 	 *	If the dest port died, or is a kobject AND its receive right belongs to kernel,
3103 	 *  allow copyin of immovable send rights in the message body (port descriptor) to
3104 	 *  succeed since those send rights are simply "moved" or "copied" into kernel.
3105 	 *
3106 	 *  See: ipc_object_copyin().
3107 	 */
3108 
3109 	ip_mq_lock(dport);
3110 
3111 	if (!ip_active(dport) || (ip_is_kobject(dport) &&
3112 	    ip_in_space(dport, ipc_space_kernel))) {
3113 		assert(ip_kotype(dport) != IKOT_HOST_NOTIFY && ip_kotype(dport) != IKOT_TIMER);
3114 		kmsg->ikm_flags |= IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND;
3115 	}
3116 
3117 	/*
3118 	 * JMM - Without rdar://problem/6275821, this is the last place we can
3119 	 * re-arm the send-possible notifications.  It may trigger unexpectedly
3120 	 * early (send may NOT have failed), but better than missing.  We assure
3121 	 * we won't miss by forcing MACH_SEND_ALWAYS if we got past arming.
3122 	 */
3123 	if (((*optionp & MACH_SEND_NOTIFY) != 0) &&
3124 	    dest_type != MACH_MSG_TYPE_PORT_SEND_ONCE &&
3125 	    dest_entry != IE_NULL && dest_entry->ie_request != IE_REQ_NONE) {
3126 		/* dport still locked from above */
3127 		if (ip_active(dport) && !ip_in_space(dport, ipc_space_kernel)) {
3128 			/* dport could be in-transit, or in an ipc space */
3129 			if (ip_full(dport)) {
3130 #if IMPORTANCE_INHERITANCE
3131 				needboost = ipc_port_request_sparm(dport, dest_name,
3132 				    dest_entry->ie_request,
3133 				    *optionp,
3134 				    priority);
3135 				if (needboost == FALSE) {
3136 					ip_mq_unlock(dport);
3137 				}
3138 #else
3139 				ipc_port_request_sparm(dport, dest_name,
3140 				    dest_entry->ie_request,
3141 				    *optionp,
3142 				    priority);
3143 				ip_mq_unlock(dport);
3144 #endif /* IMPORTANCE_INHERITANCE */
3145 			} else {
3146 				*optionp |= MACH_SEND_ALWAYS;
3147 				ip_mq_unlock(dport);
3148 			}
3149 		} else {
3150 			ip_mq_unlock(dport);
3151 		}
3152 	} else {
3153 		ip_mq_unlock(dport);
3154 	}
3155 	/* dport is unlocked, unless needboost == TRUE */
3156 
3157 	is_write_unlock(space);
3158 
3159 #if IMPORTANCE_INHERITANCE
3160 	/*
3161 	 * If our request is the first boosting send-possible
3162 	 * notification this cycle, push the boost down the
3163 	 * destination port.
3164 	 */
3165 	if (needboost == TRUE) {
3166 		/* dport still locked from above */
3167 		if (ipc_port_importance_delta(dport, IPID_OPTION_SENDPOSSIBLE, 1) == FALSE) {
3168 			ip_mq_unlock(dport);
3169 		}
3170 	}
3171 #endif /* IMPORTANCE_INHERITANCE */
3172 
3173 	/* dport is unlocked */
3174 
3175 	if (dest_soright != IP_NULL) {
3176 		ipc_notify_port_deleted(dest_soright, dest_name);
3177 	}
3178 	if (reply_soright != IP_NULL) {
3179 		ipc_notify_port_deleted(reply_soright, reply_name);
3180 	}
3181 	if (voucher_soright != IP_NULL) {
3182 		ipc_notify_port_deleted(voucher_soright, voucher_name);
3183 	}
3184 
3185 	/*
3186 	 * No room to store voucher port in in-kernel msg header,
3187 	 * so we store it back in the kmsg itself. Store original voucher
3188 	 * type there as well, but set the bits to the post-copyin type.
3189 	 */
3190 	if (IP_VALID(voucher_port)) {
3191 		ipc_kmsg_set_voucher_port(kmsg, voucher_port, voucher_type);
3192 		voucher_type = MACH_MSG_TYPE_MOVE_SEND;
3193 	}
3194 
3195 	msg->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, voucher_type, mbits);
3196 	msg->msgh_remote_port = ip_object_to_port(dest_port);
3197 	msg->msgh_local_port = ip_object_to_port(reply_port);
3198 
3199 	/*
3200 	 * capture the qos value(s) for the kmsg qos,
3201 	 * and apply any override before we enqueue the kmsg.
3202 	 */
3203 	ipc_kmsg_set_qos(kmsg, *optionp, priority);
3204 
3205 	if (release_port != IP_NULL) {
3206 		ip_release(release_port);
3207 	}
3208 
3209 	if (voucher_release_port != IP_NULL) {
3210 		ip_release(voucher_release_port);
3211 	}
3212 
3213 	if (enforce_strict_reply && MACH_SEND_WITH_STRICT_REPLY(*optionp) && IP_VALID(msg->msgh_local_port)) {
3214 		/*
3215 		 * We've already validated that the reply disposition is a
3216 		 * [make/move] send-once. Ideally, we should enforce that the
3217 		 * reply port is also not dead, but XPC asynchronous
3218 		 * cancellation can make the reply port dead before we
3219 		 * actually make it to the mach_msg send.
3220 		 *
3221 		 * Here, we ensure that if we have a non-dead reply port, then
3222 		 * the reply port's receive right should not be in-transit,
3223 		 * and should live in the caller's IPC space.
3224 		 */
3225 		ipc_port_t rport = msg->msgh_local_port;
3226 		ip_mq_lock(rport);
3227 		kr = ipc_kmsg_validate_reply_port_locked(rport, *optionp);
3228 		ip_mq_unlock(rport);
3229 		if (kr != KERN_SUCCESS) {
3230 			/*
3231 			 * no descriptors have been copied in yet, but the
3232 			 * full header has been copied in: clean it up
3233 			 */
3234 			ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
3235 			if ((*optionp & MACH_SEND_KERNEL) == 0) {
3236 				mach_port_guard_exception(reply_name, 0,
3237 				    (MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_PORT | kr),
3238 				    kGUARD_EXC_STRICT_REPLY);
3239 			}
3240 			return MACH_SEND_INVALID_REPLY;
3241 		}
3242 	}
3243 
3244 	return MACH_MSG_SUCCESS;
3245 
3246 invalid_reply:
3247 	is_write_unlock(space);
3248 
3249 	if (release_port != IP_NULL) {
3250 		ip_release(release_port);
3251 	}
3252 
3253 	assert(voucher_port == IP_NULL);
3254 	assert(voucher_soright == IP_NULL);
3255 
3256 	if ((*optionp & MACH_SEND_KERNEL) == 0) {
3257 		mach_port_guard_exception(reply_name, 0, 0, kGUARD_EXC_SEND_INVALID_REPLY);
3258 	}
3259 	return MACH_SEND_INVALID_REPLY;
3260 
3261 invalid_dest:
3262 	is_write_unlock(space);
3263 
3264 	if (release_port != IP_NULL) {
3265 		ip_release(release_port);
3266 	}
3267 
3268 	if (reply_soright != IP_NULL) {
3269 		ipc_notify_port_deleted(reply_soright, reply_name);
3270 	}
3271 
3272 	assert(voucher_port == IP_NULL);
3273 	assert(voucher_soright == IP_NULL);
3274 
3275 	return MACH_SEND_INVALID_DEST;
3276 }
3277 
3278 static mach_msg_descriptor_t *
ipc_kmsg_copyin_port_descriptor(mach_msg_port_descriptor_t * dsc,mach_msg_user_port_descriptor_t * user_dsc_in,ipc_space_t space,ipc_object_t dest,ipc_kmsg_t kmsg,mach_msg_option_t * optionp,mach_msg_return_t * mr)3279 ipc_kmsg_copyin_port_descriptor(
3280 	mach_msg_port_descriptor_t *dsc,
3281 	mach_msg_user_port_descriptor_t *user_dsc_in,
3282 	ipc_space_t space,
3283 	ipc_object_t dest,
3284 	ipc_kmsg_t kmsg,
3285 	mach_msg_option_t *optionp,
3286 	mach_msg_return_t *mr)
3287 {
3288 	mach_msg_user_port_descriptor_t user_dsc = *user_dsc_in;
3289 	mach_msg_type_name_t        user_disp;
3290 	mach_msg_type_name_t        result_disp;
3291 	mach_port_name_t            name;
3292 	ipc_object_t                object;
3293 
3294 	user_disp = user_dsc.disposition;
3295 	result_disp = ipc_object_copyin_type(user_disp);
3296 
3297 	name = (mach_port_name_t)user_dsc.name;
3298 	if (MACH_PORT_VALID(name)) {
3299 		kern_return_t kr = ipc_object_copyin(space, name, user_disp, &object, 0, NULL, kmsg->ikm_flags);
3300 		if (kr != KERN_SUCCESS) {
3301 			if (((*optionp & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
3302 				mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
3303 			}
3304 			*mr = MACH_SEND_INVALID_RIGHT;
3305 			return NULL;
3306 		}
3307 
3308 		if ((result_disp == MACH_MSG_TYPE_PORT_RECEIVE) &&
3309 		    ipc_port_check_circularity(ip_object_to_port(object),
3310 		    ip_object_to_port(dest))) {
3311 			kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
3312 		}
3313 		dsc->name = ip_object_to_port(object);
3314 	} else {
3315 		dsc->name = CAST_MACH_NAME_TO_PORT(name);
3316 	}
3317 	dsc->disposition = result_disp;
3318 	dsc->type = MACH_MSG_PORT_DESCRIPTOR;
3319 
3320 	dsc->pad_end = 0;         // debug, unnecessary
3321 
3322 	return (mach_msg_descriptor_t *)(user_dsc_in + 1);
3323 }
3324 
3325 static mach_msg_descriptor_t *
ipc_kmsg_copyin_ool_descriptor(mach_msg_ool_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,mach_vm_address_t * paddr,vm_map_copy_t * copy,vm_size_t * space_needed,vm_map_t map,__unused mach_msg_option_t * optionp,mach_msg_return_t * mr)3326 ipc_kmsg_copyin_ool_descriptor(
3327 	mach_msg_ool_descriptor_t *dsc,
3328 	mach_msg_descriptor_t *user_dsc,
3329 	int is_64bit,
3330 	mach_vm_address_t *paddr,
3331 	vm_map_copy_t *copy,
3332 	vm_size_t *space_needed,
3333 	vm_map_t map,
3334 	__unused mach_msg_option_t *optionp,
3335 	mach_msg_return_t *mr)
3336 {
3337 	vm_size_t                           length;
3338 	boolean_t                           dealloc;
3339 	mach_msg_copy_options_t             copy_options;
3340 	mach_vm_offset_t            addr;
3341 	mach_msg_descriptor_type_t  dsc_type;
3342 
3343 	if (is_64bit) {
3344 		mach_msg_ool_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3345 
3346 		addr = (mach_vm_offset_t) user_ool_dsc->address;
3347 		length = user_ool_dsc->size;
3348 		dealloc = user_ool_dsc->deallocate;
3349 		copy_options = user_ool_dsc->copy;
3350 		dsc_type = user_ool_dsc->type;
3351 
3352 		user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3353 	} else {
3354 		mach_msg_ool_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3355 
3356 		addr = CAST_USER_ADDR_T(user_ool_dsc->address);
3357 		dealloc = user_ool_dsc->deallocate;
3358 		copy_options = user_ool_dsc->copy;
3359 		dsc_type = user_ool_dsc->type;
3360 		length = user_ool_dsc->size;
3361 
3362 		user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3363 	}
3364 
3365 	dsc->size = (mach_msg_size_t)length;
3366 	dsc->deallocate = dealloc;
3367 	dsc->copy = copy_options;
3368 	dsc->type = dsc_type;
3369 
3370 	if (length == 0) {
3371 		dsc->address = NULL;
3372 	} else if (length > MSG_OOL_SIZE_SMALL &&
3373 	    (copy_options == MACH_MSG_PHYSICAL_COPY) && !dealloc) {
3374 		/*
3375 		 * If the request is a physical copy and the source
3376 		 * is not being deallocated, then allocate space
3377 		 * in the kernel's pageable ipc copy map and copy
3378 		 * the data in.  The semantics guarantee that the
3379 		 * data will have been physically copied before
3380 		 * the send operation terminates.  Thus if the data
3381 		 * is not being deallocated, we must be prepared
3382 		 * to page if the region is sufficiently large.
3383 		 */
3384 		if (copyin(addr, (char *)*paddr, length)) {
3385 			*mr = MACH_SEND_INVALID_MEMORY;
3386 			return NULL;
3387 		}
3388 
3389 		/*
3390 		 * The kernel ipc copy map is marked no_zero_fill.
3391 		 * If the transfer is not a page multiple, we need
3392 		 * to zero fill the balance.
3393 		 */
3394 		if (!page_aligned(length)) {
3395 			(void) memset((void *) (*paddr + length), 0,
3396 			    round_page(length) - length);
3397 		}
3398 		if (vm_map_copyin(ipc_kernel_copy_map, (vm_map_address_t)*paddr,
3399 		    (vm_map_size_t)length, TRUE, copy) != KERN_SUCCESS) {
3400 			*mr = MACH_MSG_VM_KERNEL;
3401 			return NULL;
3402 		}
3403 		dsc->address = (void *)*copy;
3404 		*paddr += round_page(length);
3405 		*space_needed -= round_page(length);
3406 	} else {
3407 		/*
3408 		 * Make a vm_map_copy_t of the of the data.  If the
3409 		 * data is small, this will do an optimized physical
3410 		 * copy.  Otherwise, it will do a virtual copy.
3411 		 *
3412 		 * NOTE: A virtual copy is OK if the original is being
3413 		 * deallocted, even if a physical copy was requested.
3414 		 */
3415 		kern_return_t kr = vm_map_copyin(map, addr,
3416 		    (vm_map_size_t)length, dealloc, copy);
3417 		if (kr != KERN_SUCCESS) {
3418 			*mr = (kr == KERN_RESOURCE_SHORTAGE) ?
3419 			    MACH_MSG_VM_KERNEL :
3420 			    MACH_SEND_INVALID_MEMORY;
3421 			return NULL;
3422 		}
3423 		dsc->address = (void *)*copy;
3424 	}
3425 
3426 	return user_dsc;
3427 }
3428 
3429 static mach_msg_descriptor_t *
ipc_kmsg_copyin_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,vm_map_t map,ipc_space_t space,ipc_object_t dest,ipc_kmsg_t kmsg,mach_msg_option_t * optionp,mach_msg_return_t * mr)3430 ipc_kmsg_copyin_ool_ports_descriptor(
3431 	mach_msg_ool_ports_descriptor_t *dsc,
3432 	mach_msg_descriptor_t *user_dsc,
3433 	int is_64bit,
3434 	vm_map_t map,
3435 	ipc_space_t space,
3436 	ipc_object_t dest,
3437 	ipc_kmsg_t kmsg,
3438 	mach_msg_option_t *optionp,
3439 	mach_msg_return_t *mr)
3440 {
3441 	void *data;
3442 	ipc_object_t *objects;
3443 	unsigned int i;
3444 	mach_vm_offset_t addr;
3445 	mach_msg_type_name_t user_disp;
3446 	mach_msg_type_name_t result_disp;
3447 	mach_msg_type_number_t count;
3448 	mach_msg_copy_options_t copy_option;
3449 	boolean_t deallocate;
3450 	mach_msg_descriptor_type_t type;
3451 	vm_size_t ports_length, names_length;
3452 
3453 	if (is_64bit) {
3454 		mach_msg_ool_ports_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3455 
3456 		addr = (mach_vm_offset_t)user_ool_dsc->address;
3457 		count = user_ool_dsc->count;
3458 		deallocate = user_ool_dsc->deallocate;
3459 		copy_option = user_ool_dsc->copy;
3460 		user_disp = user_ool_dsc->disposition;
3461 		type = user_ool_dsc->type;
3462 
3463 		user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3464 	} else {
3465 		mach_msg_ool_ports_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3466 
3467 		addr = CAST_USER_ADDR_T(user_ool_dsc->address);
3468 		count = user_ool_dsc->count;
3469 		deallocate = user_ool_dsc->deallocate;
3470 		copy_option = user_ool_dsc->copy;
3471 		user_disp = user_ool_dsc->disposition;
3472 		type = user_ool_dsc->type;
3473 
3474 		user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3475 	}
3476 
3477 	dsc->deallocate = deallocate;
3478 	dsc->copy = copy_option;
3479 	dsc->type = type;
3480 	dsc->count = count;
3481 	dsc->address = NULL; /* for now */
3482 
3483 	result_disp = ipc_object_copyin_type(user_disp);
3484 	dsc->disposition = result_disp;
3485 
3486 	/* We always do a 'physical copy', but you have to specify something valid */
3487 	if (copy_option != MACH_MSG_PHYSICAL_COPY &&
3488 	    copy_option != MACH_MSG_VIRTUAL_COPY) {
3489 		*mr = MACH_SEND_INVALID_TYPE;
3490 		return NULL;
3491 	}
3492 
3493 	/* calculate length of data in bytes, rounding up */
3494 
3495 	if (os_mul_overflow(count, sizeof(mach_port_t), &ports_length)) {
3496 		*mr = MACH_SEND_TOO_LARGE;
3497 		return NULL;
3498 	}
3499 
3500 	if (os_mul_overflow(count, sizeof(mach_port_name_t), &names_length)) {
3501 		*mr = MACH_SEND_TOO_LARGE;
3502 		return NULL;
3503 	}
3504 
3505 	if (ports_length == 0) {
3506 		return user_dsc;
3507 	}
3508 
3509 	data = kalloc_type(mach_port_t, count, Z_WAITOK);
3510 
3511 	if (data == NULL) {
3512 		*mr = MACH_SEND_NO_BUFFER;
3513 		return NULL;
3514 	}
3515 
3516 #ifdef __LP64__
3517 	mach_port_name_t *names = &((mach_port_name_t *)data)[count];
3518 #else
3519 	mach_port_name_t *names = ((mach_port_name_t *)data);
3520 #endif
3521 
3522 	if (copyinmap(map, addr, names, names_length) != KERN_SUCCESS) {
3523 		kfree_type(mach_port_t, count, data);
3524 		*mr = MACH_SEND_INVALID_MEMORY;
3525 		return NULL;
3526 	}
3527 
3528 	if (deallocate) {
3529 		(void) mach_vm_deallocate(map, addr, (mach_vm_size_t)names_length);
3530 	}
3531 
3532 	objects = (ipc_object_t *) data;
3533 	dsc->address = data;
3534 
3535 	for (i = 0; i < count; i++) {
3536 		mach_port_name_t name = names[i];
3537 		ipc_object_t object;
3538 
3539 		if (!MACH_PORT_VALID(name)) {
3540 			objects[i] = ip_to_object(CAST_MACH_NAME_TO_PORT(name));
3541 			continue;
3542 		}
3543 
3544 		kern_return_t kr = ipc_object_copyin(space, name, user_disp, &object, 0, NULL, kmsg->ikm_flags);
3545 
3546 		if (kr != KERN_SUCCESS) {
3547 			unsigned int j;
3548 
3549 			for (j = 0; j < i; j++) {
3550 				object = objects[j];
3551 				if (IPC_OBJECT_VALID(object)) {
3552 					ipc_object_destroy(object, result_disp);
3553 				}
3554 			}
3555 			kfree_type(mach_port_t, count, data);
3556 			dsc->address = NULL;
3557 			if (((*optionp & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
3558 				mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
3559 			}
3560 			*mr = MACH_SEND_INVALID_RIGHT;
3561 			return NULL;
3562 		}
3563 
3564 		if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
3565 		    ipc_port_check_circularity(ip_object_to_port(object),
3566 		    ip_object_to_port(dest))) {
3567 			kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
3568 		}
3569 
3570 		objects[i] = object;
3571 	}
3572 
3573 	return user_dsc;
3574 }
3575 
3576 static mach_msg_descriptor_t *
ipc_kmsg_copyin_guarded_port_descriptor(mach_msg_guarded_port_descriptor_t * dsc,mach_msg_descriptor_t * user_addr,int is_64bit,ipc_space_t space,ipc_object_t dest,ipc_kmsg_t kmsg,mach_msg_option_t * optionp,mach_msg_return_t * mr)3577 ipc_kmsg_copyin_guarded_port_descriptor(
3578 	mach_msg_guarded_port_descriptor_t *dsc,
3579 	mach_msg_descriptor_t *user_addr,
3580 	int is_64bit,
3581 	ipc_space_t space,
3582 	ipc_object_t dest,
3583 	ipc_kmsg_t kmsg,
3584 	mach_msg_option_t *optionp,
3585 	mach_msg_return_t *mr)
3586 {
3587 	mach_msg_descriptor_t       *user_dsc;
3588 	mach_msg_type_name_t        disp;
3589 	mach_msg_type_name_t        result_disp;
3590 	mach_port_name_t            name;
3591 	mach_msg_guard_flags_t      guard_flags;
3592 	ipc_object_t                object;
3593 	mach_port_context_t         context;
3594 
3595 	if (!is_64bit) {
3596 		mach_msg_guarded_port_descriptor32_t *user_gp_dsc = (typeof(user_gp_dsc))user_addr;
3597 		name = user_gp_dsc->name;
3598 		guard_flags = user_gp_dsc->flags;
3599 		disp = user_gp_dsc->disposition;
3600 		context = user_gp_dsc->context;
3601 		user_dsc = (mach_msg_descriptor_t *)(user_gp_dsc + 1);
3602 	} else {
3603 		mach_msg_guarded_port_descriptor64_t *user_gp_dsc = (typeof(user_gp_dsc))user_addr;
3604 		name = user_gp_dsc->name;
3605 		guard_flags = user_gp_dsc->flags;
3606 		disp = user_gp_dsc->disposition;
3607 		context = user_gp_dsc->context;
3608 		user_dsc = (mach_msg_descriptor_t *)(user_gp_dsc + 1);
3609 	}
3610 
3611 	guard_flags &= MACH_MSG_GUARD_FLAGS_MASK;
3612 	result_disp = ipc_object_copyin_type(disp);
3613 
3614 	if (MACH_PORT_VALID(name)) {
3615 		kern_return_t kr = ipc_object_copyin(space, name, disp, &object, context, &guard_flags, kmsg->ikm_flags);
3616 		if (kr != KERN_SUCCESS) {
3617 			if (((*optionp & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
3618 				mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
3619 			}
3620 			*mr = MACH_SEND_INVALID_RIGHT;
3621 			return NULL;
3622 		}
3623 
3624 		if ((result_disp == MACH_MSG_TYPE_PORT_RECEIVE) &&
3625 		    ipc_port_check_circularity(ip_object_to_port(object),
3626 		    ip_object_to_port(dest))) {
3627 			kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
3628 		}
3629 		dsc->name = ip_object_to_port(object);
3630 	} else {
3631 		dsc->name = CAST_MACH_NAME_TO_PORT(name);
3632 	}
3633 	dsc->flags = guard_flags;
3634 	dsc->disposition = result_disp;
3635 	dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
3636 
3637 #if __LP64__
3638 	dsc->pad_end = 0;         // debug, unnecessary
3639 #endif
3640 
3641 	return user_dsc;
3642 }
3643 
3644 
3645 /*
3646  *	Routine:	ipc_kmsg_copyin_body
3647  *	Purpose:
3648  *		"Copy-in" port rights and out-of-line memory
3649  *		in the message body.
3650  *
3651  *		In all failure cases, the message is left holding
3652  *		no rights or memory.  However, the message buffer
3653  *		is not deallocated.  If successful, the message
3654  *		contains a valid destination port.
3655  *	Conditions:
3656  *		Nothing locked.
3657  *	Returns:
3658  *		MACH_MSG_SUCCESS	Successful copyin.
3659  *		MACH_SEND_INVALID_MEMORY	Can't grab out-of-line memory.
3660  *		MACH_SEND_INVALID_RIGHT	Can't copyin port right in body.
3661  *		MACH_SEND_INVALID_TYPE	Bad type specification.
3662  *		MACH_SEND_MSG_TOO_SMALL	Body is too small for types/data.
3663  *		MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
3664  *		MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
3665  *		MACH_SEND_NO_GRANT_DEST	Dest port doesn't accept ports in body
3666  */
3667 
3668 static mach_msg_return_t
ipc_kmsg_copyin_body(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_option_t * optionp)3669 ipc_kmsg_copyin_body(
3670 	ipc_kmsg_t      kmsg,
3671 	ipc_space_t     space,
3672 	vm_map_t    map,
3673 	mach_msg_option_t *optionp)
3674 {
3675 	ipc_object_t                dest;
3676 	mach_msg_body_t             *body;
3677 	mach_msg_descriptor_t       *daddr;
3678 	mach_msg_descriptor_t       *user_addr, *kern_addr;
3679 	mach_msg_type_number_t      dsc_count;
3680 	boolean_t                   is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
3681 	boolean_t                   complex = FALSE;
3682 	boolean_t                   contains_port_desc = FALSE;
3683 	vm_size_t                   space_needed = 0;
3684 	mach_vm_address_t           paddr = 0;
3685 	vm_offset_t                 end;
3686 	vm_map_copy_t               copy = VM_MAP_COPY_NULL;
3687 	mach_msg_return_t           mr = MACH_MSG_SUCCESS;
3688 	ipc_port_t                  remote_port = kmsg->ikm_header->msgh_remote_port;
3689 
3690 	vm_size_t           descriptor_size = 0;
3691 
3692 	mach_msg_type_number_t total_ool_port_count = 0;
3693 	mach_msg_guard_flags_t guard_flags = 0;
3694 	mach_port_context_t context;
3695 	mach_msg_type_name_t disp;
3696 
3697 	/*
3698 	 * Determine if the target is a kernel port.
3699 	 */
3700 	dest = ip_to_object(remote_port);
3701 	body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
3702 	daddr = (mach_msg_descriptor_t *) (body + 1);
3703 	end = ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
3704 
3705 	dsc_count = body->msgh_descriptor_count;
3706 	if (dsc_count == 0) {
3707 		return MACH_MSG_SUCCESS;
3708 	}
3709 
3710 	/*
3711 	 * Make an initial pass to determine kernal VM space requirements for
3712 	 * physical copies and possible contraction of the descriptors from
3713 	 * processes with pointers larger than the kernel's.
3714 	 */
3715 	for (mach_msg_type_number_t i = 0; i < dsc_count; i++) {
3716 		mach_msg_size_t dsize;
3717 		mach_msg_size_t size;
3718 		mach_msg_type_number_t ool_port_count = 0;
3719 
3720 		if ((vm_offset_t)daddr + MACH_MSG_DESC_MIN_SIZE > end) {
3721 			mr = MACH_SEND_MSG_TOO_SMALL;
3722 			goto clean_message;
3723 		}
3724 
3725 		dsize = ipc_kmsg_user_desc_size(daddr->type.type, is_task_64bit);
3726 		if ((vm_offset_t)daddr + dsize > end) {
3727 			mr = MACH_SEND_MSG_TOO_SMALL;
3728 			goto clean_message;
3729 		}
3730 
3731 		switch (daddr->type.type) {
3732 		case MACH_MSG_OOL_DESCRIPTOR:
3733 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
3734 			size = (is_task_64bit) ?
3735 			    ((mach_msg_ool_descriptor64_t *)daddr)->size :
3736 			    daddr->out_of_line.size;
3737 
3738 			if (daddr->out_of_line.copy != MACH_MSG_PHYSICAL_COPY &&
3739 			    daddr->out_of_line.copy != MACH_MSG_VIRTUAL_COPY) {
3740 				/*
3741 				 * Invalid copy option
3742 				 */
3743 				mr = MACH_SEND_INVALID_TYPE;
3744 				goto clean_message;
3745 			}
3746 
3747 			if (size > MSG_OOL_SIZE_SMALL &&
3748 			    (daddr->out_of_line.copy == MACH_MSG_PHYSICAL_COPY) &&
3749 			    !(daddr->out_of_line.deallocate)) {
3750 				/*
3751 				 * Out-of-line memory descriptor, accumulate kernel
3752 				 * memory requirements
3753 				 */
3754 				if (space_needed + round_page(size) <= space_needed) {
3755 					/* Overflow dectected */
3756 					mr = MACH_MSG_VM_KERNEL;
3757 					goto clean_message;
3758 				}
3759 
3760 				space_needed += round_page(size);
3761 				if (space_needed > ipc_kmsg_max_vm_space) {
3762 					/* Per message kernel memory limit exceeded */
3763 					mr = MACH_MSG_VM_KERNEL;
3764 					goto clean_message;
3765 				}
3766 			}
3767 			break;
3768 		case MACH_MSG_PORT_DESCRIPTOR:
3769 			if (os_add_overflow(total_ool_port_count, 1, &total_ool_port_count)) {
3770 				/* Overflow detected */
3771 				mr = MACH_SEND_TOO_LARGE;
3772 				goto clean_message;
3773 			}
3774 			contains_port_desc = TRUE;
3775 			break;
3776 		case MACH_MSG_OOL_PORTS_DESCRIPTOR:
3777 			ool_port_count = (is_task_64bit) ?
3778 			    ((mach_msg_ool_ports_descriptor64_t *)daddr)->count :
3779 			    daddr->ool_ports.count;
3780 
3781 			if (os_add_overflow(total_ool_port_count, ool_port_count, &total_ool_port_count)) {
3782 				/* Overflow detected */
3783 				mr = MACH_SEND_TOO_LARGE;
3784 				goto clean_message;
3785 			}
3786 
3787 			if (ool_port_count > (ipc_kmsg_max_vm_space / sizeof(mach_port_t))) {
3788 				/* Per message kernel memory limit exceeded */
3789 				mr = MACH_SEND_TOO_LARGE;
3790 				goto clean_message;
3791 			}
3792 			contains_port_desc = TRUE;
3793 			break;
3794 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
3795 			guard_flags = (is_task_64bit) ?
3796 			    ((mach_msg_guarded_port_descriptor64_t *)daddr)->flags :
3797 			    ((mach_msg_guarded_port_descriptor32_t *)daddr)->flags;
3798 			context = (is_task_64bit) ?
3799 			    ((mach_msg_guarded_port_descriptor64_t *)daddr)->context :
3800 			    ((mach_msg_guarded_port_descriptor32_t *)daddr)->context;
3801 			disp = (is_task_64bit) ?
3802 			    ((mach_msg_guarded_port_descriptor64_t *)daddr)->disposition :
3803 			    ((mach_msg_guarded_port_descriptor32_t *)daddr)->disposition;
3804 
3805 			/* Only MACH_MSG_TYPE_MOVE_RECEIVE is supported for now */
3806 			if (!guard_flags || ((guard_flags & ~MACH_MSG_GUARD_FLAGS_MASK) != 0) ||
3807 			    ((guard_flags & MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND) && (context != 0)) ||
3808 			    (disp != MACH_MSG_TYPE_MOVE_RECEIVE)) {
3809 				/*
3810 				 * Invalid guard flags, context or disposition
3811 				 */
3812 				mr = MACH_SEND_INVALID_TYPE;
3813 				goto clean_message;
3814 			}
3815 			if (os_add_overflow(total_ool_port_count, 1, &total_ool_port_count)) {
3816 				/* Overflow detected */
3817 				mr = MACH_SEND_TOO_LARGE;
3818 				goto clean_message;
3819 			}
3820 			contains_port_desc = TRUE;
3821 			break;
3822 		default:
3823 			mr = MACH_SEND_INVALID_TYPE;
3824 			goto clean_message;
3825 		}
3826 
3827 		descriptor_size += dsize;
3828 		daddr = (typeof(daddr))((vm_offset_t)daddr + dsize);
3829 	}
3830 
3831 	/* Sending more than 16383 rights in one message seems crazy */
3832 	if (total_ool_port_count >= (MACH_PORT_UREFS_MAX / 4)) {
3833 		mr = MACH_SEND_TOO_LARGE;
3834 		goto clean_message;
3835 	}
3836 
3837 	/*
3838 	 * Check if dest is a no-grant port; Since this bit is set only on
3839 	 * port construction and cannot be unset later, we can peek at the
3840 	 * bit without paying the cost of locking the port.
3841 	 */
3842 	if (contains_port_desc && remote_port->ip_no_grant) {
3843 		mr = MACH_SEND_NO_GRANT_DEST;
3844 		goto clean_message;
3845 	}
3846 
3847 	/*
3848 	 * Allocate space in the pageable kernel ipc copy map for all the
3849 	 * ool data that is to be physically copied.  Map is marked wait for
3850 	 * space.
3851 	 */
3852 	if (space_needed) {
3853 		if (mach_vm_allocate_kernel(ipc_kernel_copy_map, &paddr, space_needed,
3854 		    VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IPC) != KERN_SUCCESS) {
3855 			mr = MACH_MSG_VM_KERNEL;
3856 			goto clean_message;
3857 		}
3858 	}
3859 
3860 	/* user_addr = just after base as it was copied in */
3861 	user_addr = (mach_msg_descriptor_t *)((vm_offset_t)kmsg->ikm_header + sizeof(mach_msg_base_t));
3862 
3863 	/* Shift the mach_msg_base_t down to make room for dsc_count*16bytes of descriptors on 64 bit kernels
3864 	 */
3865 	if (descriptor_size != KERNEL_DESC_SIZE * dsc_count) {
3866 		vm_offset_t dsc_adjust = KERNEL_DESC_SIZE * dsc_count - descriptor_size;
3867 
3868 		memmove((char *)(((vm_offset_t)kmsg->ikm_header) - dsc_adjust), kmsg->ikm_header, sizeof(mach_msg_base_t));
3869 		kmsg->ikm_header = (mach_msg_header_t *)((vm_offset_t)kmsg->ikm_header - dsc_adjust);
3870 
3871 		/* Update the message size for the larger in-kernel representation */
3872 		kmsg->ikm_header->msgh_size += (mach_msg_size_t)dsc_adjust;
3873 	}
3874 
3875 
3876 	/* kern_addr = just after base after it has been (conditionally) moved */
3877 	kern_addr = (mach_msg_descriptor_t *)((vm_offset_t)kmsg->ikm_header + sizeof(mach_msg_base_t));
3878 
3879 	/* handle the OOL regions and port descriptors. */
3880 	for (mach_msg_type_number_t copied_in_dscs = 0;
3881 	    copied_in_dscs < dsc_count; copied_in_dscs++) {
3882 		switch (user_addr->type.type) {
3883 		case MACH_MSG_PORT_DESCRIPTOR:
3884 			user_addr = ipc_kmsg_copyin_port_descriptor((mach_msg_port_descriptor_t *)kern_addr,
3885 			    (mach_msg_user_port_descriptor_t *)user_addr, space, dest, kmsg, optionp, &mr);
3886 			kern_addr++;
3887 			complex = TRUE;
3888 			break;
3889 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
3890 		case MACH_MSG_OOL_DESCRIPTOR:
3891 			user_addr = ipc_kmsg_copyin_ool_descriptor((mach_msg_ool_descriptor_t *)kern_addr,
3892 			    user_addr, is_task_64bit, &paddr, &copy, &space_needed, map, optionp, &mr);
3893 			kern_addr++;
3894 			complex = TRUE;
3895 			break;
3896 		case MACH_MSG_OOL_PORTS_DESCRIPTOR:
3897 			user_addr = ipc_kmsg_copyin_ool_ports_descriptor((mach_msg_ool_ports_descriptor_t *)kern_addr,
3898 			    user_addr, is_task_64bit, map, space, dest, kmsg, optionp, &mr);
3899 			kern_addr++;
3900 			complex = TRUE;
3901 			break;
3902 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
3903 			user_addr = ipc_kmsg_copyin_guarded_port_descriptor((mach_msg_guarded_port_descriptor_t *)kern_addr,
3904 			    user_addr, is_task_64bit, space, dest, kmsg, optionp, &mr);
3905 			kern_addr++;
3906 			complex = TRUE;
3907 			break;
3908 		default:
3909 			panic("invalid descriptor type %d", user_addr->type.type);
3910 		}
3911 
3912 		if (MACH_MSG_SUCCESS != mr) {
3913 			/* clean from start of message descriptors to copied_in_dscs */
3914 			ipc_kmsg_clean_partial(kmsg, copied_in_dscs,
3915 			    (mach_msg_descriptor_t *)((mach_msg_base_t *)kmsg->ikm_header + 1),
3916 			    paddr, space_needed);
3917 			goto out;
3918 		}
3919 	}         /* End of loop */
3920 
3921 	if (!complex) {
3922 		kmsg->ikm_header->msgh_bits &= ~MACH_MSGH_BITS_COMPLEX;
3923 	}
3924 out:
3925 	return mr;
3926 
3927 clean_message:
3928 	/* no descriptors have been copied in yet */
3929 	ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
3930 	return mr;
3931 }
3932 
3933 #define MACH_BOOTSTRAP_PORT_MSG_ID_MASK ((1ul << 24) - 1)
3934 
3935 /*
3936  *	Routine:	ipc_kmsg_copyin_from_user
3937  *	Purpose:
3938  *		"Copy-in" port rights and out-of-line memory
3939  *		in the message.
3940  *
3941  *		In all failure cases, the message is left holding
3942  *		no rights or memory.  However, the message buffer
3943  *		is not deallocated.  If successful, the message
3944  *		contains a valid destination port.
3945  *	Conditions:
3946  *		Nothing locked.
3947  *	Returns:
3948  *		MACH_MSG_SUCCESS	Successful copyin.
3949  *		MACH_SEND_INVALID_HEADER Illegal value in the message header bits.
3950  *		MACH_SEND_INVALID_DEST	Can't copyin destination port.
3951  *		MACH_SEND_INVALID_REPLY	Can't copyin reply port.
3952  *		MACH_SEND_INVALID_MEMORY	Can't grab out-of-line memory.
3953  *		MACH_SEND_INVALID_RIGHT	Can't copyin port right in body.
3954  *		MACH_SEND_INVALID_TYPE	Bad type specification.
3955  *		MACH_SEND_MSG_TOO_SMALL	Body is too small for types/data.
3956  */
3957 
3958 mach_msg_return_t
ipc_kmsg_copyin_from_user(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_priority_t priority,mach_msg_option_t * optionp,bool filter_nonfatal)3959 ipc_kmsg_copyin_from_user(
3960 	ipc_kmsg_t              kmsg,
3961 	ipc_space_t             space,
3962 	vm_map_t                map,
3963 	mach_msg_priority_t     priority,
3964 	mach_msg_option_t       *optionp,
3965 	bool                    filter_nonfatal)
3966 {
3967 	mach_msg_return_t           mr;
3968 	mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_remote_port);
3969 
3970 	kmsg->ikm_header->msgh_bits &= MACH_MSGH_BITS_USER;
3971 
3972 	mr = ipc_kmsg_copyin_header(kmsg, space, priority, optionp);
3973 
3974 	if (mr != MACH_MSG_SUCCESS) {
3975 		return mr;
3976 	}
3977 
3978 	/* Get the message filter policy if the task and port support filtering */
3979 	mach_msg_filter_id fid = 0;
3980 	mach_port_t remote_port = kmsg->ikm_header->msgh_remote_port;
3981 	mach_msg_id_t msg_id = kmsg->ikm_header->msgh_id;
3982 	void * sblabel = NULL;
3983 
3984 	if (mach_msg_filter_at_least(MACH_MSG_FILTER_CALLBACKS_VERSION_1) &&
3985 	    task_get_filter_msg_flag(current_task()) &&
3986 	    ip_enforce_msg_filtering(remote_port)) {
3987 		ip_mq_lock(remote_port);
3988 		if (ip_active(remote_port)) {
3989 			if (remote_port->ip_service_port) {
3990 				ipc_service_port_label_t label = remote_port->ip_splabel;
3991 				sblabel = label->ispl_sblabel;
3992 				if (label && ipc_service_port_label_is_bootstrap_port(label)) {
3993 					/*
3994 					 * Mask the top byte for messages sent to launchd's bootstrap port.
3995 					 * Filter any messages with domain 0 (as they correspond to MIG
3996 					 * based messages)
3997 					 */
3998 					unsigned msg_protocol = msg_id & ~MACH_BOOTSTRAP_PORT_MSG_ID_MASK;
3999 					if (!msg_protocol) {
4000 						ip_mq_unlock(remote_port);
4001 						goto filtered_msg;
4002 					}
4003 					msg_id = msg_id & MACH_BOOTSTRAP_PORT_MSG_ID_MASK;
4004 				}
4005 			} else {
4006 				assert(!ip_is_kolabeled(remote_port));
4007 				/* Connection ports can also have send-side message filters */
4008 				sblabel = remote_port->ip_splabel;
4009 			}
4010 			if (sblabel) {
4011 				mach_msg_filter_retain_sblabel_callback(sblabel);
4012 			}
4013 		}
4014 		ip_mq_unlock(remote_port);
4015 
4016 		if (sblabel && !mach_msg_fetch_filter_policy(sblabel, msg_id, &fid)) {
4017 			goto filtered_msg;
4018 		}
4019 	}
4020 
4021 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_SEND) | DBG_FUNC_NONE,
4022 	    VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
4023 	    (uintptr_t)kmsg->ikm_header->msgh_bits,
4024 	    (uintptr_t)kmsg->ikm_header->msgh_id,
4025 	    VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg))),
4026 	    0);
4027 
4028 	DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_copyin_from_user header:\n%.8x\n%.8x\n%p\n%p\n%p\n%.8x\n",
4029 	    kmsg->ikm_header->msgh_size,
4030 	    kmsg->ikm_header->msgh_bits,
4031 	    kmsg->ikm_header->msgh_remote_port,
4032 	    kmsg->ikm_header->msgh_local_port,
4033 	    ipc_kmsg_get_voucher_port(kmsg),
4034 	    kmsg->ikm_header->msgh_id);
4035 
4036 	if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
4037 		mr = ipc_kmsg_copyin_body( kmsg, space, map, optionp);
4038 
4039 		if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
4040 			__unreachable_ok_push /* unreachable if !DEBUG */
4041 			kprintf("body:\n");
4042 			for (uint32_t i = 0; i * 4 < (kmsg->ikm_header->msgh_size - sizeof(mach_msg_header_t)); i++) {
4043 				kprintf("%.4x\n", ((uint32_t *)(kmsg->ikm_header + 1))[i]);
4044 			}
4045 			__unreachable_ok_pop
4046 		}
4047 	}
4048 
4049 	/* Sign the message contents */
4050 	if (mr == MACH_MSG_SUCCESS) {
4051 		ipc_kmsg_init_trailer(kmsg, kmsg->ikm_header->msgh_size, current_task());
4052 		ikm_sign(kmsg);
4053 	}
4054 
4055 	return mr;
4056 
4057 filtered_msg:
4058 	if (!filter_nonfatal) {
4059 		mach_port_guard_exception(dest_name, 0, 0, kGUARD_EXC_MSG_FILTERED);
4060 	}
4061 	/* no descriptors have been copied in yet */
4062 	ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
4063 	return MACH_SEND_MSG_FILTERED;
4064 }
4065 
4066 /*
4067  *	Routine:	ipc_kmsg_copyin_from_kernel
4068  *	Purpose:
4069  *		"Copy-in" port rights and out-of-line memory
4070  *		in a message sent from the kernel.
4071  *
4072  *		Because the message comes from the kernel,
4073  *		the implementation assumes there are no errors
4074  *		or peculiarities in the message.
4075  *	Conditions:
4076  *		Nothing locked.
4077  */
4078 
4079 mach_msg_return_t
ipc_kmsg_copyin_from_kernel(ipc_kmsg_t kmsg)4080 ipc_kmsg_copyin_from_kernel(
4081 	ipc_kmsg_t      kmsg)
4082 {
4083 	mach_msg_bits_t bits = kmsg->ikm_header->msgh_bits;
4084 	mach_msg_type_name_t rname = MACH_MSGH_BITS_REMOTE(bits);
4085 	mach_msg_type_name_t lname = MACH_MSGH_BITS_LOCAL(bits);
4086 	mach_msg_type_name_t vname = MACH_MSGH_BITS_VOUCHER(bits);
4087 	ipc_object_t remote = ip_to_object(kmsg->ikm_header->msgh_remote_port);
4088 	ipc_object_t local = ip_to_object(kmsg->ikm_header->msgh_local_port);
4089 	ipc_object_t voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
4090 	ipc_port_t dest = kmsg->ikm_header->msgh_remote_port;
4091 
4092 	/* translate the destination and reply ports */
4093 	if (!IO_VALID(remote)) {
4094 		return MACH_SEND_INVALID_DEST;
4095 	}
4096 
4097 	ipc_object_copyin_from_kernel(remote, rname);
4098 	if (IO_VALID(local)) {
4099 		ipc_object_copyin_from_kernel(local, lname);
4100 	}
4101 
4102 	if (IO_VALID(voucher)) {
4103 		ipc_object_copyin_from_kernel(voucher, vname);
4104 	}
4105 
4106 	/*
4107 	 *	The common case is a complex message with no reply port,
4108 	 *	because that is what the memory_object interface uses.
4109 	 */
4110 
4111 	if (bits == (MACH_MSGH_BITS_COMPLEX |
4112 	    MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0))) {
4113 		bits = (MACH_MSGH_BITS_COMPLEX |
4114 		    MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0));
4115 
4116 		kmsg->ikm_header->msgh_bits = bits;
4117 	} else {
4118 		bits = (MACH_MSGH_BITS_OTHER(bits) |
4119 		    MACH_MSGH_BITS_SET_PORTS(ipc_object_copyin_type(rname),
4120 		    ipc_object_copyin_type(lname), ipc_object_copyin_type(vname)));
4121 
4122 		kmsg->ikm_header->msgh_bits = bits;
4123 	}
4124 
4125 	ipc_kmsg_set_qos_kernel(kmsg);
4126 
4127 	if (bits & MACH_MSGH_BITS_COMPLEX) {
4128 		/*
4129 		 * Check if the remote port accepts ports in the body.
4130 		 */
4131 		if (dest->ip_no_grant) {
4132 			mach_msg_descriptor_t   *saddr;
4133 			mach_msg_body_t         *body;
4134 			mach_msg_type_number_t  i, count;
4135 
4136 			body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
4137 			saddr = (mach_msg_descriptor_t *) (body + 1);
4138 			count = body->msgh_descriptor_count;
4139 
4140 			for (i = 0; i < count; i++, saddr++) {
4141 				switch (saddr->type.type) {
4142 				case MACH_MSG_PORT_DESCRIPTOR:
4143 				case MACH_MSG_OOL_PORTS_DESCRIPTOR:
4144 				case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
4145 					/* no descriptors have been copied in yet */
4146 					ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
4147 					return MACH_SEND_NO_GRANT_DEST;
4148 				}
4149 			}
4150 		}
4151 
4152 		mach_msg_descriptor_t   *saddr;
4153 		mach_msg_body_t         *body;
4154 		mach_msg_type_number_t  i, count;
4155 
4156 		body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
4157 		saddr = (mach_msg_descriptor_t *) (body + 1);
4158 		count = body->msgh_descriptor_count;
4159 
4160 		for (i = 0; i < count; i++, saddr++) {
4161 			switch (saddr->type.type) {
4162 			case MACH_MSG_PORT_DESCRIPTOR: {
4163 				mach_msg_type_name_t        name;
4164 				ipc_object_t                object;
4165 				mach_msg_port_descriptor_t  *dsc;
4166 
4167 				dsc = &saddr->port;
4168 
4169 				/* this is really the type SEND, SEND_ONCE, etc. */
4170 				name = dsc->disposition;
4171 				object = ip_to_object(dsc->name);
4172 				dsc->disposition = ipc_object_copyin_type(name);
4173 
4174 				if (!IO_VALID(object)) {
4175 					break;
4176 				}
4177 
4178 				ipc_object_copyin_from_kernel(object, name);
4179 
4180 				/* CDY avoid circularity when the destination is also */
4181 				/* the kernel.  This check should be changed into an  */
4182 				/* assert when the new kobject model is in place since*/
4183 				/* ports will not be used in kernel to kernel chats   */
4184 
4185 				/* do not lock remote port, use raw pointer comparison */
4186 				if (!ip_in_space_noauth(ip_object_to_port(remote), ipc_space_kernel)) {
4187 					/* remote port could be dead, in-transit or in an ipc space */
4188 					if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4189 					    ipc_port_check_circularity(ip_object_to_port(object),
4190 					    ip_object_to_port(remote))) {
4191 						kmsg->ikm_header->msgh_bits |=
4192 						    MACH_MSGH_BITS_CIRCULAR;
4193 					}
4194 				}
4195 				break;
4196 			}
4197 			case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
4198 			case MACH_MSG_OOL_DESCRIPTOR: {
4199 				/*
4200 				 * The sender should supply ready-made memory, i.e.
4201 				 * a vm_map_copy_t, so we don't need to do anything.
4202 				 */
4203 				break;
4204 			}
4205 			case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
4206 				ipc_object_t                        *objects;
4207 				unsigned int                        j;
4208 				mach_msg_type_name_t                name;
4209 				mach_msg_ool_ports_descriptor_t     *dsc;
4210 
4211 				dsc = (mach_msg_ool_ports_descriptor_t *)&saddr->ool_ports;
4212 
4213 				/* this is really the type SEND, SEND_ONCE, etc. */
4214 				name = dsc->disposition;
4215 				dsc->disposition = ipc_object_copyin_type(name);
4216 
4217 				objects = (ipc_object_t *) dsc->address;
4218 
4219 				for (j = 0; j < dsc->count; j++) {
4220 					ipc_object_t object = objects[j];
4221 
4222 					if (!IO_VALID(object)) {
4223 						continue;
4224 					}
4225 
4226 					ipc_object_copyin_from_kernel(object, name);
4227 
4228 					if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4229 					    ipc_port_check_circularity(ip_object_to_port(object),
4230 					    ip_object_to_port(remote))) {
4231 						kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4232 					}
4233 				}
4234 				break;
4235 			}
4236 			case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
4237 				mach_msg_guarded_port_descriptor_t *dsc = (typeof(dsc)) & saddr->guarded_port;
4238 				mach_msg_type_name_t disp = dsc->disposition;
4239 				ipc_object_t object = ip_to_object(dsc->name);
4240 				dsc->disposition = ipc_object_copyin_type(disp);
4241 				assert(dsc->flags == 0);
4242 
4243 				if (!IO_VALID(object)) {
4244 					break;
4245 				}
4246 
4247 				ipc_object_copyin_from_kernel(object, disp);
4248 				/*
4249 				 * avoid circularity when the destination is also
4250 				 * the kernel.  This check should be changed into an
4251 				 * assert when the new kobject model is in place since
4252 				 * ports will not be used in kernel to kernel chats
4253 				 */
4254 
4255 				/* do not lock remote port, use raw pointer comparison */
4256 				if (!ip_in_space_noauth(ip_object_to_port(remote), ipc_space_kernel)) {
4257 					/* remote port could be dead, in-transit or in an ipc space */
4258 					if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4259 					    ipc_port_check_circularity(ip_object_to_port(object),
4260 					    ip_object_to_port(remote))) {
4261 						kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4262 					}
4263 				}
4264 				break;
4265 			}
4266 			default: {
4267 #if     MACH_ASSERT
4268 				panic("ipc_kmsg_copyin_from_kernel:  bad descriptor");
4269 #endif  /* MACH_ASSERT */
4270 			}
4271 			}
4272 		}
4273 	}
4274 
4275 	/* Add trailer and signature to the message */
4276 	ipc_kmsg_init_trailer(kmsg, kmsg->ikm_header->msgh_size, TASK_NULL);
4277 	ikm_sign(kmsg);
4278 
4279 	return MACH_MSG_SUCCESS;
4280 }
4281 
4282 /*
4283  *	Routine:	ipc_kmsg_copyout_header
4284  *	Purpose:
4285  *		"Copy-out" port rights in the header of a message.
4286  *		Operates atomically; if it doesn't succeed the
4287  *		message header and the space are left untouched.
4288  *		If it does succeed the remote/local port fields
4289  *		contain port names instead of object pointers,
4290  *		and the bits field is updated.
4291  *	Conditions:
4292  *		Nothing locked.
4293  *	Returns:
4294  *		MACH_MSG_SUCCESS	Copied out port rights.
4295  *		MACH_RCV_INVALID_NOTIFY
4296  *			Notify is non-null and doesn't name a receive right.
4297  *			(Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
4298  *		MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
4299  *			The space is dead.
4300  *		MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
4301  *			No room in space for another name.
4302  *		MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
4303  *			Couldn't allocate memory for the reply port.
4304  *		MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
4305  *			Couldn't allocate memory for the dead-name request.
4306  */
4307 
4308 static mach_msg_return_t
ipc_kmsg_copyout_header(ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_option_t option)4309 ipc_kmsg_copyout_header(
4310 	ipc_kmsg_t              kmsg,
4311 	ipc_space_t             space,
4312 	mach_msg_option_t       option)
4313 {
4314 	mach_msg_header_t *msg = kmsg->ikm_header;
4315 	mach_msg_bits_t mbits = msg->msgh_bits;
4316 	ipc_port_t dest = msg->msgh_remote_port;
4317 
4318 	assert(IP_VALID(dest));
4319 
4320 	/*
4321 	 * While we still hold a reference on the received-from port,
4322 	 * process all send-possible notfications we received along with
4323 	 * the message.
4324 	 */
4325 	ipc_port_spnotify(dest);
4326 
4327 	{
4328 		mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
4329 		mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
4330 		mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
4331 		ipc_port_t reply = msg->msgh_local_port;
4332 		ipc_port_t release_reply_port = IP_NULL;
4333 		mach_port_name_t dest_name, reply_name;
4334 
4335 		ipc_port_t voucher = ipc_kmsg_get_voucher_port(kmsg);
4336 		uintptr_t voucher_addr = 0;
4337 		ipc_port_t release_voucher_port = IP_NULL;
4338 		mach_port_name_t voucher_name;
4339 
4340 		uint32_t entries_held = 0;
4341 		boolean_t need_write_lock = FALSE;
4342 		ipc_object_copyout_flags_t reply_copyout_options = IPC_OBJECT_COPYOUT_FLAGS_NONE;
4343 		kern_return_t kr;
4344 
4345 		/*
4346 		 * Reserve any potentially needed entries in the target space.
4347 		 * We'll free any unused before unlocking the space.
4348 		 */
4349 		if (IP_VALID(reply)) {
4350 			entries_held++;
4351 			need_write_lock = TRUE;
4352 		}
4353 		if (IP_VALID(voucher)) {
4354 			assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
4355 
4356 			if ((option & MACH_RCV_VOUCHER) != 0) {
4357 				entries_held++;
4358 			}
4359 			need_write_lock = TRUE;
4360 			voucher_addr = unsafe_convert_port_to_voucher(voucher);
4361 		}
4362 
4363 		if (need_write_lock) {
4364 handle_reply_again:
4365 			is_write_lock(space);
4366 
4367 			while (entries_held) {
4368 				if (!is_active(space)) {
4369 					is_write_unlock(space);
4370 					return MACH_RCV_HEADER_ERROR |
4371 					       MACH_MSG_IPC_SPACE;
4372 				}
4373 
4374 				kr = ipc_entries_hold(space, entries_held);
4375 				if (KERN_SUCCESS == kr) {
4376 					break;
4377 				}
4378 
4379 				kr = ipc_entry_grow_table(space, ITS_SIZE_NONE);
4380 				if (KERN_SUCCESS != kr) {
4381 					return MACH_RCV_HEADER_ERROR |
4382 					       MACH_MSG_IPC_SPACE;
4383 				}
4384 				/* space was unlocked and relocked - retry */
4385 			}
4386 
4387 			/* Handle reply port. */
4388 			if (IP_VALID(reply)) {
4389 				ipc_port_t reply_subst = IP_NULL;
4390 				ipc_entry_t entry;
4391 
4392 				ip_mq_lock(reply);
4393 
4394 				/* Is the reply port still active and allowed to be copied out? */
4395 				if (!ip_active(reply) ||
4396 				    !ip_label_check(space, reply, reply_type,
4397 				    &reply_copyout_options, &reply_subst)) {
4398 					/* clear the context value */
4399 					reply->ip_reply_context = 0;
4400 					ip_mq_unlock(reply);
4401 
4402 					assert(reply_subst == IP_NULL);
4403 					release_reply_port = reply;
4404 					reply = IP_DEAD;
4405 					reply_name = MACH_PORT_DEAD;
4406 					goto done_with_reply;
4407 				}
4408 
4409 				/* is the kolabel requesting a substitution */
4410 				if (reply_subst != IP_NULL) {
4411 					/*
4412 					 * port is unlocked, its right consumed
4413 					 * space is unlocked
4414 					 */
4415 					assert(reply_type == MACH_MSG_TYPE_PORT_SEND);
4416 					msg->msgh_local_port = reply = reply_subst;
4417 					goto handle_reply_again;
4418 				}
4419 
4420 
4421 				/* Is there already an entry we can use? */
4422 				if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
4423 				    ipc_right_reverse(space, ip_to_object(reply), &reply_name, &entry)) {
4424 					assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
4425 				} else {
4426 					/* claim a held entry for the reply port */
4427 					assert(entries_held > 0);
4428 					entries_held--;
4429 					ipc_entry_claim(space, ip_to_object(reply),
4430 					    &reply_name, &entry);
4431 				}
4432 
4433 				/* space and reply port are locked and active */
4434 				ip_reference(reply);         /* hold onto the reply port */
4435 
4436 				/*
4437 				 * If the receiver would like to enforce strict reply
4438 				 * semantics, and the message looks like it expects a reply,
4439 				 * and contains a voucher, then link the context in the
4440 				 * voucher with the reply port so that the next message sent
4441 				 * to the reply port must come from a thread that has a
4442 				 * matching context (voucher).
4443 				 */
4444 				if (enforce_strict_reply && MACH_RCV_WITH_STRICT_REPLY(option) && IP_VALID(voucher)) {
4445 					if (ipc_kmsg_validate_reply_port_locked(reply, option) != KERN_SUCCESS) {
4446 						/* if the receiver isn't happy with the reply port: fail the receive. */
4447 						assert(!ip_is_pinned(reply));
4448 						ipc_entry_dealloc(space, ip_to_object(reply),
4449 						    reply_name, entry);
4450 						ip_mq_unlock(reply);
4451 						is_write_unlock(space);
4452 						ip_release(reply);
4453 						return MACH_RCV_INVALID_REPLY;
4454 					}
4455 					ipc_kmsg_link_reply_context_locked(reply, voucher);
4456 				} else {
4457 					/*
4458 					 * if the receive did not choose to participate
4459 					 * in the strict reply/RPC, then don't enforce
4460 					 * anything (as this could lead to booby-trapped
4461 					 * messages that kill the server).
4462 					 */
4463 					reply->ip_reply_context = 0;
4464 				}
4465 
4466 				kr = ipc_right_copyout(space, reply_name, entry,
4467 				    reply_type, IPC_OBJECT_COPYOUT_FLAGS_NONE, NULL, NULL,
4468 				    ip_to_object(reply));
4469 				assert(kr == KERN_SUCCESS);
4470 				/* reply port is unlocked */
4471 			} else {
4472 				reply_name = CAST_MACH_PORT_TO_NAME(reply);
4473 			}
4474 
4475 done_with_reply:
4476 
4477 			/* Handle voucher port. */
4478 			if (voucher_type != MACH_MSGH_BITS_ZERO) {
4479 				assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
4480 
4481 				if (!IP_VALID(voucher)) {
4482 					if ((option & MACH_RCV_VOUCHER) == 0) {
4483 						voucher_type = MACH_MSGH_BITS_ZERO;
4484 					}
4485 					voucher_name = MACH_PORT_NULL;
4486 					goto done_with_voucher;
4487 				}
4488 
4489 #if CONFIG_PREADOPT_TG
4490 				struct knote *kn = current_thread()->ith_knote;
4491 				if (kn == ITH_KNOTE_NULL || kn == ITH_KNOTE_PSEUDO) {
4492 					/*
4493 					 * We are not in this path of voucher copyout because of
4494 					 * kevent - we cannot expect a voucher preadopt happening on
4495 					 * this thread for this message later on
4496 					 */
4497 					KDBG_DEBUG(MACHDBG_CODE(DBG_MACH_THREAD_GROUP, MACH_THREAD_GROUP_PREADOPT_NA),
4498 					    thread_tid(current_thread()), 0, 0, 0);
4499 				}
4500 #endif
4501 
4502 				/* clear voucher from its hiding place back in the kmsg */
4503 				ipc_kmsg_clear_voucher_port(kmsg);
4504 
4505 				if ((option & MACH_RCV_VOUCHER) != 0) {
4506 					ipc_entry_t entry;
4507 
4508 					ip_mq_lock(voucher);
4509 
4510 					if (ipc_right_reverse(space, ip_to_object(voucher),
4511 					    &voucher_name, &entry)) {
4512 						assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
4513 					} else {
4514 						assert(entries_held > 0);
4515 						entries_held--;
4516 						ipc_entry_claim(space, ip_to_object(voucher), &voucher_name, &entry);
4517 					}
4518 					/* space is locked and active */
4519 
4520 					assert(ip_kotype(voucher) == IKOT_VOUCHER);
4521 					kr = ipc_right_copyout(space, voucher_name, entry,
4522 					    MACH_MSG_TYPE_MOVE_SEND, IPC_OBJECT_COPYOUT_FLAGS_NONE,
4523 					    NULL, NULL, ip_to_object(voucher));
4524 					/* voucher port is unlocked */
4525 				} else {
4526 					voucher_type = MACH_MSGH_BITS_ZERO;
4527 					release_voucher_port = voucher;
4528 					voucher_name = MACH_PORT_NULL;
4529 				}
4530 			} else {
4531 				voucher_name = msg->msgh_voucher_port;
4532 			}
4533 
4534 done_with_voucher:
4535 
4536 			ip_mq_lock(dest);
4537 			is_write_unlock(space);
4538 		} else {
4539 			/*
4540 			 *	No reply or voucher port!  This is an easy case.
4541 			 *
4542 			 *	We only need to check that the space is still
4543 			 *	active once we locked the destination:
4544 			 *
4545 			 *	- if the space holds a receive right for `dest`,
4546 			 *	  then holding the port lock means we can't fail
4547 			 *	  to notice if the space went dead because
4548 			 *	  the is_write_unlock() will pair with
4549 			 *	  os_atomic_barrier_before_lock_acquire() + ip_mq_lock().
4550 			 *
4551 			 *	- if this space doesn't hold a receive right
4552 			 *	  for `dest`, then `dest->ip_receiver` points
4553 			 *	  elsewhere, and ipc_object_copyout_dest() will
4554 			 *	  handle this situation, and failing to notice
4555 			 *	  that the space was dead is accetable.
4556 			 */
4557 
4558 			os_atomic_barrier_before_lock_acquire();
4559 			ip_mq_lock(dest);
4560 			if (!is_active(space)) {
4561 				ip_mq_unlock(dest);
4562 				return MACH_RCV_HEADER_ERROR | MACH_MSG_IPC_SPACE;
4563 			}
4564 
4565 			reply_name = CAST_MACH_PORT_TO_NAME(reply);
4566 
4567 			if (voucher_type != MACH_MSGH_BITS_ZERO) {
4568 				assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
4569 				if ((option & MACH_RCV_VOUCHER) == 0) {
4570 					voucher_type = MACH_MSGH_BITS_ZERO;
4571 				}
4572 				voucher_name = MACH_PORT_NULL;
4573 			} else {
4574 				voucher_name = msg->msgh_voucher_port;
4575 			}
4576 		}
4577 
4578 		/*
4579 		 *	At this point, the space is unlocked and the destination
4580 		 *	port is locked.
4581 		 *	reply_name is taken care of; we still need dest_name.
4582 		 *	We still hold a ref for reply (if it is valid).
4583 		 *
4584 		 *	If the space holds receive rights for the destination,
4585 		 *	we return its name for the right.  Otherwise the task
4586 		 *	managed to destroy or give away the receive right between
4587 		 *	receiving the message and this copyout.  If the destination
4588 		 *	is dead, return MACH_PORT_DEAD, and if the receive right
4589 		 *	exists somewhere else (another space, in transit)
4590 		 *	return MACH_PORT_NULL.
4591 		 *
4592 		 *	Making this copyout operation atomic with the previous
4593 		 *	copyout of the reply port is a bit tricky.  If there was
4594 		 *	no real reply port (it wasn't IP_VALID) then this isn't
4595 		 *	an issue.  If the reply port was dead at copyout time,
4596 		 *	then we are OK, because if dest is dead we serialize
4597 		 *	after the death of both ports and if dest is alive
4598 		 *	we serialize after reply died but before dest's (later) death.
4599 		 *	So assume reply was alive when we copied it out.  If dest
4600 		 *	is alive, then we are OK because we serialize before
4601 		 *	the ports' deaths.  So assume dest is dead when we look at it.
4602 		 *	If reply dies/died after dest, then we are OK because
4603 		 *	we serialize after dest died but before reply dies.
4604 		 *	So the hard case is when reply is alive at copyout,
4605 		 *	dest is dead at copyout, and reply died before dest died.
4606 		 *	In this case pretend that dest is still alive, so
4607 		 *	we serialize while both ports are alive.
4608 		 *
4609 		 *	Because the space lock is held across the copyout of reply
4610 		 *	and locking dest, the receive right for dest can't move
4611 		 *	in or out of the space while the copyouts happen, so
4612 		 *	that isn't an atomicity problem.  In the last hard case
4613 		 *	above, this implies that when dest is dead that the
4614 		 *	space couldn't have had receive rights for dest at
4615 		 *	the time reply was copied-out, so when we pretend
4616 		 *	that dest is still alive, we can return MACH_PORT_NULL.
4617 		 *
4618 		 *	If dest == reply, then we have to make it look like
4619 		 *	either both copyouts happened before the port died,
4620 		 *	or both happened after the port died.  This special
4621 		 *	case works naturally if the timestamp comparison
4622 		 *	is done correctly.
4623 		 */
4624 
4625 		if (ip_active(dest)) {
4626 			ipc_object_copyout_dest(space, ip_to_object(dest),
4627 			    dest_type, &dest_name);
4628 			/* dest is unlocked */
4629 		} else {
4630 			ipc_port_timestamp_t timestamp;
4631 
4632 			timestamp = ip_get_death_time(dest);
4633 			ip_mq_unlock(dest);
4634 			ip_release(dest);
4635 
4636 			if (IP_VALID(reply)) {
4637 				ip_mq_lock(reply);
4638 				if (ip_active(reply) ||
4639 				    IP_TIMESTAMP_ORDER(timestamp,
4640 				    ip_get_death_time(reply))) {
4641 					dest_name = MACH_PORT_DEAD;
4642 				} else {
4643 					dest_name = MACH_PORT_NULL;
4644 				}
4645 				ip_mq_unlock(reply);
4646 			} else {
4647 				dest_name = MACH_PORT_DEAD;
4648 			}
4649 		}
4650 
4651 		if (IP_VALID(reply)) {
4652 			ip_release(reply);
4653 		}
4654 
4655 		if (IP_VALID(release_reply_port)) {
4656 			if (reply_type == MACH_MSG_TYPE_PORT_SEND_ONCE) {
4657 				ipc_port_release_sonce(release_reply_port);
4658 			} else {
4659 				ipc_port_release_send(release_reply_port);
4660 			}
4661 		}
4662 
4663 		if ((option & MACH_RCV_VOUCHER) != 0) {
4664 			KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV) | DBG_FUNC_NONE,
4665 			    VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
4666 			    (uintptr_t)kmsg->ikm_header->msgh_bits,
4667 			    (uintptr_t)kmsg->ikm_header->msgh_id,
4668 			    VM_KERNEL_ADDRPERM(voucher_addr), 0);
4669 		} else {
4670 			KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV_VOUCHER_REFUSED) | DBG_FUNC_NONE,
4671 			    VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
4672 			    (uintptr_t)kmsg->ikm_header->msgh_bits,
4673 			    (uintptr_t)kmsg->ikm_header->msgh_id,
4674 			    VM_KERNEL_ADDRPERM(voucher_addr), 0);
4675 		}
4676 
4677 		if (IP_VALID(release_voucher_port)) {
4678 			ipc_port_release_send(release_voucher_port);
4679 		}
4680 
4681 		msg->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
4682 		    voucher_type, mbits);
4683 		msg->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
4684 		msg->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
4685 		msg->msgh_voucher_port = voucher_name;
4686 	}
4687 
4688 	return MACH_MSG_SUCCESS;
4689 }
4690 
4691 /*
4692  *	Routine:	ipc_kmsg_copyout_object
4693  *	Purpose:
4694  *		Copy-out a port right.  Always returns a name,
4695  *		even for unsuccessful return codes.  Always
4696  *		consumes the supplied object.
4697  *	Conditions:
4698  *		Nothing locked.
4699  *	Returns:
4700  *		MACH_MSG_SUCCESS	The space acquired the right
4701  *			(name is valid) or the object is dead (MACH_PORT_DEAD).
4702  *		MACH_MSG_IPC_SPACE	No room in space for the right,
4703  *			or the space is dead.  (Name is MACH_PORT_NULL.)
4704  *		MACH_MSG_IPC_KERNEL	Kernel resource shortage.
4705  *			(Name is MACH_PORT_NULL.)
4706  */
4707 static mach_msg_return_t
ipc_kmsg_copyout_object(ipc_space_t space,ipc_object_t object,mach_msg_type_name_t msgt_name,mach_port_context_t * context,mach_msg_guard_flags_t * guard_flags,mach_port_name_t * namep)4708 ipc_kmsg_copyout_object(
4709 	ipc_space_t             space,
4710 	ipc_object_t            object,
4711 	mach_msg_type_name_t    msgt_name,
4712 	mach_port_context_t     *context,
4713 	mach_msg_guard_flags_t  *guard_flags,
4714 	mach_port_name_t        *namep)
4715 {
4716 	kern_return_t kr;
4717 
4718 	if (!IO_VALID(object)) {
4719 		*namep = CAST_MACH_PORT_TO_NAME(object);
4720 		return MACH_MSG_SUCCESS;
4721 	}
4722 
4723 	kr = ipc_object_copyout(space, object, msgt_name, IPC_OBJECT_COPYOUT_FLAGS_NONE,
4724 	    context, guard_flags, namep);
4725 	if (kr != KERN_SUCCESS) {
4726 		if (kr == KERN_INVALID_CAPABILITY) {
4727 			*namep = MACH_PORT_DEAD;
4728 		} else {
4729 			*namep = MACH_PORT_NULL;
4730 
4731 			if (kr == KERN_RESOURCE_SHORTAGE) {
4732 				return MACH_MSG_IPC_KERNEL;
4733 			} else {
4734 				return MACH_MSG_IPC_SPACE;
4735 			}
4736 		}
4737 	}
4738 
4739 	return MACH_MSG_SUCCESS;
4740 }
4741 
4742 static mach_msg_descriptor_t *
ipc_kmsg_copyout_port_descriptor(mach_msg_descriptor_t * dsc,mach_msg_descriptor_t * dest_dsc,ipc_space_t space,kern_return_t * mr)4743 ipc_kmsg_copyout_port_descriptor(
4744 	mach_msg_descriptor_t   *dsc,
4745 	mach_msg_descriptor_t   *dest_dsc,
4746 	ipc_space_t             space,
4747 	kern_return_t           *mr)
4748 {
4749 	mach_msg_user_port_descriptor_t *user_dsc;
4750 	mach_port_t             port;
4751 	mach_port_name_t        name;
4752 	mach_msg_type_name_t    disp;
4753 
4754 	/* Copyout port right carried in the message */
4755 	port = dsc->port.name;
4756 	disp = dsc->port.disposition;
4757 	*mr |= ipc_kmsg_copyout_object(space,
4758 	    ip_to_object(port), disp, NULL, NULL, &name);
4759 
4760 	// point to the start of this port descriptor
4761 	user_dsc = ((mach_msg_user_port_descriptor_t *)dest_dsc - 1);
4762 	bzero((void *)user_dsc, sizeof(*user_dsc));
4763 	user_dsc->name = CAST_MACH_PORT_TO_NAME(name);
4764 	user_dsc->disposition = disp;
4765 	user_dsc->type = MACH_MSG_PORT_DESCRIPTOR;
4766 
4767 	return (mach_msg_descriptor_t *)user_dsc;
4768 }
4769 
4770 static mach_msg_descriptor_t *
ipc_kmsg_copyout_ool_descriptor(mach_msg_ool_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,vm_map_t map,mach_msg_return_t * mr)4771 ipc_kmsg_copyout_ool_descriptor(
4772 	mach_msg_ool_descriptor_t   *dsc,
4773 	mach_msg_descriptor_t       *user_dsc,
4774 	int                         is_64bit,
4775 	vm_map_t                    map,
4776 	mach_msg_return_t           *mr)
4777 {
4778 	vm_map_copy_t               copy;
4779 	vm_map_address_t            rcv_addr;
4780 	mach_msg_copy_options_t     copy_options;
4781 	vm_map_size_t               size;
4782 	mach_msg_descriptor_type_t  dsc_type;
4783 	boolean_t                   misaligned = FALSE;
4784 
4785 	copy = (vm_map_copy_t)dsc->address;
4786 	size = (vm_map_size_t)dsc->size;
4787 	copy_options = dsc->copy;
4788 	assert(copy_options != MACH_MSG_KALLOC_COPY_T);
4789 	dsc_type = dsc->type;
4790 
4791 	if (copy != VM_MAP_COPY_NULL) {
4792 		kern_return_t kr;
4793 
4794 		rcv_addr = 0;
4795 		if (vm_map_copy_validate_size(map, copy, &size) == FALSE) {
4796 			panic("Inconsistent OOL/copyout size on %p: expected %d, got %lld @%p",
4797 			    dsc, dsc->size, (unsigned long long)copy->size, copy);
4798 		}
4799 
4800 		if ((copy->type == VM_MAP_COPY_ENTRY_LIST) &&
4801 		    (trunc_page(copy->offset) != copy->offset ||
4802 		    round_page(dsc->size) != dsc->size)) {
4803 			misaligned = TRUE;
4804 		}
4805 
4806 		if (misaligned) {
4807 			mach_vm_offset_t rounded_addr;
4808 			vm_map_size_t   rounded_size;
4809 			vm_map_offset_t effective_page_mask, effective_page_size;
4810 
4811 			effective_page_mask = VM_MAP_PAGE_MASK(map);
4812 			effective_page_size = effective_page_mask + 1;
4813 
4814 			rounded_size = vm_map_round_page(copy->offset + size, effective_page_mask) - vm_map_trunc_page(copy->offset, effective_page_mask);
4815 
4816 			kr = mach_vm_allocate_kernel(map, &rounded_addr,
4817 			    rounded_size, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IPC);
4818 
4819 			if (kr == KERN_SUCCESS) {
4820 				/*
4821 				 * vm_map_copy_overwrite does a full copy
4822 				 * if size is too small to optimize.
4823 				 * So we tried skipping the offset adjustment
4824 				 * if we fail the 'size' test.
4825 				 *
4826 				 * if (size >= VM_MAP_COPY_OVERWRITE_OPTIMIZATION_THRESHOLD_PAGES * effective_page_size) {
4827 				 *
4828 				 * This resulted in leaked memory especially on the
4829 				 * older watches (16k user - 4k kernel) because we
4830 				 * would do a physical copy into the start of this
4831 				 * rounded range but could leak part of it
4832 				 * on deallocation if the 'size' being deallocated
4833 				 * does not cover the full range. So instead we do
4834 				 * the misalignment adjustment always so that on
4835 				 * deallocation we will remove the full range.
4836 				 */
4837 				if ((rounded_addr & effective_page_mask) !=
4838 				    (copy->offset & effective_page_mask)) {
4839 					/*
4840 					 * Need similar mis-alignment of source and destination...
4841 					 */
4842 					rounded_addr += (copy->offset & effective_page_mask);
4843 
4844 					assert((rounded_addr & effective_page_mask) == (copy->offset & effective_page_mask));
4845 				}
4846 				rcv_addr = rounded_addr;
4847 
4848 				kr = vm_map_copy_overwrite(map, rcv_addr, copy, size, FALSE);
4849 			}
4850 		} else {
4851 			kr = vm_map_copyout_size(map, &rcv_addr, copy, size);
4852 		}
4853 		if (kr != KERN_SUCCESS) {
4854 			if (kr == KERN_RESOURCE_SHORTAGE) {
4855 				*mr |= MACH_MSG_VM_KERNEL;
4856 			} else {
4857 				*mr |= MACH_MSG_VM_SPACE;
4858 			}
4859 			vm_map_copy_discard(copy);
4860 			rcv_addr = 0;
4861 			size = 0;
4862 		}
4863 	} else {
4864 		rcv_addr = 0;
4865 		size = 0;
4866 	}
4867 
4868 	/*
4869 	 * Now update the descriptor as the user would see it.
4870 	 * This may require expanding the descriptor to the user
4871 	 * visible size.  There is already space allocated for
4872 	 * this in what naddr points to.
4873 	 */
4874 	if (is_64bit) {
4875 		mach_msg_ool_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4876 		user_ool_dsc--;
4877 		bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4878 
4879 		user_ool_dsc->address = rcv_addr;
4880 		user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4881 		    TRUE : FALSE;
4882 		user_ool_dsc->copy = copy_options;
4883 		user_ool_dsc->type = dsc_type;
4884 		user_ool_dsc->size = (mach_msg_size_t)size;
4885 
4886 		user_dsc = (typeof(user_dsc))user_ool_dsc;
4887 	} else {
4888 		mach_msg_ool_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4889 		user_ool_dsc--;
4890 		bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4891 
4892 		user_ool_dsc->address = CAST_DOWN_EXPLICIT(uint32_t, rcv_addr);
4893 		user_ool_dsc->size = (mach_msg_size_t)size;
4894 		user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4895 		    TRUE : FALSE;
4896 		user_ool_dsc->copy = copy_options;
4897 		user_ool_dsc->type = dsc_type;
4898 
4899 		user_dsc = (typeof(user_dsc))user_ool_dsc;
4900 	}
4901 	return user_dsc;
4902 }
4903 
4904 static mach_msg_descriptor_t *
ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,vm_map_t map,ipc_space_t space,ipc_kmsg_t kmsg,mach_msg_return_t * mr)4905 ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t *dsc,
4906     mach_msg_descriptor_t *user_dsc,
4907     int is_64bit,
4908     vm_map_t map,
4909     ipc_space_t space,
4910     ipc_kmsg_t kmsg,
4911     mach_msg_return_t *mr)
4912 {
4913 	mach_vm_offset_t        rcv_addr = 0;
4914 	mach_msg_type_name_t    disp;
4915 	mach_msg_type_number_t  count, i;
4916 	vm_size_t               ports_length, names_length;
4917 	mach_msg_copy_options_t copy_options = MACH_MSG_VIRTUAL_COPY;
4918 
4919 	count = dsc->count;
4920 	disp = dsc->disposition;
4921 	ports_length = count * sizeof(mach_port_t);
4922 	names_length = count * sizeof(mach_port_name_t);
4923 
4924 	if (ports_length != 0 && dsc->address != 0) {
4925 		if (copy_options == MACH_MSG_VIRTUAL_COPY) {
4926 			/*
4927 			 * Dynamically allocate the region
4928 			 */
4929 			vm_tag_t tag;
4930 			if (vm_kernel_map_is_kernel(map)) {
4931 				tag = VM_KERN_MEMORY_IPC;
4932 			} else {
4933 				tag = VM_MEMORY_MACH_MSG;
4934 			}
4935 
4936 			kern_return_t kr;
4937 			if ((kr = mach_vm_allocate_kernel(map, &rcv_addr,
4938 			    (mach_vm_size_t)names_length,
4939 			    VM_FLAGS_ANYWHERE, tag)) != KERN_SUCCESS) {
4940 				ipc_kmsg_clean_body(kmsg, 1, (mach_msg_descriptor_t *)dsc);
4941 				rcv_addr = 0;
4942 
4943 				if (kr == KERN_RESOURCE_SHORTAGE) {
4944 					*mr |= MACH_MSG_VM_KERNEL;
4945 				} else {
4946 					*mr |= MACH_MSG_VM_SPACE;
4947 				}
4948 			}
4949 		}
4950 
4951 		/*
4952 		 * Handle the port rights and copy out the names
4953 		 * for those rights out to user-space.
4954 		 */
4955 		if (rcv_addr != 0) {
4956 			ipc_object_t *objects = (ipc_object_t *) dsc->address;
4957 			mach_port_name_t *names = (mach_port_name_t *) dsc->address;
4958 
4959 			/* copyout port rights carried in the message */
4960 
4961 			for (i = 0; i < count; i++) {
4962 				ipc_object_t object = objects[i];
4963 
4964 				*mr |= ipc_kmsg_copyout_object(space, object,
4965 				    disp, NULL, NULL, &names[i]);
4966 			}
4967 
4968 			/* copyout to memory allocated above */
4969 			void *data = dsc->address;
4970 			if (copyoutmap(map, data, rcv_addr, names_length) != KERN_SUCCESS) {
4971 				*mr |= MACH_MSG_VM_SPACE;
4972 			}
4973 			kfree_type(mach_port_t, count, data);
4974 		}
4975 	} else {
4976 		rcv_addr = 0;
4977 	}
4978 
4979 	/*
4980 	 * Now update the descriptor based on the information
4981 	 * calculated above.
4982 	 */
4983 	if (is_64bit) {
4984 		mach_msg_ool_ports_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4985 		user_ool_dsc--;
4986 		bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4987 
4988 		user_ool_dsc->address = rcv_addr;
4989 		user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4990 		    TRUE : FALSE;
4991 		user_ool_dsc->copy = copy_options;
4992 		user_ool_dsc->disposition = disp;
4993 		user_ool_dsc->type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
4994 		user_ool_dsc->count = count;
4995 
4996 		user_dsc = (typeof(user_dsc))user_ool_dsc;
4997 	} else {
4998 		mach_msg_ool_ports_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4999 		user_ool_dsc--;
5000 		bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
5001 
5002 		user_ool_dsc->address = CAST_DOWN_EXPLICIT(uint32_t, rcv_addr);
5003 		user_ool_dsc->count = count;
5004 		user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
5005 		    TRUE : FALSE;
5006 		user_ool_dsc->copy = copy_options;
5007 		user_ool_dsc->disposition = disp;
5008 		user_ool_dsc->type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
5009 
5010 		user_dsc = (typeof(user_dsc))user_ool_dsc;
5011 	}
5012 	return user_dsc;
5013 }
5014 
5015 static mach_msg_descriptor_t *
ipc_kmsg_copyout_guarded_port_descriptor(mach_msg_guarded_port_descriptor_t * dsc,mach_msg_descriptor_t * dest_dsc,int is_64bit,__unused ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_option_t option,kern_return_t * mr)5016 ipc_kmsg_copyout_guarded_port_descriptor(
5017 	mach_msg_guarded_port_descriptor_t *dsc,
5018 	mach_msg_descriptor_t *dest_dsc,
5019 	int is_64bit,
5020 	__unused ipc_kmsg_t  kmsg,
5021 	ipc_space_t space,
5022 	mach_msg_option_t option,
5023 	kern_return_t *mr)
5024 {
5025 	mach_port_t                 port;
5026 	mach_port_name_t            name = MACH_PORT_NULL;
5027 	mach_msg_type_name_t        disp;
5028 	mach_msg_guard_flags_t      guard_flags;
5029 	mach_port_context_t         context;
5030 
5031 	/* Copyout port right carried in the message */
5032 	port = dsc->name;
5033 	disp = dsc->disposition;
5034 	guard_flags = dsc->flags;
5035 	context = 0;
5036 
5037 	/* Currently kernel_task doesnt support receiving guarded port descriptors */
5038 	struct knote *kn = current_thread()->ith_knote;
5039 	if ((kn != ITH_KNOTE_PSEUDO) && ((option & MACH_RCV_GUARDED_DESC) == 0)) {
5040 #if DEVELOPMENT || DEBUG
5041 		/*
5042 		 * Simulated crash needed for debugging, notifies the receiver to opt into receiving
5043 		 * guarded descriptors.
5044 		 */
5045 		mach_port_guard_exception(current_thread()->ith_receiver_name,
5046 		    0, 0, kGUARD_EXC_RCV_GUARDED_DESC);
5047 #endif
5048 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_DESTROY_GUARDED_DESC), current_thread()->ith_receiver_name,
5049 		    VM_KERNEL_ADDRPERM(port), disp, guard_flags);
5050 		ipc_object_destroy(ip_to_object(port), disp);
5051 		mach_msg_user_port_descriptor_t *user_dsc = (typeof(user_dsc))dest_dsc;
5052 		user_dsc--;         // point to the start of this port descriptor
5053 		bzero((void *)user_dsc, sizeof(*user_dsc));
5054 		user_dsc->name = name;
5055 		user_dsc->disposition = disp;
5056 		user_dsc->type = MACH_MSG_PORT_DESCRIPTOR;
5057 		dest_dsc = (typeof(dest_dsc))user_dsc;
5058 	} else {
5059 		*mr |= ipc_kmsg_copyout_object(space,
5060 		    ip_to_object(port), disp, &context, &guard_flags, &name);
5061 
5062 		if (!is_64bit) {
5063 			mach_msg_guarded_port_descriptor32_t *user_dsc = (typeof(user_dsc))dest_dsc;
5064 			user_dsc--;         // point to the start of this port descriptor
5065 			bzero((void *)user_dsc, sizeof(*user_dsc));
5066 			user_dsc->name = name;
5067 			user_dsc->flags = guard_flags;
5068 			user_dsc->disposition = disp;
5069 			user_dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
5070 			user_dsc->context = CAST_DOWN_EXPLICIT(uint32_t, context);
5071 			dest_dsc = (typeof(dest_dsc))user_dsc;
5072 		} else {
5073 			mach_msg_guarded_port_descriptor64_t *user_dsc = (typeof(user_dsc))dest_dsc;
5074 			user_dsc--;         // point to the start of this port descriptor
5075 			bzero((void *)user_dsc, sizeof(*user_dsc));
5076 			user_dsc->name = name;
5077 			user_dsc->flags = guard_flags;
5078 			user_dsc->disposition = disp;
5079 			user_dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
5080 			user_dsc->context = context;
5081 			dest_dsc = (typeof(dest_dsc))user_dsc;
5082 		}
5083 	}
5084 
5085 	return (mach_msg_descriptor_t *)dest_dsc;
5086 }
5087 
5088 
5089 /*
5090  *	Routine:	ipc_kmsg_copyout_body
5091  *	Purpose:
5092  *		"Copy-out" port rights and out-of-line memory
5093  *		in the body of a message.
5094  *
5095  *		The error codes are a combination of special bits.
5096  *		The copyout proceeds despite errors.
5097  *	Conditions:
5098  *		Nothing locked.
5099  *	Returns:
5100  *		MACH_MSG_SUCCESS	Successful copyout.
5101  *		MACH_MSG_IPC_SPACE	No room for port right in name space.
5102  *		MACH_MSG_VM_SPACE	No room for memory in address space.
5103  *		MACH_MSG_IPC_KERNEL	Resource shortage handling port right.
5104  *		MACH_MSG_VM_KERNEL	Resource shortage handling memory.
5105  *		MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
5106  */
5107 
5108 static mach_msg_return_t
ipc_kmsg_copyout_body(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_option_t option,mach_msg_body_t * slist)5109 ipc_kmsg_copyout_body(
5110 	ipc_kmsg_t              kmsg,
5111 	ipc_space_t             space,
5112 	vm_map_t                map,
5113 	mach_msg_option_t       option,
5114 	mach_msg_body_t         *slist)
5115 {
5116 	mach_msg_body_t             *body;
5117 	mach_msg_descriptor_t       *kern_dsc, *user_dsc;
5118 	mach_msg_descriptor_t       *saddr;
5119 	mach_msg_type_number_t      dsc_count, sdsc_count;
5120 	mach_msg_return_t           mr = MACH_MSG_SUCCESS;
5121 	boolean_t                   is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
5122 
5123 	body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
5124 	dsc_count = body->msgh_descriptor_count;
5125 	kern_dsc = (mach_msg_descriptor_t *) (body + 1);
5126 	/* Point user_dsc just after the end of all the descriptors */
5127 	user_dsc = &kern_dsc[dsc_count];
5128 
5129 	assert(current_task() != kernel_task);
5130 
5131 	/* Do scatter list setup */
5132 	if (slist != MACH_MSG_BODY_NULL) {
5133 		panic("Scatter lists disabled");
5134 		saddr = (mach_msg_descriptor_t *) (slist + 1);
5135 		sdsc_count = slist->msgh_descriptor_count;
5136 	} else {
5137 		saddr = MACH_MSG_DESCRIPTOR_NULL;
5138 		sdsc_count = 0;
5139 	}
5140 
5141 	/* Now process the descriptors - in reverse order */
5142 	for (mach_msg_type_number_t i = dsc_count; i-- > 0;) {
5143 		switch (kern_dsc[i].type.type) {
5144 		case MACH_MSG_PORT_DESCRIPTOR:
5145 			user_dsc = ipc_kmsg_copyout_port_descriptor(&kern_dsc[i],
5146 			    user_dsc, space, &mr);
5147 			break;
5148 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
5149 		case MACH_MSG_OOL_DESCRIPTOR:
5150 			user_dsc = ipc_kmsg_copyout_ool_descriptor(
5151 				(mach_msg_ool_descriptor_t *)&kern_dsc[i],
5152 				user_dsc, is_task_64bit, map, &mr);
5153 			break;
5154 		case MACH_MSG_OOL_PORTS_DESCRIPTOR:
5155 			user_dsc = ipc_kmsg_copyout_ool_ports_descriptor(
5156 				(mach_msg_ool_ports_descriptor_t *)&kern_dsc[i],
5157 				user_dsc, is_task_64bit, map, space, kmsg, &mr);
5158 			break;
5159 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
5160 			user_dsc = ipc_kmsg_copyout_guarded_port_descriptor(
5161 				(mach_msg_guarded_port_descriptor_t *)&kern_dsc[i],
5162 				user_dsc, is_task_64bit, kmsg, space, option, &mr);
5163 			break;
5164 		default:
5165 			panic("untyped IPC copyout body: invalid message descriptor");
5166 		}
5167 	}
5168 
5169 	if (user_dsc != kern_dsc) {
5170 		vm_offset_t dsc_adjust = (vm_offset_t)user_dsc - (vm_offset_t)kern_dsc;
5171 		memmove((char *)((vm_offset_t)kmsg->ikm_header + dsc_adjust), kmsg->ikm_header, sizeof(mach_msg_base_t));
5172 		kmsg->ikm_header = (mach_msg_header_t *)((vm_offset_t)kmsg->ikm_header + dsc_adjust);
5173 		/* Update the message size for the smaller user representation */
5174 		kmsg->ikm_header->msgh_size -= (mach_msg_size_t)dsc_adjust;
5175 	}
5176 
5177 	return mr;
5178 }
5179 
5180 /*
5181  *	Routine:	ipc_kmsg_copyout_size
5182  *	Purpose:
5183  *		Compute the size of the message as copied out to the given
5184  *		map. If the destination map's pointers are a different size
5185  *		than the kernel's, we have to allow for expansion/
5186  *		contraction of the descriptors as appropriate.
5187  *	Conditions:
5188  *		Nothing locked.
5189  *	Returns:
5190  *		size of the message as it would be received.
5191  */
5192 
5193 mach_msg_size_t
ipc_kmsg_copyout_size(ipc_kmsg_t kmsg,vm_map_t map)5194 ipc_kmsg_copyout_size(
5195 	ipc_kmsg_t              kmsg,
5196 	vm_map_t                map)
5197 {
5198 	mach_msg_size_t             send_size;
5199 
5200 	send_size = kmsg->ikm_header->msgh_size;
5201 
5202 	boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
5203 
5204 #if defined(__LP64__)
5205 	send_size -= USER_HEADER_SIZE_DELTA;
5206 #endif
5207 
5208 	if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
5209 		mach_msg_body_t *body;
5210 		mach_msg_descriptor_t *saddr, *eaddr;
5211 
5212 		body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
5213 		saddr = (mach_msg_descriptor_t *) (body + 1);
5214 		eaddr = saddr + body->msgh_descriptor_count;
5215 
5216 		send_size -= KERNEL_DESC_SIZE * body->msgh_descriptor_count;
5217 		for (; saddr < eaddr; saddr++) {
5218 			send_size += ipc_kmsg_user_desc_size(saddr->type.type,
5219 			    is_task_64bit);
5220 		}
5221 	}
5222 	return send_size;
5223 }
5224 
5225 /*
5226  *	Routine:	ipc_kmsg_copyout
5227  *	Purpose:
5228  *		"Copy-out" port rights and out-of-line memory
5229  *		in the message.
5230  *	Conditions:
5231  *		Nothing locked.
5232  *	Returns:
5233  *		MACH_MSG_SUCCESS	Copied out all rights and memory.
5234  *		MACH_RCV_HEADER_ERROR + special bits
5235  *			Rights and memory in the message are intact.
5236  *		MACH_RCV_BODY_ERROR + special bits
5237  *			The message header was successfully copied out.
5238  *			As much of the body was handled as possible.
5239  */
5240 
5241 mach_msg_return_t
ipc_kmsg_copyout(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_body_t * slist,mach_msg_option_t option)5242 ipc_kmsg_copyout(
5243 	ipc_kmsg_t              kmsg,
5244 	ipc_space_t             space,
5245 	vm_map_t                map,
5246 	mach_msg_body_t         *slist,
5247 	mach_msg_option_t      option)
5248 {
5249 	mach_msg_return_t mr;
5250 
5251 	ikm_validate_sig(kmsg);
5252 
5253 	mr = ipc_kmsg_copyout_header(kmsg, space, option);
5254 	if (mr != MACH_MSG_SUCCESS) {
5255 		return mr;
5256 	}
5257 
5258 	if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
5259 		mr = ipc_kmsg_copyout_body(kmsg, space, map, option, slist);
5260 
5261 		if (mr != MACH_MSG_SUCCESS) {
5262 			mr |= MACH_RCV_BODY_ERROR;
5263 		}
5264 	}
5265 
5266 	return mr;
5267 }
5268 
5269 /*
5270  *	Routine:	ipc_kmsg_copyout_pseudo
5271  *	Purpose:
5272  *		Does a pseudo-copyout of the message.
5273  *		This is like a regular copyout, except
5274  *		that the ports in the header are handled
5275  *		as if they are in the body.  They aren't reversed.
5276  *
5277  *		The error codes are a combination of special bits.
5278  *		The copyout proceeds despite errors.
5279  *	Conditions:
5280  *		Nothing locked.
5281  *	Returns:
5282  *		MACH_MSG_SUCCESS	Successful copyout.
5283  *		MACH_MSG_IPC_SPACE	No room for port right in name space.
5284  *		MACH_MSG_VM_SPACE	No room for memory in address space.
5285  *		MACH_MSG_IPC_KERNEL	Resource shortage handling port right.
5286  *		MACH_MSG_VM_KERNEL	Resource shortage handling memory.
5287  */
5288 
5289 mach_msg_return_t
ipc_kmsg_copyout_pseudo(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_body_t * slist)5290 ipc_kmsg_copyout_pseudo(
5291 	ipc_kmsg_t              kmsg,
5292 	ipc_space_t             space,
5293 	vm_map_t                map,
5294 	mach_msg_body_t         *slist)
5295 {
5296 	mach_msg_bits_t mbits = kmsg->ikm_header->msgh_bits;
5297 	ipc_object_t dest = ip_to_object(kmsg->ikm_header->msgh_remote_port);
5298 	ipc_object_t reply = ip_to_object(kmsg->ikm_header->msgh_local_port);
5299 	ipc_object_t voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
5300 	mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
5301 	mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
5302 	mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
5303 	mach_port_name_t voucher_name = kmsg->ikm_header->msgh_voucher_port;
5304 	mach_port_name_t dest_name, reply_name;
5305 	mach_msg_return_t mr;
5306 
5307 	/* Set ith_knote to ITH_KNOTE_PSEUDO */
5308 	current_thread()->ith_knote = ITH_KNOTE_PSEUDO;
5309 
5310 	ikm_validate_sig(kmsg);
5311 
5312 	assert(IO_VALID(dest));
5313 
5314 #if 0
5315 	/*
5316 	 * If we did this here, it looks like we wouldn't need the undo logic
5317 	 * at the end of ipc_kmsg_send() in the error cases.  Not sure which
5318 	 * would be more elegant to keep.
5319 	 */
5320 	ipc_importance_clean(kmsg);
5321 #else
5322 	/* just assert it is already clean */
5323 	ipc_importance_assert_clean(kmsg);
5324 #endif
5325 
5326 	mr = (ipc_kmsg_copyout_object(space, dest, dest_type, NULL, NULL, &dest_name) |
5327 	    ipc_kmsg_copyout_object(space, reply, reply_type, NULL, NULL, &reply_name));
5328 
5329 	kmsg->ikm_header->msgh_bits = mbits & MACH_MSGH_BITS_USER;
5330 	kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(dest_name);
5331 	kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(reply_name);
5332 
5333 	/* restore the voucher:
5334 	 * If it was copied in via move-send, have to put back a voucher send right.
5335 	 *
5336 	 * If it was copied in via copy-send, the header still contains the old voucher name.
5337 	 * Restore the type and discard the copied-in/pre-processed voucher.
5338 	 */
5339 	if (IO_VALID(voucher)) {
5340 		assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5341 		if (kmsg->ikm_voucher_type == MACH_MSG_TYPE_MOVE_SEND) {
5342 			mr |= ipc_kmsg_copyout_object(space, voucher, voucher_type, NULL, NULL, &voucher_name);
5343 			kmsg->ikm_header->msgh_voucher_port = voucher_name;
5344 		} else {
5345 			assert(kmsg->ikm_voucher_type == MACH_MSG_TYPE_COPY_SEND);
5346 			kmsg->ikm_header->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, MACH_MSG_TYPE_COPY_SEND,
5347 			    MACH_MSGH_BITS_OTHER(kmsg->ikm_header->msgh_bits));
5348 			ipc_object_destroy(voucher, voucher_type);
5349 		}
5350 		ipc_kmsg_clear_voucher_port(kmsg);
5351 	}
5352 
5353 	if (mbits & MACH_MSGH_BITS_COMPLEX) {
5354 		mr |= ipc_kmsg_copyout_body(kmsg, space, map, 0, slist);
5355 	}
5356 
5357 	return mr;
5358 }
5359 
5360 /*
5361  *	Routine:	ipc_kmsg_copyout_dest_to_user
5362  *	Purpose:
5363  *		Copies out the destination port in the message.
5364  *		Destroys all other rights and memory in the message.
5365  *	Conditions:
5366  *		Nothing locked.
5367  */
5368 
5369 void
ipc_kmsg_copyout_dest_to_user(ipc_kmsg_t kmsg,ipc_space_t space)5370 ipc_kmsg_copyout_dest_to_user(
5371 	ipc_kmsg_t      kmsg,
5372 	ipc_space_t     space)
5373 {
5374 	mach_msg_bits_t mbits;
5375 	ipc_object_t dest;
5376 	ipc_object_t reply;
5377 	ipc_object_t voucher;
5378 	mach_msg_type_name_t dest_type;
5379 	mach_msg_type_name_t reply_type;
5380 	mach_msg_type_name_t voucher_type;
5381 	mach_port_name_t dest_name, reply_name, voucher_name;
5382 
5383 	ikm_validate_sig(kmsg);
5384 
5385 	mbits = kmsg->ikm_header->msgh_bits;
5386 	dest = ip_to_object(kmsg->ikm_header->msgh_remote_port);
5387 	reply = ip_to_object(kmsg->ikm_header->msgh_local_port);
5388 	voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
5389 	voucher_name = kmsg->ikm_header->msgh_voucher_port;
5390 	dest_type = MACH_MSGH_BITS_REMOTE(mbits);
5391 	reply_type = MACH_MSGH_BITS_LOCAL(mbits);
5392 	voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
5393 
5394 	assert(IO_VALID(dest));
5395 
5396 	ipc_importance_assert_clean(kmsg);
5397 
5398 	io_lock(dest);
5399 	if (io_active(dest)) {
5400 		ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
5401 		/* dest is unlocked */
5402 	} else {
5403 		io_unlock(dest);
5404 		io_release(dest);
5405 		dest_name = MACH_PORT_DEAD;
5406 	}
5407 
5408 	if (IO_VALID(reply)) {
5409 		ipc_object_destroy(reply, reply_type);
5410 		reply_name = MACH_PORT_NULL;
5411 	} else {
5412 		reply_name = CAST_MACH_PORT_TO_NAME(reply);
5413 	}
5414 
5415 	if (IO_VALID(voucher)) {
5416 		assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5417 		ipc_object_destroy(voucher, voucher_type);
5418 		ipc_kmsg_clear_voucher_port(kmsg);
5419 		voucher_name = MACH_PORT_NULL;
5420 	}
5421 
5422 	kmsg->ikm_header->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
5423 	    voucher_type, mbits);
5424 	kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
5425 	kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
5426 	kmsg->ikm_header->msgh_voucher_port = voucher_name;
5427 
5428 	if (mbits & MACH_MSGH_BITS_COMPLEX) {
5429 		mach_msg_body_t *body;
5430 
5431 		body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
5432 		ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
5433 		    (mach_msg_descriptor_t *)(body + 1));
5434 	}
5435 }
5436 
5437 /*
5438  *	Routine:	ipc_kmsg_copyout_dest_to_kernel
5439  *	Purpose:
5440  *		Copies out the destination and reply ports in the message.
5441  *		Leaves all other rights and memory in the message alone.
5442  *	Conditions:
5443  *		Nothing locked.
5444  *
5445  *	Derived from ipc_kmsg_copyout_dest_to_user.
5446  *	Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
5447  *	We really do want to save rights and memory.
5448  */
5449 
5450 void
ipc_kmsg_copyout_dest_to_kernel(ipc_kmsg_t kmsg,ipc_space_t space)5451 ipc_kmsg_copyout_dest_to_kernel(
5452 	ipc_kmsg_t      kmsg,
5453 	ipc_space_t     space)
5454 {
5455 	ipc_object_t dest;
5456 	mach_port_t reply;
5457 	mach_msg_type_name_t dest_type;
5458 	mach_msg_type_name_t reply_type;
5459 	mach_port_name_t dest_name;
5460 
5461 	ikm_validate_sig(kmsg);
5462 
5463 	dest = ip_to_object(kmsg->ikm_header->msgh_remote_port);
5464 	reply = kmsg->ikm_header->msgh_local_port;
5465 	dest_type = MACH_MSGH_BITS_REMOTE(kmsg->ikm_header->msgh_bits);
5466 	reply_type = MACH_MSGH_BITS_LOCAL(kmsg->ikm_header->msgh_bits);
5467 
5468 	assert(IO_VALID(dest));
5469 
5470 	io_lock(dest);
5471 	if (io_active(dest)) {
5472 		ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
5473 		/* dest is unlocked */
5474 	} else {
5475 		io_unlock(dest);
5476 		io_release(dest);
5477 		dest_name = MACH_PORT_DEAD;
5478 	}
5479 
5480 	/*
5481 	 * While MIG kernel users don't receive vouchers, the
5482 	 * msgh_voucher_port field is intended to be round-tripped through the
5483 	 * kernel if there is no voucher disposition set. Here we check for a
5484 	 * non-zero voucher disposition, and consume the voucher send right as
5485 	 * there is no possible way to specify MACH_RCV_VOUCHER semantics.
5486 	 */
5487 	mach_msg_type_name_t voucher_type;
5488 	voucher_type = MACH_MSGH_BITS_VOUCHER(kmsg->ikm_header->msgh_bits);
5489 	if (voucher_type != MACH_MSGH_BITS_ZERO) {
5490 		ipc_port_t voucher = ipc_kmsg_get_voucher_port(kmsg);
5491 
5492 		assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5493 		/*
5494 		 * someone managed to send this kernel routine a message with
5495 		 * a voucher in it. Cleanup the reference in
5496 		 * kmsg->ikm_voucher.
5497 		 */
5498 		if (IP_VALID(voucher)) {
5499 			ipc_port_release_send(voucher);
5500 		}
5501 		kmsg->ikm_header->msgh_voucher_port = 0;
5502 		ipc_kmsg_clear_voucher_port(kmsg);
5503 	}
5504 
5505 	kmsg->ikm_header->msgh_bits =
5506 	    (MACH_MSGH_BITS_OTHER(kmsg->ikm_header->msgh_bits) |
5507 	    MACH_MSGH_BITS(reply_type, dest_type));
5508 	kmsg->ikm_header->msgh_local_port =  CAST_MACH_NAME_TO_PORT(dest_name);
5509 	kmsg->ikm_header->msgh_remote_port = reply;
5510 }
5511 
5512 /*
5513  * Caller has a reference to the kmsg and the mqueue lock held.
5514  *
5515  * As such, we can safely return a pointer to the thread group in the kmsg and
5516  * not an additional reference. It is up to the caller to decide to take an
5517  * additional reference on the thread group while still holding the mqueue lock,
5518  * if needed.
5519  */
5520 #if CONFIG_PREADOPT_TG
5521 struct thread_group *
ipc_kmsg_get_thread_group(ipc_kmsg_t kmsg)5522 ipc_kmsg_get_thread_group(ipc_kmsg_t kmsg)
5523 {
5524 	struct thread_group *tg = NULL;
5525 	kern_return_t __assert_only kr;
5526 
5527 	ipc_voucher_t voucher = convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg));
5528 	kr = bank_get_preadopt_thread_group(voucher, &tg);
5529 	ipc_voucher_release(voucher);
5530 
5531 	return tg;
5532 }
5533 #endif
5534 
5535 #ifdef __arm64__
5536 /*
5537  * Just sets those parts of the trailer that aren't set up at allocation time.
5538  */
5539 static void
ipc_kmsg_munge_trailer(mach_msg_max_trailer_t * in,void * _out,boolean_t is64bit)5540 ipc_kmsg_munge_trailer(mach_msg_max_trailer_t *in, void *_out, boolean_t is64bit)
5541 {
5542 	if (is64bit) {
5543 		mach_msg_max_trailer64_t *out = (mach_msg_max_trailer64_t*)_out;
5544 		out->msgh_seqno = in->msgh_seqno;
5545 		out->msgh_context = in->msgh_context;
5546 		out->msgh_trailer_size = in->msgh_trailer_size;
5547 		out->msgh_ad = in->msgh_ad;
5548 	} else {
5549 		mach_msg_max_trailer32_t *out = (mach_msg_max_trailer32_t*)_out;
5550 		out->msgh_seqno = in->msgh_seqno;
5551 		out->msgh_context = (mach_port_context32_t)in->msgh_context;
5552 		out->msgh_trailer_size = in->msgh_trailer_size;
5553 		out->msgh_ad = in->msgh_ad;
5554 	}
5555 }
5556 #endif /* __arm64__ */
5557 
5558 mach_msg_trailer_size_t
ipc_kmsg_trailer_size(mach_msg_option_t option,__unused thread_t thread)5559 ipc_kmsg_trailer_size(
5560 	mach_msg_option_t option,
5561 	__unused thread_t thread)
5562 {
5563 	if (!(option & MACH_RCV_TRAILER_MASK)) {
5564 		return MACH_MSG_TRAILER_MINIMUM_SIZE;
5565 	} else {
5566 		return REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread), option);
5567 	}
5568 }
5569 
5570 /*
5571  *	Routine:	ipc_kmsg_init_trailer
5572  *	Purpose:
5573  *		Initiailizes a trailer in a message safely.
5574  */
5575 void
ipc_kmsg_init_trailer(ipc_kmsg_t kmsg,mach_msg_size_t size,task_t sender)5576 ipc_kmsg_init_trailer(
5577 	ipc_kmsg_t          kmsg,
5578 	mach_msg_size_t     size,
5579 	task_t              sender)
5580 {
5581 	static const mach_msg_max_trailer_t KERNEL_TRAILER_TEMPLATE = {
5582 		.msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0,
5583 		.msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE,
5584 		.msgh_sender = KERNEL_SECURITY_TOKEN_VALUE,
5585 		.msgh_audit = KERNEL_AUDIT_TOKEN_VALUE
5586 	};
5587 
5588 	mach_msg_max_trailer_t *trailer;
5589 
5590 	/*
5591 	 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
5592 	 * However, the internal size field of the trailer (msgh_trailer_size)
5593 	 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
5594 	 * the cases where no implicit data is requested.
5595 	 */
5596 	trailer = (mach_msg_max_trailer_t *)((vm_offset_t)kmsg->ikm_header + size);
5597 	if (sender == TASK_NULL) {
5598 		memcpy(trailer, &KERNEL_TRAILER_TEMPLATE, sizeof(*trailer));
5599 	} else {
5600 		bzero(trailer, sizeof(*trailer));
5601 		trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
5602 		trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
5603 		trailer->msgh_sender = *task_get_sec_token(sender);
5604 		trailer->msgh_audit = *task_get_audit_token(sender);
5605 	}
5606 }
5607 
5608 
5609 void
ipc_kmsg_add_trailer(ipc_kmsg_t kmsg,ipc_space_t space __unused,mach_msg_option_t option,__unused thread_t thread,mach_port_seqno_t seqno,boolean_t minimal_trailer,mach_vm_offset_t context)5610 ipc_kmsg_add_trailer(ipc_kmsg_t kmsg, ipc_space_t space __unused,
5611     mach_msg_option_t option, __unused thread_t thread,
5612     mach_port_seqno_t seqno, boolean_t minimal_trailer,
5613     mach_vm_offset_t context)
5614 {
5615 	mach_msg_max_trailer_t *trailer;
5616 
5617 #ifdef __arm64__
5618 	mach_msg_max_trailer_t tmp_trailer; /* This accommodates U64, and we'll munge */
5619 	void *real_trailer_out = (void*)(mach_msg_max_trailer_t *)
5620 	    ((vm_offset_t)kmsg->ikm_header +
5621 	    mach_round_msg(kmsg->ikm_header->msgh_size));
5622 
5623 	/*
5624 	 * Populate scratch with initial values set up at message allocation time.
5625 	 * After, we reinterpret the space in the message as the right type
5626 	 * of trailer for the address space in question.
5627 	 */
5628 	bcopy(real_trailer_out, &tmp_trailer, MAX_TRAILER_SIZE);
5629 	trailer = &tmp_trailer;
5630 #else /* __arm64__ */
5631 	(void)thread;
5632 	trailer = (mach_msg_max_trailer_t *)
5633 	    ((vm_offset_t)kmsg->ikm_header +
5634 	    mach_round_msg(kmsg->ikm_header->msgh_size));
5635 #endif /* __arm64__ */
5636 
5637 	if (!(option & MACH_RCV_TRAILER_MASK)) {
5638 		return;
5639 	}
5640 
5641 	trailer->msgh_seqno = seqno;
5642 	trailer->msgh_context = context;
5643 	trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread), option);
5644 
5645 	if (minimal_trailer) {
5646 		goto done;
5647 	}
5648 
5649 	if (GET_RCV_ELEMENTS(option) >= MACH_RCV_TRAILER_AV) {
5650 		trailer->msgh_ad = 0;
5651 	}
5652 
5653 	/*
5654 	 * The ipc_kmsg_t holds a reference to the label of a label
5655 	 * handle, not the port. We must get a reference to the port
5656 	 * and a send right to copyout to the receiver.
5657 	 */
5658 
5659 	if (option & MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_LABELS)) {
5660 		trailer->msgh_labels.sender = 0;
5661 	}
5662 
5663 done:
5664 #ifdef __arm64__
5665 	ipc_kmsg_munge_trailer(trailer, real_trailer_out, thread_is_64bit_addr(thread));
5666 #endif /* __arm64__ */
5667 	return;
5668 }
5669 
5670 void
ipc_kmsg_set_voucher_port(ipc_kmsg_t kmsg,ipc_port_t voucher_port,mach_msg_type_name_t type)5671 ipc_kmsg_set_voucher_port(
5672 	ipc_kmsg_t           kmsg,
5673 	ipc_port_t           voucher_port,
5674 	mach_msg_type_name_t type)
5675 {
5676 	if (IP_VALID(voucher_port)) {
5677 		assert(ip_kotype(voucher_port) == IKOT_VOUCHER);
5678 	}
5679 	kmsg->ikm_voucher_port = voucher_port;
5680 	kmsg->ikm_voucher_type = type;
5681 }
5682 
5683 ipc_port_t
ipc_kmsg_get_voucher_port(ipc_kmsg_t kmsg)5684 ipc_kmsg_get_voucher_port(ipc_kmsg_t kmsg)
5685 {
5686 	return kmsg->ikm_voucher_port;
5687 }
5688 
5689 void
ipc_kmsg_clear_voucher_port(ipc_kmsg_t kmsg)5690 ipc_kmsg_clear_voucher_port(ipc_kmsg_t kmsg)
5691 {
5692 	kmsg->ikm_voucher_port = IP_NULL;
5693 	kmsg->ikm_voucher_type = MACH_MSGH_BITS_ZERO;
5694 }
5695 
5696 mach_msg_header_t *
ipc_kmsg_msg_header(ipc_kmsg_t kmsg)5697 ipc_kmsg_msg_header(ipc_kmsg_t kmsg)
5698 {
5699 	if (NULL == kmsg) {
5700 		return NULL;
5701 	}
5702 	return kmsg->ikm_header;
5703 }
5704