xref: /xnu-10002.41.9/osfmk/ipc/ipc_port.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1 /*
2  * Copyright (c) 2000-2019 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_FREE_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  */
62 /*
63  */
64 /*
65  *	File:	ipc/ipc_port.c
66  *	Author:	Rich Draves
67  *	Date:	1989
68  *
69  *	Functions to manipulate IPC ports.
70  */
71 
72 #include <mach/boolean.h>
73 #include <mach_assert.h>
74 
75 #include <mach/port.h>
76 #include <mach/kern_return.h>
77 #include <kern/backtrace.h>
78 #include <kern/debug.h>
79 #include <kern/ipc_kobject.h>
80 #include <kern/kcdata.h>
81 #include <kern/misc_protos.h>
82 #include <kern/policy_internal.h>
83 #include <kern/thread.h>
84 #include <kern/waitq.h>
85 #include <kern/host_notify.h>
86 #include <ipc/ipc_entry.h>
87 #include <ipc/ipc_space.h>
88 #include <ipc/ipc_object.h>
89 #include <ipc/ipc_right.h>
90 #include <ipc/ipc_port.h>
91 #include <ipc/ipc_pset.h>
92 #include <ipc/ipc_kmsg.h>
93 #include <ipc/ipc_mqueue.h>
94 #include <ipc/ipc_notify.h>
95 #include <ipc/ipc_importance.h>
96 #include <machine/limits.h>
97 #include <kern/turnstile.h>
98 #include <kern/machine.h>
99 
100 #include <security/mac_mach_internal.h>
101 #include <ipc/ipc_service_port.h>
102 
103 #include <string.h>
104 
105 extern bool proc_is_simulated(struct proc *);
106 extern struct proc *current_proc(void);
107 extern int csproc_hardened_runtime(struct proc* p);
108 
109 static TUNABLE(bool, prioritize_launch, "prioritize_launch", true);
110 TUNABLE_WRITEABLE(int, ipc_portbt, "ipc_portbt", false);
111 
112 extern zone_t ipc_kobject_label_zone;
113 
114 LCK_SPIN_DECLARE_ATTR(ipc_port_multiple_lock_data, &ipc_lck_grp, &ipc_lck_attr);
115 ipc_port_timestamp_t ipc_port_timestamp_data;
116 
117 KALLOC_ARRAY_TYPE_DEFINE(ipc_port_request_table,
118     struct ipc_port_request, KT_DEFAULT);
119 
120 #if     MACH_ASSERT
121 static void ipc_port_init_debug(ipc_port_t, void *fp);
122 #endif  /* MACH_ASSERT */
123 
124 void __abortlike
__ipc_port_inactive_panic(ipc_port_t port)125 __ipc_port_inactive_panic(ipc_port_t port)
126 {
127 	panic("Using inactive port %p", port);
128 }
129 
130 static __abortlike void
__ipc_port_translate_receive_panic(ipc_space_t space,ipc_port_t port)131 __ipc_port_translate_receive_panic(ipc_space_t space, ipc_port_t port)
132 {
133 	panic("found receive right in space %p for port %p owned by space %p",
134 	    space, port, ip_get_receiver(port));
135 }
136 
137 __abortlike void
__ipc_right_delta_overflow_panic(ipc_port_t port,natural_t * field,int delta)138 __ipc_right_delta_overflow_panic(ipc_port_t port, natural_t *field, int delta)
139 {
140 	const char *what;
141 	if (field == &port->ip_srights) {
142 		what = "send right";
143 	} else {
144 		what = "send-once right";
145 	}
146 	panic("port %p %s count overflow (delta: %d)", port, what, delta);
147 }
148 
149 static void
150 ipc_port_send_turnstile_recompute_push_locked(
151 	ipc_port_t port);
152 
153 static thread_t
154 ipc_port_get_watchport_inheritor(
155 	ipc_port_t port);
156 
157 static kern_return_t
158 ipc_port_update_qos_n_iotier(
159 	ipc_port_t port,
160 	uint8_t    qos,
161 	uint8_t    iotier);
162 
163 void
ipc_port_release(ipc_port_t port)164 ipc_port_release(ipc_port_t port)
165 {
166 	ip_release(port);
167 }
168 
169 void
ipc_port_reference(ipc_port_t port)170 ipc_port_reference(ipc_port_t port)
171 {
172 	ip_validate(port);
173 	ip_reference(port);
174 }
175 
176 /*
177  *	Routine:	ipc_port_timestamp
178  *	Purpose:
179  *		Retrieve a timestamp value.
180  */
181 
182 ipc_port_timestamp_t
ipc_port_timestamp(void)183 ipc_port_timestamp(void)
184 {
185 	return OSIncrementAtomic(&ipc_port_timestamp_data);
186 }
187 
188 
189 /*
190  *	Routine:	ipc_port_translate_send
191  *	Purpose:
192  *		Look up a send right in a space.
193  *	Conditions:
194  *		Nothing locked before.  If successful, the object
195  *		is returned active and locked.  The caller doesn't get a ref.
196  *	Returns:
197  *		KERN_SUCCESS		Object returned locked.
198  *		KERN_INVALID_TASK	The space is dead.
199  *		KERN_INVALID_NAME	The name doesn't denote a right
200  *		KERN_INVALID_RIGHT	Name doesn't denote the correct right
201  */
202 kern_return_t
ipc_port_translate_send(ipc_space_t space,mach_port_name_t name,ipc_port_t * portp)203 ipc_port_translate_send(
204 	ipc_space_t                     space,
205 	mach_port_name_t                name,
206 	ipc_port_t                     *portp)
207 {
208 	ipc_port_t port = IP_NULL;
209 	ipc_object_t object;
210 	kern_return_t kr;
211 
212 	kr = ipc_object_translate(space, name, MACH_PORT_RIGHT_SEND, &object);
213 	if (kr == KERN_SUCCESS) {
214 		port = ip_object_to_port(object);
215 	}
216 	*portp = port;
217 	return kr;
218 }
219 
220 
221 /*
222  *	Routine:	ipc_port_translate_receive
223  *	Purpose:
224  *		Look up a receive right in a space.
225  *		Performs some minimal security checks against tampering.
226  *	Conditions:
227  *		Nothing locked before.  If successful, the object
228  *		is returned active and locked.  The caller doesn't get a ref.
229  *	Returns:
230  *		KERN_SUCCESS		Object returned locked.
231  *		KERN_INVALID_TASK	The space is dead.
232  *		KERN_INVALID_NAME	The name doesn't denote a right
233  *		KERN_INVALID_RIGHT	Name doesn't denote the correct right
234  */
235 kern_return_t
ipc_port_translate_receive(ipc_space_t space,mach_port_name_t name,ipc_port_t * portp)236 ipc_port_translate_receive(
237 	ipc_space_t                     space,
238 	mach_port_name_t                name,
239 	ipc_port_t                     *portp)
240 {
241 	ipc_port_t port = IP_NULL;
242 	ipc_object_t object;
243 	kern_return_t kr;
244 
245 	kr = ipc_object_translate(space, name, MACH_PORT_RIGHT_RECEIVE, &object);
246 	if (kr == KERN_SUCCESS) {
247 		/* object is locked */
248 		port = ip_object_to_port(object);
249 		if (!ip_in_space(port, space)) {
250 			__ipc_port_translate_receive_panic(space, port);
251 		}
252 	}
253 	*portp = port;
254 	return kr;
255 }
256 
257 
258 /*
259  *	Routine:	ipc_port_request_alloc
260  *	Purpose:
261  *		Try to allocate a request slot.
262  *		If successful, returns the request index.
263  *		Otherwise returns zero.
264  *	Conditions:
265  *		The port is locked and active.
266  *	Returns:
267  *		KERN_SUCCESS		A request index was found.
268  *		KERN_NO_SPACE		No index allocated.
269  */
270 
271 kern_return_t
ipc_port_request_alloc(ipc_port_t port,mach_port_name_t name,ipc_port_t soright,ipc_port_request_opts_t options,ipc_port_request_index_t * indexp)272 ipc_port_request_alloc(
273 	ipc_port_t                      port,
274 	mach_port_name_t                name,
275 	ipc_port_t                      soright,
276 	ipc_port_request_opts_t         options,
277 	ipc_port_request_index_t        *indexp)
278 {
279 	ipc_port_request_table_t table;
280 	ipc_port_request_index_t index;
281 	ipc_port_request_t ipr, base;
282 
283 	require_ip_active(port);
284 	assert(name != MACH_PORT_NULL);
285 	assert(soright != IP_NULL);
286 
287 	table = port->ip_requests;
288 	if (table == NULL) {
289 		return KERN_NO_SPACE;
290 	}
291 
292 	base  = ipc_port_request_table_base(table);
293 	index = base->ipr_next;
294 	if (index == 0) {
295 		return KERN_NO_SPACE;
296 	}
297 
298 	ipr = ipc_port_request_table_get(table, index);
299 	assert(ipr->ipr_soright == IP_NULL);
300 
301 	base->ipr_next = ipr->ipr_next;
302 	ipr->ipr_name = name;
303 	ipr->ipr_soright = IPR_SOR_MAKE(soright, options);
304 
305 	if (options == (IPR_SOR_SPARM_MASK | IPR_SOR_SPREQ_MASK) &&
306 	    port->ip_sprequests == 0) {
307 		port->ip_sprequests = 1;
308 	}
309 
310 	*indexp = index;
311 
312 	return KERN_SUCCESS;
313 }
314 
315 
316 /*
317  *	Routine:	ipc_port_request_hnotify_alloc
318  *	Purpose:
319  *		Try to allocate a request slot.
320  *		If successful, returns the request index.
321  *		Otherwise returns zero.
322  *	Conditions:
323  *		The port is locked and active.
324  *	Returns:
325  *		KERN_SUCCESS		A request index was found.
326  *		KERN_NO_SPACE		No index allocated.
327  */
328 
329 kern_return_t
ipc_port_request_hnotify_alloc(ipc_port_t port,struct host_notify_entry * hnotify,ipc_port_request_index_t * indexp)330 ipc_port_request_hnotify_alloc(
331 	ipc_port_t                      port,
332 	struct host_notify_entry       *hnotify,
333 	ipc_port_request_index_t       *indexp)
334 {
335 	ipc_port_request_table_t table;
336 	ipc_port_request_index_t index;
337 	ipc_port_request_t ipr, base;
338 
339 	require_ip_active(port);
340 
341 	table = port->ip_requests;
342 	if (table == NULL) {
343 		return KERN_NO_SPACE;
344 	}
345 
346 	base  = ipc_port_request_table_base(table);
347 	index = base->ipr_next;
348 	if (index == 0) {
349 		return KERN_NO_SPACE;
350 	}
351 
352 	ipr = ipc_port_request_table_get(table, index);
353 	assert(ipr->ipr_soright == IP_NULL);
354 
355 	base->ipr_next = ipr->ipr_next;
356 	ipr->ipr_name = IPR_HOST_NOTIFY;
357 	ipr->ipr_hnotify = hnotify;
358 
359 	*indexp = index;
360 
361 	return KERN_SUCCESS;
362 }
363 
364 /*
365  *	Routine:	ipc_port_request_grow
366  *	Purpose:
367  *		Grow a port's table of requests.
368  *	Conditions:
369  *		The port must be locked and active.
370  *		Nothing else locked; will allocate memory.
371  *		Upon return the port is unlocked.
372  *	Returns:
373  *		KERN_SUCCESS		Grew the table.
374  *		KERN_SUCCESS		Somebody else grew the table.
375  *		KERN_SUCCESS		The port died.
376  *		KERN_RESOURCE_SHORTAGE	Couldn't allocate new table.
377  *		KERN_NO_SPACE		Couldn't grow to desired size
378  */
379 
380 kern_return_t
ipc_port_request_grow(ipc_port_t port)381 ipc_port_request_grow(
382 	ipc_port_t              port)
383 {
384 	ipc_port_request_table_t otable, ntable;
385 	uint32_t osize, nsize;
386 	uint32_t ocount, ncount;
387 
388 	require_ip_active(port);
389 
390 	otable = port->ip_requests;
391 	if (otable) {
392 		osize = ipc_port_request_table_size(otable);
393 	} else {
394 		osize = 0;
395 	}
396 	nsize = ipc_port_request_table_next_size(2, osize, 16);
397 	if (nsize > CONFIG_IPC_TABLE_REQUEST_SIZE_MAX) {
398 		nsize = CONFIG_IPC_TABLE_REQUEST_SIZE_MAX;
399 	}
400 	if (nsize == osize) {
401 		return KERN_RESOURCE_SHORTAGE;
402 	}
403 
404 	ip_reference(port);
405 	ip_mq_unlock(port);
406 
407 	ntable = ipc_port_request_table_alloc_by_size(nsize, Z_WAITOK | Z_ZERO);
408 	if (ntable == NULL) {
409 		ip_release(port);
410 		return KERN_RESOURCE_SHORTAGE;
411 	}
412 
413 	ip_mq_lock(port);
414 
415 	/*
416 	 *	Check that port is still active and that nobody else
417 	 *	has slipped in and grown the table on us.  Note that
418 	 *	just checking if the current table pointer == otable
419 	 *	isn't sufficient; must check ipr_size.
420 	 */
421 
422 	ocount = ipc_port_request_table_size_to_count(osize);
423 	ncount = ipc_port_request_table_size_to_count(nsize);
424 
425 	if (ip_active(port) && port->ip_requests == otable) {
426 		ipc_port_request_index_t free, i;
427 
428 		/* copy old table to new table */
429 
430 		if (otable != NULL) {
431 			memcpy(ipc_port_request_table_base(ntable),
432 			    ipc_port_request_table_base(otable),
433 			    osize);
434 		} else {
435 			ocount = 1;
436 			free   = 0;
437 		}
438 
439 		/* add new elements to the new table's free list */
440 
441 		for (i = ocount; i < ncount; i++) {
442 			ipc_port_request_table_get_nocheck(ntable, i)->ipr_next = free;
443 			free = i;
444 		}
445 
446 		ipc_port_request_table_base(ntable)->ipr_next = free;
447 		port->ip_requests = ntable;
448 		ip_mq_unlock(port);
449 		ip_release(port);
450 
451 		if (otable != NULL) {
452 			ipc_port_request_table_free(&otable);
453 		}
454 	} else {
455 		ip_mq_unlock(port);
456 		ip_release(port);
457 		ipc_port_request_table_free(&ntable);
458 	}
459 
460 	return KERN_SUCCESS;
461 }
462 
463 /*
464  *	Routine:	ipc_port_request_sparm
465  *	Purpose:
466  *		Arm delayed send-possible request.
467  *	Conditions:
468  *		The port must be locked and active.
469  *
470  *		Returns TRUE if the request was armed
471  *		(or armed with importance in that version).
472  */
473 
474 boolean_t
ipc_port_request_sparm(ipc_port_t port,__assert_only mach_port_name_t name,ipc_port_request_index_t index,mach_msg_option_t option,mach_msg_priority_t priority)475 ipc_port_request_sparm(
476 	ipc_port_t                      port,
477 	__assert_only mach_port_name_t  name,
478 	ipc_port_request_index_t        index,
479 	mach_msg_option_t               option,
480 	mach_msg_priority_t             priority)
481 {
482 	if (index != IE_REQ_NONE) {
483 		ipc_port_request_table_t table;
484 		ipc_port_request_t ipr;
485 
486 		require_ip_active(port);
487 
488 		table = port->ip_requests;
489 		assert(table != NULL);
490 
491 		ipr = ipc_port_request_table_get(table, index);
492 		assert(ipr->ipr_name == name);
493 
494 		/* Is there a valid destination? */
495 		if (IPR_SOR_SPREQ(ipr->ipr_soright)) {
496 			ipr->ipr_soright = IPR_SOR_MAKE(ipr->ipr_soright, IPR_SOR_SPARM_MASK);
497 			port->ip_sprequests = 1;
498 
499 			if (option & MACH_SEND_OVERRIDE) {
500 				/* apply override to message queue */
501 				mach_msg_qos_t qos_ovr;
502 				if (mach_msg_priority_is_pthread_priority(priority)) {
503 					qos_ovr = _pthread_priority_thread_qos(priority);
504 				} else {
505 					qos_ovr = mach_msg_priority_overide_qos(priority);
506 				}
507 				if (qos_ovr) {
508 					ipc_mqueue_override_send_locked(&port->ip_messages, qos_ovr);
509 				}
510 			}
511 
512 #if IMPORTANCE_INHERITANCE
513 			if (((option & MACH_SEND_NOIMPORTANCE) == 0) &&
514 			    (port->ip_impdonation != 0) &&
515 			    (port->ip_spimportant == 0) &&
516 			    (((option & MACH_SEND_IMPORTANCE) != 0) ||
517 			    (task_is_importance_donor(current_task())))) {
518 				return TRUE;
519 			}
520 #else
521 			return TRUE;
522 #endif /* IMPORTANCE_INHERITANCE */
523 		}
524 	}
525 	return FALSE;
526 }
527 
528 /*
529  *	Routine:	ipc_port_request_type
530  *	Purpose:
531  *		Determine the type(s) of port requests enabled for a name.
532  *	Conditions:
533  *		The port must be locked or inactive (to avoid table growth).
534  *		The index must not be IE_REQ_NONE and for the name in question.
535  */
536 mach_port_type_t
ipc_port_request_type(ipc_port_t port,__assert_only mach_port_name_t name,ipc_port_request_index_t index)537 ipc_port_request_type(
538 	ipc_port_t                      port,
539 	__assert_only mach_port_name_t  name,
540 	ipc_port_request_index_t        index)
541 {
542 	ipc_port_request_table_t table;
543 	ipc_port_request_t ipr;
544 	mach_port_type_t type = 0;
545 
546 	table = port->ip_requests;
547 	assert(table != NULL);
548 
549 	assert(index != IE_REQ_NONE);
550 	ipr = ipc_port_request_table_get(table, index);
551 	assert(ipr->ipr_name == name);
552 
553 	if (IP_VALID(IPR_SOR_PORT(ipr->ipr_soright))) {
554 		type |= MACH_PORT_TYPE_DNREQUEST;
555 
556 		if (IPR_SOR_SPREQ(ipr->ipr_soright)) {
557 			type |= MACH_PORT_TYPE_SPREQUEST;
558 
559 			if (!IPR_SOR_SPARMED(ipr->ipr_soright)) {
560 				type |= MACH_PORT_TYPE_SPREQUEST_DELAYED;
561 			}
562 		}
563 	}
564 	return type;
565 }
566 
567 /*
568  *	Routine:	ipc_port_request_cancel
569  *	Purpose:
570  *		Cancel a dead-name/send-possible request and return the send-once right.
571  *	Conditions:
572  *		The port must be locked and active.
573  *		The index must not be IPR_REQ_NONE and must correspond with name.
574  */
575 
576 ipc_port_t
ipc_port_request_cancel(ipc_port_t port,__assert_only mach_port_name_t name,ipc_port_request_index_t index)577 ipc_port_request_cancel(
578 	ipc_port_t                      port,
579 	__assert_only mach_port_name_t  name,
580 	ipc_port_request_index_t        index)
581 {
582 	ipc_port_request_table_t table;
583 	ipc_port_request_t base, ipr;
584 	ipc_port_t request = IP_NULL;
585 
586 	require_ip_active(port);
587 	table = port->ip_requests;
588 	base  = ipc_port_request_table_base(table);
589 	assert(table != NULL);
590 
591 	assert(index != IE_REQ_NONE);
592 	ipr = ipc_port_request_table_get(table, index);
593 	assert(ipr->ipr_name == name);
594 	request = IPR_SOR_PORT(ipr->ipr_soright);
595 
596 	/* return ipr to the free list inside the table */
597 	ipr->ipr_next = base->ipr_next;
598 	ipr->ipr_soright = IP_NULL;
599 	base->ipr_next = index;
600 
601 	return request;
602 }
603 
604 
605 /*
606  *	Routine:	ipc_port_nsrequest
607  *	Purpose:
608  *		Make a no-senders request, returning the
609  *		previously registered send-once right.
610  *		Just cancels the previous request if notify is IP_NULL.
611  *	Conditions:
612  *		The port is locked and active.  It is unlocked.
613  *		Consumes a ref for notify (if non-null), and
614  *		returns previous with a ref (if non-null).
615  */
616 
617 void
ipc_port_nsrequest(ipc_port_t port,mach_port_mscount_t sync,ipc_port_t notify,ipc_port_t * previousp)618 ipc_port_nsrequest(
619 	ipc_port_t              port,
620 	mach_port_mscount_t     sync,
621 	ipc_port_t              notify,
622 	ipc_port_t              *previousp)
623 {
624 	ipc_port_t previous;
625 	mach_port_mscount_t mscount;
626 	require_ip_active(port);
627 
628 	assert(!ip_in_space(port, ipc_space_kernel));
629 	assert(port->ip_nsrequest != IP_KOBJECT_NSREQUEST_ARMED);
630 
631 	previous = port->ip_nsrequest;
632 	mscount = port->ip_mscount;
633 
634 	if ((port->ip_srights == 0) && (sync <= mscount) &&
635 	    (notify != IP_NULL)) {
636 		port->ip_nsrequest = IP_NULL;
637 		ip_mq_unlock(port);
638 		ipc_notify_no_senders(notify, mscount, /* kobject */ false);
639 	} else {
640 		port->ip_nsrequest = notify;
641 		ip_mq_unlock(port);
642 	}
643 
644 	*previousp = previous;
645 }
646 
647 
648 /*
649  *	Routine:	ipc_port_clear_receiver
650  *	Purpose:
651  *		Prepares a receive right for transmission/destruction,
652  *		optionally performs mqueue destruction (with port lock held)
653  *
654  *	Conditions:
655  *		The port is locked and active.
656  *	Returns:
657  *		If should_destroy is TRUE, then the return value indicates
658  *		whether the caller needs to reap kmsg structures that should
659  *		be destroyed (by calling ipc_kmsg_reap_delayed)
660  *
661  *              If should_destroy is FALSE, this always returns FALSE
662  */
663 
664 boolean_t
ipc_port_clear_receiver(ipc_port_t port,boolean_t should_destroy,waitq_link_list_t * free_l)665 ipc_port_clear_receiver(
666 	ipc_port_t          port,
667 	boolean_t           should_destroy,
668 	waitq_link_list_t  *free_l)
669 {
670 	ipc_mqueue_t    mqueue = &port->ip_messages;
671 	boolean_t       reap_messages = FALSE;
672 
673 	/*
674 	 * Pull ourselves out of any sets to which we belong.
675 	 * We hold the write space lock or the receive entry has
676 	 * been deleted, so even though this acquires and releases
677 	 * the port lock, we know we won't be added to any other sets.
678 	 */
679 	if (ip_in_pset(port)) {
680 		waitq_unlink_all_locked(&port->ip_waitq, NULL, free_l);
681 		assert(!ip_in_pset(port));
682 	}
683 
684 	/*
685 	 * Send anyone waiting on the port's queue directly away.
686 	 * Also clear the mscount, seqno, guard bits
687 	 */
688 	if (ip_in_a_space(port)) {
689 		ipc_mqueue_changed(ip_get_receiver(port), &port->ip_waitq);
690 	} else {
691 		ipc_mqueue_changed(NULL, &port->ip_waitq);
692 	}
693 	port->ip_mscount = 0;
694 	mqueue->imq_seqno = 0;
695 	port->ip_context = port->ip_guarded = port->ip_strict_guard = 0;
696 
697 	/*
698 	 * clear the immovable bit so the port can move back to anyone listening
699 	 * for the port destroy notification.
700 	 */
701 	port->ip_immovable_receive = 0;
702 
703 	if (should_destroy) {
704 		/*
705 		 * Mark the port and mqueue invalid, preventing further send/receive
706 		 * operations from succeeding. It's important for this to be
707 		 * done under the same lock hold as the ipc_mqueue_changed
708 		 * call to avoid additional threads blocking on an mqueue
709 		 * that's being destroyed.
710 		 *
711 		 * The port active bit needs to be guarded under mqueue lock for
712 		 * turnstiles
713 		 */
714 
715 		/* port transitions to INACTIVE state */
716 		io_bits_andnot(ip_to_object(port), IO_BITS_ACTIVE);
717 		port->ip_receiver_name = MACH_PORT_NULL;
718 		port->ip_timestamp = ipc_port_timestamp();
719 
720 		reap_messages = ipc_mqueue_destroy_locked(mqueue, free_l);
721 	} else {
722 		/* port transtions to IN-LIMBO state */
723 		port->ip_receiver_name = MACH_PORT_NULL;
724 		port->ip_destination = IP_NULL;
725 	}
726 
727 	return reap_messages;
728 }
729 
730 
731 /*
732  *	Routine:	ipc_port_init_validate_flags
733  *	Purpose:
734  *		Validates the flag arguments for ipc_port_init
735  *		so that overlapping flags are not accidentally used together
736  */
737 
738 static kern_return_t
ipc_port_init_validate_flags(ipc_port_init_flags_t flags)739 ipc_port_init_validate_flags(ipc_port_init_flags_t flags)
740 {
741 	uint32_t at_most_one_flags = flags & (IPC_PORT_ENFORCE_REPLY_PORT_SEMANTICS |
742 	    IPC_PORT_ENFORCE_RIGID_REPLY_PORT_SEMANTICS |
743 	    IPC_PORT_INIT_PROVISIONAL_ID_PROT_OPTOUT |
744 	    IPC_PORT_INIT_PROVISIONAL_REPLY);
745 
746 	if (at_most_one_flags & (at_most_one_flags - 1)) {
747 		/* at most one of the listed flags can be set */
748 		return KERN_INVALID_ARGUMENT;
749 	}
750 	return KERN_SUCCESS;
751 }
752 
753 
754 /*
755  *	Routine:	ipc_port_init
756  *	Purpose:
757  *		Initializes a newly-allocated port.
758  *
759  *		The memory is expected to be zero initialized (allocated with Z_ZERO).
760  */
761 
762 void
ipc_port_init(ipc_port_t port,ipc_space_t space,ipc_port_init_flags_t flags,mach_port_name_t name)763 ipc_port_init(
764 	ipc_port_t              port,
765 	ipc_space_t             space,
766 	ipc_port_init_flags_t   flags,
767 	mach_port_name_t        name)
768 {
769 	int policy = SYNC_POLICY_FIFO;
770 	task_t task = TASK_NULL;
771 
772 	/* the port has been 0 initialized when called */
773 
774 	if (flags & IPC_PORT_INIT_FILTER_MESSAGE) {
775 		io_bits_or(ip_to_object(port), IP_BIT_FILTER_MSG);
776 	}
777 	if (flags & IPC_PORT_INIT_LOCKED) {
778 		policy |= SYNC_POLICY_INIT_LOCKED;
779 	}
780 
781 	/* must be done first, many ip_* bits live inside the waitq */
782 	waitq_init(&port->ip_waitq, WQT_PORT, policy);
783 	if (flags & IPC_PORT_INIT_TG_BLOCK_TRACKING) {
784 		port->ip_tg_block_tracking = true;
785 	}
786 	if (flags & IPC_PORT_INIT_SPECIAL_REPLY) {
787 		port->ip_specialreply = true;
788 	}
789 	if ((flags & IPC_PORT_INIT_REPLY) || (flags & IPC_PORT_INIT_SPECIAL_REPLY)) {
790 		task = current_task_early();
791 
792 		/* Strict enforcement of reply port semantics are disabled for 3p - rdar://97441265. */
793 		if (task && task_get_platform_binary(task)) {
794 			port->ip_immovable_receive = true;
795 			ip_mark_reply_port(port);
796 		} else {
797 			ip_mark_provisional_reply_port(port);
798 		}
799 	}
800 	if (flags & IPC_PORT_ENFORCE_REPLY_PORT_SEMANTICS) {
801 		ip_enforce_reply_port_semantics(port);
802 	}
803 	if (flags & IPC_PORT_ENFORCE_RIGID_REPLY_PORT_SEMANTICS) {
804 		ip_enforce_rigid_reply_port_semantics(port);
805 	}
806 	if (flags & IPC_PORT_INIT_PROVISIONAL_REPLY) {
807 		ip_mark_provisional_reply_port(port);
808 	}
809 
810 	if (flags & IPC_PORT_INIT_PROVISIONAL_ID_PROT_OPTOUT) {
811 		ip_mark_id_prot_opt_out(port);
812 	}
813 
814 	port->ip_kernel_qos_override = THREAD_QOS_UNSPECIFIED;
815 	port->ip_kernel_iotier_override = THROTTLE_LEVEL_END;
816 
817 	ipc_mqueue_init(&port->ip_messages);
818 #if MACH_ASSERT
819 	ipc_port_init_debug(port, __builtin_frame_address(0));
820 #endif  /* MACH_ASSERT */
821 
822 	/* port transitions to IN-SPACE state */
823 	port->ip_receiver_name = name;
824 	port->ip_receiver = space;
825 
826 	if (flags & IPC_PORT_INIT_MAKE_SEND_RIGHT) {
827 		port->ip_srights = 1;
828 		port->ip_mscount = 1;
829 	}
830 }
831 
832 /*
833  *	Routine:	ipc_port_alloc
834  *	Purpose:
835  *		Allocate a port.
836  *	Conditions:
837  *		Nothing locked.  If successful, the port is returned
838  *		locked.  (The caller doesn't have a reference.)
839  *	Returns:
840  *		KERN_SUCCESS		The port is allocated.
841  *		KERN_INVALID_TASK	The space is dead.
842  *		KERN_NO_SPACE		No room for an entry in the space.
843  *		KERN_RESOURCE_SHORTAGE	Couldn't allocate memory.
844  */
845 
846 kern_return_t
ipc_port_alloc(ipc_space_t space,ipc_port_init_flags_t flags,mach_port_name_t * namep,ipc_port_t * portp)847 ipc_port_alloc(
848 	ipc_space_t             space,
849 	ipc_port_init_flags_t   flags,
850 	mach_port_name_t        *namep,
851 	ipc_port_t              *portp)
852 {
853 	ipc_port_t port;
854 	mach_port_name_t name;
855 	kern_return_t kr;
856 	mach_port_type_t type = MACH_PORT_TYPE_RECEIVE;
857 	mach_port_urefs_t urefs = 0;
858 
859 	kr = ipc_port_init_validate_flags(flags);
860 	if (kr != KERN_SUCCESS) {
861 		return kr;
862 	}
863 
864 	if (flags & IPC_PORT_INIT_MAKE_SEND_RIGHT) {
865 		type |= MACH_PORT_TYPE_SEND;
866 		urefs = 1;
867 	}
868 	kr = ipc_object_alloc(space, IOT_PORT, type, urefs,
869 	    &name, (ipc_object_t *) &port);
870 	if (kr != KERN_SUCCESS) {
871 		return kr;
872 	}
873 
874 	/* space is locked */
875 	ipc_port_init(port, space, flags | IPC_PORT_INIT_LOCKED, name);
876 	/* port is locked */
877 #if MACH_ASSERT
878 	ipc_port_init_debug(port, __builtin_frame_address(0));
879 #endif  /* MACH_ASSERT */
880 
881 	/* unlock space after init */
882 	is_write_unlock(space);
883 
884 	*namep = name;
885 	*portp = port;
886 
887 	return KERN_SUCCESS;
888 }
889 
890 /*
891  *	Routine:	ipc_port_alloc_name
892  *	Purpose:
893  *		Allocate a port, with a specific name.
894  *	Conditions:
895  *		Nothing locked.  If successful, the port is returned
896  *		locked.  (The caller doesn't have a reference.)
897  *	Returns:
898  *		KERN_SUCCESS		The port is allocated.
899  *		KERN_INVALID_TASK	The space is dead.
900  *		KERN_NAME_EXISTS	The name already denotes a right.
901  *		KERN_RESOURCE_SHORTAGE	Couldn't allocate memory.
902  */
903 
904 kern_return_t
ipc_port_alloc_name(ipc_space_t space,ipc_port_init_flags_t flags,mach_port_name_t name,ipc_port_t * portp)905 ipc_port_alloc_name(
906 	ipc_space_t             space,
907 	ipc_port_init_flags_t   flags,
908 	mach_port_name_t        name,
909 	ipc_port_t              *portp)
910 {
911 	mach_port_type_t type = MACH_PORT_TYPE_RECEIVE;
912 	mach_port_urefs_t urefs = 0;
913 
914 	kern_return_t kr = ipc_port_init_validate_flags(flags);
915 	if (kr != KERN_SUCCESS) {
916 		return kr;
917 	}
918 
919 	if (flags & IPC_PORT_INIT_MAKE_SEND_RIGHT) {
920 		type |= MACH_PORT_TYPE_SEND;
921 		urefs = 1;
922 	}
923 	flags |= IPC_PORT_INIT_LOCKED;
924 
925 	return ipc_object_alloc_name(space, IOT_PORT, type, urefs,
926 	           name, (ipc_object_t *)portp, ^(ipc_object_t object){
927 		ipc_port_init(ip_object_to_port(object), space, flags, name);
928 	});
929 }
930 
931 /*
932  *      Routine:	ipc_port_spnotify
933  *	Purpose:
934  *		Generate send-possible port notifications.
935  *	Conditions:
936  *		Nothing locked, reference held on port.
937  */
938 void
ipc_port_spnotify(ipc_port_t port)939 ipc_port_spnotify(
940 	ipc_port_t      port)
941 {
942 	ipc_port_request_index_t index = 0;
943 	ipc_table_elems_t size = 0;
944 
945 	/*
946 	 * If the port has no send-possible request
947 	 * armed, don't bother to lock the port.
948 	 */
949 	if (port->ip_sprequests == 0) {
950 		return;
951 	}
952 
953 	ip_mq_lock(port);
954 
955 #if IMPORTANCE_INHERITANCE
956 	if (port->ip_spimportant != 0) {
957 		port->ip_spimportant = 0;
958 		if (ipc_port_importance_delta(port, IPID_OPTION_NORMAL, -1) == TRUE) {
959 			ip_mq_lock(port);
960 		}
961 	}
962 #endif /* IMPORTANCE_INHERITANCE */
963 
964 	if (port->ip_sprequests == 0) {
965 		ip_mq_unlock(port);
966 		return;
967 	}
968 	port->ip_sprequests = 0;
969 
970 revalidate:
971 	if (ip_active(port)) {
972 		ipc_port_request_table_t requests;
973 
974 		/* table may change each time port unlocked (reload) */
975 		requests = port->ip_requests;
976 		assert(requests != NULL);
977 
978 		/*
979 		 * no need to go beyond table size when first
980 		 * we entered - those are future notifications.
981 		 */
982 		if (size == 0) {
983 			size = ipc_port_request_table_count(requests);
984 		}
985 
986 		/* no need to backtrack either */
987 		while (++index < size) {
988 			ipc_port_request_t ipr = ipc_port_request_table_get_nocheck(requests, index);
989 			mach_port_name_t name = ipr->ipr_name;
990 			ipc_port_t soright = IPR_SOR_PORT(ipr->ipr_soright);
991 			boolean_t armed = IPR_SOR_SPARMED(ipr->ipr_soright);
992 
993 			if (MACH_PORT_VALID(name) && armed && IP_VALID(soright)) {
994 				/* claim send-once right - slot still inuse */
995 				assert(name != IPR_HOST_NOTIFY);
996 				ipr->ipr_soright = IP_NULL;
997 				ip_mq_unlock(port);
998 
999 				ipc_notify_send_possible(soright, name);
1000 
1001 				ip_mq_lock(port);
1002 				goto revalidate;
1003 			}
1004 		}
1005 	}
1006 	ip_mq_unlock(port);
1007 	return;
1008 }
1009 
1010 /*
1011  *      Routine:	ipc_port_dnnotify
1012  *	Purpose:
1013  *		Generate dead name notifications for
1014  *		all outstanding dead-name and send-
1015  *		possible requests.
1016  *	Conditions:
1017  *		Nothing locked.
1018  *		Port must be inactive.
1019  *		Reference held on port.
1020  */
1021 void
ipc_port_dnnotify(ipc_port_t port)1022 ipc_port_dnnotify(
1023 	ipc_port_t      port)
1024 {
1025 	ipc_port_request_table_t requests = port->ip_requests;
1026 
1027 	assert(!ip_active(port));
1028 	if (requests != NULL) {
1029 		ipc_port_request_t ipr = ipc_port_request_table_base(requests);
1030 
1031 		while ((ipr = ipc_port_request_table_next_elem(requests, ipr))) {
1032 			mach_port_name_t name = ipr->ipr_name;
1033 			ipc_port_t soright;
1034 
1035 			switch (name) {
1036 			case MACH_PORT_DEAD:
1037 			case MACH_PORT_NULL:
1038 				break;
1039 			case IPR_HOST_NOTIFY:
1040 				host_notify_cancel(ipr->ipr_hnotify);
1041 				break;
1042 			default:
1043 				soright = IPR_SOR_PORT(ipr->ipr_soright);
1044 				if (IP_VALID(soright)) {
1045 					ipc_notify_dead_name(soright, name);
1046 				}
1047 				break;
1048 			}
1049 		}
1050 	}
1051 }
1052 
1053 /*
1054  *	Routine:	ipc_port_destroy
1055  *	Purpose:
1056  *		Destroys a port.  Cleans up queued messages.
1057  *
1058  *		If the port has a backup, it doesn't get destroyed,
1059  *		but is sent in a port-destroyed notification to the backup.
1060  *	Conditions:
1061  *		The port is locked and alive; nothing else locked.
1062  *		The caller has a reference, which is consumed.
1063  *		Afterwards, the port is unlocked and dead.
1064  */
1065 
1066 void
ipc_port_destroy(ipc_port_t port)1067 ipc_port_destroy(ipc_port_t port)
1068 {
1069 	bool special_reply = port->ip_specialreply;
1070 	bool service_port  = port->ip_service_port;
1071 	bool reap_msgs;
1072 
1073 	ipc_port_t pdrequest = IP_NULL;
1074 	struct task_watchport_elem *twe = NULL;
1075 	waitq_link_list_t free_l = { };
1076 
1077 #if IMPORTANCE_INHERITANCE
1078 	ipc_importance_task_t release_imp_task = IIT_NULL;
1079 	thread_t self = current_thread();
1080 	boolean_t top = (self->ith_assertions == 0);
1081 	natural_t assertcnt = 0;
1082 #endif /* IMPORTANCE_INHERITANCE */
1083 
1084 	require_ip_active(port);
1085 	/* port->ip_receiver_name is garbage */
1086 	/* port->ip_receiver/port->ip_destination is garbage */
1087 
1088 	/* clear any reply-port context */
1089 	port->ip_reply_context = 0;
1090 
1091 	/* must be done before we access ip_pdrequest */
1092 	twe = ipc_port_clear_watchport_elem_internal(port);
1093 	assert(!port->ip_has_watchport);
1094 
1095 	if (!special_reply) {
1096 		/* we assume the ref for pdrequest */
1097 		pdrequest = port->ip_pdrequest;
1098 		port->ip_pdrequest = IP_NULL;
1099 	} else if (port->ip_tempowner) {
1100 		panic("ipc_port_destroy: invalid state");
1101 	}
1102 
1103 #if IMPORTANCE_INHERITANCE
1104 	/* determine how many assertions to drop and from whom */
1105 	if (port->ip_tempowner != 0) {
1106 		assert(top);
1107 		release_imp_task = ip_get_imp_task(port);
1108 		if (IIT_NULL != release_imp_task) {
1109 			port->ip_imp_task = IIT_NULL;
1110 			assertcnt = port->ip_impcount;
1111 		}
1112 		/* Otherwise, nothing to drop */
1113 	} else {
1114 		assertcnt = port->ip_impcount;
1115 		if (pdrequest != IP_NULL) {
1116 			/* mark in limbo for the journey */
1117 			port->ip_tempowner = 1;
1118 		}
1119 	}
1120 
1121 	if (top) {
1122 		self->ith_assertions = assertcnt;
1123 	}
1124 #endif /* IMPORTANCE_INHERITANCE */
1125 
1126 	/*
1127 	 * If no port-destroyed notification is armed, calling
1128 	 * ipc_port_clear_receiver() will mark the port inactive
1129 	 * and will wakeup any threads which may be blocked receiving on it.
1130 	 */
1131 	reap_msgs = ipc_port_clear_receiver(port, pdrequest == IP_NULL, &free_l);
1132 	assert(!ip_in_pset(port));
1133 	assert(port->ip_mscount == 0);
1134 
1135 	/*
1136 	 * Handle port-destroyed notification
1137 	 */
1138 	if (pdrequest != IP_NULL) {
1139 		assert(reap_msgs == false);
1140 
1141 		if (service_port) {
1142 			assert(port->ip_splabel != NULL);
1143 			if (ipc_service_port_label_is_special_pdrequest((ipc_service_port_label_t)port->ip_splabel)) {
1144 				ipc_service_port_label_set_flag(port->ip_splabel, ISPL_FLAGS_SEND_PD_NOTIFICATION);
1145 			}
1146 		}
1147 
1148 		ipc_port_send_turnstile_recompute_push_locked(port);
1149 		/* port unlocked */
1150 
1151 		/* consumes our refs for port and pdrequest */
1152 		ipc_notify_port_destroyed(pdrequest, port);
1153 	} else {
1154 		ipc_service_port_label_t splabel = NULL;
1155 		ipc_notify_nsenders_t nsrequest;
1156 
1157 		nsrequest = ipc_notify_no_senders_prepare(port);
1158 
1159 		if (!ip_is_kolabeled(port)) {
1160 			splabel = port->ip_splabel;
1161 			port->ip_splabel = NULL;
1162 			port->ip_service_port = false;
1163 		}
1164 
1165 		ipc_port_send_turnstile_recompute_push_locked(port);
1166 		/* port unlocked */
1167 
1168 		/* unlink the kmsg from special reply port */
1169 		if (special_reply) {
1170 			ipc_port_adjust_special_reply_port(port,
1171 			    IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE);
1172 		}
1173 
1174 		/* Deallocate the service/connection port label */
1175 		if (splabel) {
1176 			ipc_service_port_label_dealloc(splabel, service_port);
1177 			splabel = NULL;
1178 		}
1179 
1180 		if (reap_msgs) {
1181 			ipc_kmsg_reap_delayed();
1182 		}
1183 
1184 		if (nsrequest.ns_notify) {
1185 			/*
1186 			 * ipc_notify_no_senders_prepare will consume
1187 			 * the reference for kobjects.
1188 			 */
1189 			assert(!nsrequest.ns_is_kobject);
1190 			ip_mq_lock(nsrequest.ns_notify);
1191 			ipc_notify_send_once_and_unlock(nsrequest.ns_notify); /* consumes ref */
1192 		}
1193 
1194 		/* generate dead-name notifications */
1195 		ipc_port_dnnotify(port);
1196 
1197 		ipc_kobject_destroy(port);
1198 
1199 		ip_release(port); /* consume caller's ref */
1200 	}
1201 
1202 	if (twe) {
1203 		task_watchport_elem_deallocate(twe);
1204 		twe = NULL;
1205 	}
1206 
1207 	waitq_link_free_list(WQT_PORT_SET, &free_l);
1208 
1209 #if IMPORTANCE_INHERITANCE
1210 	if (release_imp_task != IIT_NULL) {
1211 		if (assertcnt > 0) {
1212 			assert(top);
1213 			self->ith_assertions = 0;
1214 			assert(ipc_importance_task_is_any_receiver_type(release_imp_task));
1215 			ipc_importance_task_drop_internal_assertion(release_imp_task, assertcnt);
1216 		}
1217 		ipc_importance_task_release(release_imp_task);
1218 	} else if (assertcnt > 0) {
1219 		if (top) {
1220 			self->ith_assertions = 0;
1221 			release_imp_task = current_task()->task_imp_base;
1222 			if (ipc_importance_task_is_any_receiver_type(release_imp_task)) {
1223 				ipc_importance_task_drop_internal_assertion(release_imp_task, assertcnt);
1224 			}
1225 		}
1226 	}
1227 #endif /* IMPORTANCE_INHERITANCE */
1228 }
1229 
1230 /*
1231  *	Routine:	ipc_port_destination_chain_lock
1232  *	Purpose:
1233  *		Search for the end of the chain (a port not in transit),
1234  *		acquiring locks along the way, and return it in `base`.
1235  *
1236  *		Returns true if a reference was taken on `base`
1237  *
1238  *	Conditions:
1239  *		No ports locked.
1240  *		ipc_port_multiple_lock held.
1241  */
1242 boolean_t
ipc_port_destination_chain_lock(ipc_port_t port,ipc_port_t * base)1243 ipc_port_destination_chain_lock(
1244 	ipc_port_t port,
1245 	ipc_port_t *base)
1246 {
1247 	for (;;) {
1248 		ip_mq_lock(port);
1249 
1250 		if (!ip_active(port)) {
1251 			/*
1252 			 * Active ports that are ip_mq_lock()ed cannot go away.
1253 			 *
1254 			 * But inactive ports at the end of walking
1255 			 * an ip_destination chain are only protected
1256 			 * from space termination cleanup while the entire
1257 			 * chain of ports leading to them is held.
1258 			 *
1259 			 * Callers of this code tend to unlock the chain
1260 			 * in the same order than this walk which doesn't
1261 			 * protect `base` properly when it's inactive.
1262 			 *
1263 			 * In that case, take a reference that the caller
1264 			 * is responsible for releasing.
1265 			 */
1266 			ip_reference(port);
1267 			*base = port;
1268 			return true;
1269 		}
1270 
1271 		/* port is active */
1272 		if (!ip_in_transit(port)) {
1273 			*base = port;
1274 			return false;
1275 		}
1276 
1277 		port = ip_get_destination(port);
1278 	}
1279 }
1280 
1281 
1282 /*
1283  *	Routine:	ipc_port_check_circularity
1284  *	Purpose:
1285  *		Check if queueing "port" in a message for "dest"
1286  *		would create a circular group of ports and messages.
1287  *
1288  *		If no circularity (FALSE returned), then "port"
1289  *		is changed from "in limbo" to "in transit".
1290  *
1291  *		That is, we want to set port->ip_destination == dest,
1292  *		but guaranteeing that this doesn't create a circle
1293  *		port->ip_destination->ip_destination->... == port
1294  *
1295  *	Conditions:
1296  *		No ports locked.  References held for "port" and "dest".
1297  */
1298 
1299 boolean_t
ipc_port_check_circularity(ipc_port_t port,ipc_port_t dest)1300 ipc_port_check_circularity(
1301 	ipc_port_t      port,
1302 	ipc_port_t      dest)
1303 {
1304 #if IMPORTANCE_INHERITANCE
1305 	/* adjust importance counts at the same time */
1306 	return ipc_importance_check_circularity(port, dest);
1307 #else
1308 	ipc_port_t base;
1309 	struct task_watchport_elem *watchport_elem = NULL;
1310 	bool took_base_ref = false;
1311 
1312 	assert(port != IP_NULL);
1313 	assert(dest != IP_NULL);
1314 
1315 	if (port == dest) {
1316 		return TRUE;
1317 	}
1318 	base = dest;
1319 
1320 	/* Check if destination needs a turnstile */
1321 	ipc_port_send_turnstile_prepare(dest);
1322 
1323 	/*
1324 	 *	First try a quick check that can run in parallel.
1325 	 *	No circularity if dest is not in transit.
1326 	 */
1327 	ip_mq_lock(port);
1328 	if (ip_mq_lock_try(dest)) {
1329 		if (!ip_in_transit(dest)) {
1330 			goto not_circular;
1331 		}
1332 
1333 		/* dest is in transit; further checking necessary */
1334 
1335 		ip_mq_unlock(dest);
1336 	}
1337 	ip_mq_unlock(port);
1338 
1339 	ipc_port_multiple_lock(); /* massive serialization */
1340 
1341 	/*
1342 	 *	Search for the end of the chain (a port not in transit),
1343 	 *	acquiring locks along the way.
1344 	 */
1345 
1346 	took_base_ref = ipc_port_destination_chain_lock(dest, &base);
1347 	/* all ports in chain from dest to base, inclusive, are locked */
1348 
1349 	if (port == base) {
1350 		/* circularity detected! */
1351 
1352 		ipc_port_multiple_unlock();
1353 
1354 		/* port (== base) is in limbo */
1355 		require_ip_active(port);
1356 		assert(ip_in_limbo(port));
1357 		assert(!took_base_ref);
1358 
1359 		base = dest;
1360 		while (base != IP_NULL) {
1361 			ipc_port_t next;
1362 
1363 			/* dest is in transit or in limbo */
1364 			require_ip_active(base);
1365 			assert(!ip_in_a_space(base));
1366 
1367 			next = ip_get_destination(base);
1368 			ip_mq_unlock(base);
1369 			base = next;
1370 		}
1371 
1372 		ipc_port_send_turnstile_complete(dest);
1373 		return TRUE;
1374 	}
1375 
1376 	/*
1377 	 *	The guarantee:  lock port while the entire chain is locked.
1378 	 *	Once port is locked, we can take a reference to dest,
1379 	 *	add port to the chain, and unlock everything.
1380 	 */
1381 
1382 	ip_mq_lock(port);
1383 	ipc_port_multiple_unlock();
1384 
1385 not_circular:
1386 	require_ip_active(port);
1387 	assert(ip_in_limbo(port));
1388 
1389 	/* Clear the watchport boost */
1390 	watchport_elem = ipc_port_clear_watchport_elem_internal(port);
1391 
1392 	/* Check if the port is being enqueued as a part of sync bootstrap checkin */
1393 	if (dest->ip_specialreply && dest->ip_sync_bootstrap_checkin) {
1394 		port->ip_sync_bootstrap_checkin = 1;
1395 	}
1396 
1397 	ip_reference(dest);
1398 
1399 	/* port transitions to IN-TRANSIT state */
1400 	assert(port->ip_receiver_name == MACH_PORT_NULL);
1401 	port->ip_destination = dest;
1402 
1403 	/* Setup linkage for source port if it has sync ipc push */
1404 	struct turnstile *send_turnstile = TURNSTILE_NULL;
1405 	if (port_send_turnstile(port)) {
1406 		send_turnstile = turnstile_prepare((uintptr_t)port,
1407 		    port_send_turnstile_address(port),
1408 		    TURNSTILE_NULL, TURNSTILE_SYNC_IPC);
1409 
1410 		/*
1411 		 * What ipc_port_adjust_port_locked would do,
1412 		 * but we need to also drop even more locks before
1413 		 * calling turnstile_update_inheritor_complete().
1414 		 */
1415 		ipc_port_adjust_sync_link_state_locked(port, PORT_SYNC_LINK_ANY, NULL);
1416 
1417 		turnstile_update_inheritor(send_turnstile, port_send_turnstile(dest),
1418 		    (TURNSTILE_INHERITOR_TURNSTILE | TURNSTILE_IMMEDIATE_UPDATE));
1419 
1420 		/* update complete and turnstile complete called after dropping all locks */
1421 	}
1422 	/* now unlock chain */
1423 
1424 	ip_mq_unlock(port);
1425 
1426 	for (;;) {
1427 		ipc_port_t next;
1428 
1429 		if (dest == base) {
1430 			break;
1431 		}
1432 
1433 		/* port is IN-TRANSIT */
1434 		require_ip_active(dest);
1435 		assert(ip_in_transit(dest));
1436 
1437 		next = ip_get_destination(dest);
1438 		ip_mq_unlock(dest);
1439 		dest = next;
1440 	}
1441 
1442 	/* base is not IN-TRANSIT */
1443 	assert(!ip_in_transit(base));
1444 
1445 	ip_mq_unlock(base);
1446 	if (took_base_ref) {
1447 		ip_release(base);
1448 	}
1449 
1450 	/* All locks dropped, call turnstile_update_inheritor_complete for source port's turnstile */
1451 	if (send_turnstile) {
1452 		turnstile_update_inheritor_complete(send_turnstile, TURNSTILE_INTERLOCK_NOT_HELD);
1453 
1454 		/* Take the mq lock to call turnstile complete */
1455 		ip_mq_lock(port);
1456 		turnstile_complete((uintptr_t)port, port_send_turnstile_address(port), NULL, TURNSTILE_SYNC_IPC);
1457 		send_turnstile = TURNSTILE_NULL;
1458 		ip_mq_unlock(port);
1459 		turnstile_cleanup();
1460 	}
1461 
1462 	if (watchport_elem) {
1463 		task_watchport_elem_deallocate(watchport_elem);
1464 	}
1465 
1466 	return FALSE;
1467 #endif /* !IMPORTANCE_INHERITANCE */
1468 }
1469 
1470 /*
1471  *	Routine:	ipc_port_watchport_elem
1472  *	Purpose:
1473  *		Get the port's watchport elem field
1474  *
1475  *	Conditions:
1476  *		port locked
1477  */
1478 static struct task_watchport_elem *
ipc_port_watchport_elem(ipc_port_t port)1479 ipc_port_watchport_elem(ipc_port_t port)
1480 {
1481 	if (port->ip_has_watchport) {
1482 		assert(!port->ip_specialreply);
1483 		return port->ip_twe;
1484 	}
1485 	return NULL;
1486 }
1487 
1488 /*
1489  *	Routine:	ipc_port_update_watchport_elem
1490  *	Purpose:
1491  *		Set the port's watchport elem field
1492  *
1493  *	Conditions:
1494  *		port locked and is not a special reply port.
1495  */
1496 static inline struct task_watchport_elem *
ipc_port_update_watchport_elem(ipc_port_t port,struct task_watchport_elem * we)1497 ipc_port_update_watchport_elem(ipc_port_t port, struct task_watchport_elem *we)
1498 {
1499 	struct task_watchport_elem *old_we;
1500 	ipc_port_t pdrequest;
1501 
1502 	assert(!port->ip_specialreply);
1503 
1504 	/*
1505 	 * Note: ip_pdrequest and ip_twe are unioned.
1506 	 *       and ip_has_watchport controls the union "type"
1507 	 */
1508 	if (port->ip_has_watchport) {
1509 		old_we = port->ip_twe;
1510 		pdrequest = old_we->twe_pdrequest;
1511 		old_we->twe_pdrequest = IP_NULL;
1512 	} else {
1513 		old_we = NULL;
1514 		pdrequest = port->ip_pdrequest;
1515 	}
1516 
1517 	if (we) {
1518 		port->ip_has_watchport = true;
1519 		we->twe_pdrequest = pdrequest;
1520 		port->ip_twe = we;
1521 	} else {
1522 		port->ip_has_watchport = false;
1523 		port->ip_pdrequest = pdrequest;
1524 	}
1525 
1526 	return old_we;
1527 }
1528 
1529 /*
1530  *	Routine:	ipc_special_reply_stash_pid_locked
1531  *	Purpose:
1532  *		Set the pid of process that copied out send once right to special reply port.
1533  *
1534  *	Conditions:
1535  *		port locked
1536  */
1537 static inline void
ipc_special_reply_stash_pid_locked(ipc_port_t port,int pid)1538 ipc_special_reply_stash_pid_locked(ipc_port_t port, int pid)
1539 {
1540 	assert(port->ip_specialreply);
1541 	port->ip_pid = pid;
1542 }
1543 
1544 /*
1545  *	Routine:	ipc_special_reply_get_pid_locked
1546  *	Purpose:
1547  *		Get the pid of process that copied out send once right to special reply port.
1548  *
1549  *	Conditions:
1550  *		port locked
1551  */
1552 int
ipc_special_reply_get_pid_locked(ipc_port_t port)1553 ipc_special_reply_get_pid_locked(ipc_port_t port)
1554 {
1555 	assert(port->ip_specialreply);
1556 	return port->ip_pid;
1557 }
1558 
1559 /*
1560  * Update the recv turnstile inheritor for a port.
1561  *
1562  * Sync IPC through the port receive turnstile only happens for the special
1563  * reply port case. It has three sub-cases:
1564  *
1565  * 1. a send-once right is in transit, and pushes on the send turnstile of its
1566  *    destination mqueue.
1567  *
1568  * 2. a send-once right has been stashed on a knote it was copied out "through",
1569  *    as the first such copied out port.
1570  *
1571  * 3. a send-once right has been stashed on a knote it was copied out "through",
1572  *    as the second or more copied out port.
1573  */
1574 void
ipc_port_recv_update_inheritor(ipc_port_t port,struct turnstile * rcv_turnstile,turnstile_update_flags_t flags)1575 ipc_port_recv_update_inheritor(
1576 	ipc_port_t port,
1577 	struct turnstile *rcv_turnstile,
1578 	turnstile_update_flags_t flags)
1579 {
1580 	struct turnstile *inheritor = TURNSTILE_NULL;
1581 	struct knote *kn;
1582 
1583 	if (ip_active(port) && port->ip_specialreply) {
1584 		ip_mq_lock_held(port);
1585 
1586 		switch (port->ip_sync_link_state) {
1587 		case PORT_SYNC_LINK_PORT:
1588 			if (port->ip_sync_inheritor_port != NULL) {
1589 				inheritor = port_send_turnstile(port->ip_sync_inheritor_port);
1590 			}
1591 			break;
1592 
1593 		case PORT_SYNC_LINK_WORKLOOP_KNOTE:
1594 			kn = port->ip_sync_inheritor_knote;
1595 			inheritor = filt_ipc_kqueue_turnstile(kn);
1596 			break;
1597 
1598 		case PORT_SYNC_LINK_WORKLOOP_STASH:
1599 			inheritor = port->ip_sync_inheritor_ts;
1600 			break;
1601 		}
1602 	}
1603 
1604 	turnstile_update_inheritor(rcv_turnstile, inheritor,
1605 	    flags | TURNSTILE_INHERITOR_TURNSTILE);
1606 }
1607 
1608 /*
1609  * Update the send turnstile inheritor for a port.
1610  *
1611  * Sync IPC through the port send turnstile has 7 possible reasons to be linked:
1612  *
1613  * 1. a special reply port is part of sync ipc for bootstrap checkin and needs
1614  *    to push on thread doing the sync ipc.
1615  *
1616  * 2. a receive right is in transit, and pushes on the send turnstile of its
1617  *    destination mqueue.
1618  *
1619  * 3. port was passed as an exec watchport and port is pushing on main thread
1620  *    of the task.
1621  *
1622  * 4. a receive right has been stashed on a knote it was copied out "through",
1623  *    as the first such copied out port (same as PORT_SYNC_LINK_WORKLOOP_KNOTE
1624  *    for the special reply port)
1625  *
1626  * 5. a receive right has been stashed on a knote it was copied out "through",
1627  *    as the second or more copied out port (same as
1628  *    PORT_SYNC_LINK_WORKLOOP_STASH for the special reply port)
1629  *
1630  * 6. a receive right has been copied out as a part of sync bootstrap checkin
1631  *    and needs to push on thread doing the sync bootstrap checkin.
1632  *
1633  * 7. the receive right is monitored by a knote, and pushes on any that is
1634  *    registered on a workloop. filt_machport makes sure that if such a knote
1635  *    exists, it is kept as the first item in the knote list, so we never need
1636  *    to walk.
1637  */
1638 void
ipc_port_send_update_inheritor(ipc_port_t port,struct turnstile * send_turnstile,turnstile_update_flags_t flags)1639 ipc_port_send_update_inheritor(
1640 	ipc_port_t port,
1641 	struct turnstile *send_turnstile,
1642 	turnstile_update_flags_t flags)
1643 {
1644 	ipc_mqueue_t mqueue = &port->ip_messages;
1645 	turnstile_inheritor_t inheritor = TURNSTILE_INHERITOR_NULL;
1646 	struct knote *kn;
1647 	turnstile_update_flags_t inheritor_flags = TURNSTILE_INHERITOR_TURNSTILE;
1648 
1649 	ip_mq_lock_held(port);
1650 
1651 	if (!ip_active(port)) {
1652 		/* this port is no longer active, it should not push anywhere */
1653 	} else if (port->ip_specialreply) {
1654 		/* Case 1. */
1655 		if (port->ip_sync_bootstrap_checkin && prioritize_launch) {
1656 			inheritor = port->ip_messages.imq_srp_owner_thread;
1657 			inheritor_flags = TURNSTILE_INHERITOR_THREAD;
1658 		}
1659 	} else if (ip_in_transit(port)) {
1660 		/* Case 2. */
1661 		inheritor = port_send_turnstile(ip_get_destination(port));
1662 	} else if (port->ip_has_watchport) {
1663 		/* Case 3. */
1664 		if (prioritize_launch) {
1665 			assert(port->ip_sync_link_state == PORT_SYNC_LINK_ANY);
1666 			inheritor = ipc_port_get_watchport_inheritor(port);
1667 			inheritor_flags = TURNSTILE_INHERITOR_THREAD;
1668 		}
1669 	} else if (port->ip_sync_link_state == PORT_SYNC_LINK_WORKLOOP_KNOTE) {
1670 		/* Case 4. */
1671 		inheritor = filt_ipc_kqueue_turnstile(mqueue->imq_inheritor_knote);
1672 	} else if (port->ip_sync_link_state == PORT_SYNC_LINK_WORKLOOP_STASH) {
1673 		/* Case 5. */
1674 		inheritor = mqueue->imq_inheritor_turnstile;
1675 	} else if (port->ip_sync_link_state == PORT_SYNC_LINK_RCV_THREAD) {
1676 		/* Case 6. */
1677 		if (prioritize_launch) {
1678 			inheritor = port->ip_messages.imq_inheritor_thread_ref;
1679 			inheritor_flags = TURNSTILE_INHERITOR_THREAD;
1680 		}
1681 	} else if ((kn = SLIST_FIRST(&port->ip_klist))) {
1682 		/* Case 7. Push on a workloop that is interested */
1683 		if (filt_machport_kqueue_has_turnstile(kn)) {
1684 			assert(port->ip_sync_link_state == PORT_SYNC_LINK_ANY);
1685 			inheritor = filt_ipc_kqueue_turnstile(kn);
1686 		}
1687 	}
1688 
1689 	turnstile_update_inheritor(send_turnstile, inheritor,
1690 	    flags | inheritor_flags);
1691 }
1692 
1693 /*
1694  *	Routine:	ipc_port_send_turnstile_prepare
1695  *	Purpose:
1696  *		Get a reference on port's send turnstile, if
1697  *		port does not have a send turnstile then allocate one.
1698  *
1699  *	Conditions:
1700  *		Nothing is locked.
1701  */
1702 void
ipc_port_send_turnstile_prepare(ipc_port_t port)1703 ipc_port_send_turnstile_prepare(ipc_port_t port)
1704 {
1705 	struct turnstile *turnstile = TURNSTILE_NULL;
1706 	struct turnstile *send_turnstile = TURNSTILE_NULL;
1707 
1708 retry_alloc:
1709 	ip_mq_lock(port);
1710 
1711 	if (port_send_turnstile(port) == NULL ||
1712 	    port_send_turnstile(port)->ts_prim_count == 0) {
1713 		if (turnstile == TURNSTILE_NULL) {
1714 			ip_mq_unlock(port);
1715 			turnstile = turnstile_alloc();
1716 			goto retry_alloc;
1717 		}
1718 
1719 		send_turnstile = turnstile_prepare((uintptr_t)port,
1720 		    port_send_turnstile_address(port),
1721 		    turnstile, TURNSTILE_SYNC_IPC);
1722 		turnstile = TURNSTILE_NULL;
1723 
1724 		ipc_port_send_update_inheritor(port, send_turnstile,
1725 		    TURNSTILE_IMMEDIATE_UPDATE);
1726 
1727 		/* turnstile complete will be called in ipc_port_send_turnstile_complete */
1728 	}
1729 
1730 	/* Increment turnstile counter */
1731 	port_send_turnstile(port)->ts_prim_count++;
1732 	ip_mq_unlock(port);
1733 
1734 	if (send_turnstile) {
1735 		turnstile_update_inheritor_complete(send_turnstile,
1736 		    TURNSTILE_INTERLOCK_NOT_HELD);
1737 	}
1738 	if (turnstile != TURNSTILE_NULL) {
1739 		turnstile_deallocate(turnstile);
1740 	}
1741 }
1742 
1743 
1744 /*
1745  *	Routine:	ipc_port_send_turnstile_complete
1746  *	Purpose:
1747  *		Drop a ref on the port's send turnstile, if the
1748  *		ref becomes zero, deallocate the turnstile.
1749  *
1750  *	Conditions:
1751  *		The space might be locked, use safe deallocate.
1752  */
1753 void
ipc_port_send_turnstile_complete(ipc_port_t port)1754 ipc_port_send_turnstile_complete(ipc_port_t port)
1755 {
1756 	struct turnstile *turnstile = TURNSTILE_NULL;
1757 
1758 	/* Drop turnstile count on dest port */
1759 	ip_mq_lock(port);
1760 
1761 	port_send_turnstile(port)->ts_prim_count--;
1762 	if (port_send_turnstile(port)->ts_prim_count == 0) {
1763 		turnstile_complete((uintptr_t)port, port_send_turnstile_address(port),
1764 		    &turnstile, TURNSTILE_SYNC_IPC);
1765 		assert(turnstile != TURNSTILE_NULL);
1766 	}
1767 	ip_mq_unlock(port);
1768 	turnstile_cleanup();
1769 
1770 	if (turnstile != TURNSTILE_NULL) {
1771 		turnstile_deallocate_safe(turnstile);
1772 		turnstile = TURNSTILE_NULL;
1773 	}
1774 }
1775 
1776 /*
1777  *	Routine:	ipc_port_rcv_turnstile
1778  *	Purpose:
1779  *		Get the port's receive turnstile
1780  *
1781  *	Conditions:
1782  *		mqueue locked or thread waiting on turnstile is locked.
1783  */
1784 static struct turnstile *
ipc_port_rcv_turnstile(ipc_port_t port)1785 ipc_port_rcv_turnstile(ipc_port_t port)
1786 {
1787 	return *port_rcv_turnstile_address(port);
1788 }
1789 
1790 
1791 /*
1792  *	Routine:	ipc_port_link_special_reply_port
1793  *	Purpose:
1794  *		Link the special reply port with the destination port.
1795  *              Allocates turnstile to dest port.
1796  *
1797  *	Conditions:
1798  *		Nothing is locked.
1799  */
1800 void
ipc_port_link_special_reply_port(ipc_port_t special_reply_port,ipc_port_t dest_port,boolean_t sync_bootstrap_checkin)1801 ipc_port_link_special_reply_port(
1802 	ipc_port_t special_reply_port,
1803 	ipc_port_t dest_port,
1804 	boolean_t sync_bootstrap_checkin)
1805 {
1806 	boolean_t drop_turnstile_ref = FALSE;
1807 	boolean_t special_reply = FALSE;
1808 
1809 	/* Check if dest_port needs a turnstile */
1810 	ipc_port_send_turnstile_prepare(dest_port);
1811 
1812 	/* Lock the special reply port and establish the linkage */
1813 	ip_mq_lock(special_reply_port);
1814 
1815 	special_reply = special_reply_port->ip_specialreply;
1816 
1817 	if (sync_bootstrap_checkin && special_reply) {
1818 		special_reply_port->ip_sync_bootstrap_checkin = 1;
1819 	}
1820 
1821 	/* Check if we need to drop the acquired turnstile ref on dest port */
1822 	if (!special_reply ||
1823 	    special_reply_port->ip_sync_link_state != PORT_SYNC_LINK_ANY ||
1824 	    special_reply_port->ip_sync_inheritor_port != IPC_PORT_NULL) {
1825 		drop_turnstile_ref = TRUE;
1826 	} else {
1827 		/* take a reference on dest_port */
1828 		ip_reference(dest_port);
1829 		special_reply_port->ip_sync_inheritor_port = dest_port;
1830 		special_reply_port->ip_sync_link_state = PORT_SYNC_LINK_PORT;
1831 	}
1832 
1833 	ip_mq_unlock(special_reply_port);
1834 
1835 	if (special_reply) {
1836 		/*
1837 		 * For special reply ports, if the destination port is
1838 		 * marked with the thread group blocked tracking flag,
1839 		 * callout to the performance controller.
1840 		 */
1841 		ipc_port_thread_group_blocked(dest_port);
1842 	}
1843 
1844 	if (drop_turnstile_ref) {
1845 		ipc_port_send_turnstile_complete(dest_port);
1846 	}
1847 
1848 	return;
1849 }
1850 
1851 /*
1852  *	Routine:	ipc_port_thread_group_blocked
1853  *	Purpose:
1854  *		Call thread_group_blocked callout if the port
1855  *	        has ip_tg_block_tracking bit set and the thread
1856  *	        has not made this callout already.
1857  *
1858  *	Conditions:
1859  *		Nothing is locked.
1860  */
1861 void
ipc_port_thread_group_blocked(ipc_port_t port __unused)1862 ipc_port_thread_group_blocked(ipc_port_t port __unused)
1863 {
1864 #if CONFIG_THREAD_GROUPS
1865 	bool port_tg_block_tracking = false;
1866 	thread_t self = current_thread();
1867 
1868 	if (self->thread_group == NULL ||
1869 	    (self->options & TH_OPT_IPC_TG_BLOCKED)) {
1870 		return;
1871 	}
1872 
1873 	port_tg_block_tracking = port->ip_tg_block_tracking;
1874 	if (!port_tg_block_tracking) {
1875 		return;
1876 	}
1877 
1878 	machine_thread_group_blocked(self->thread_group, NULL,
1879 	    PERFCONTROL_CALLOUT_BLOCKING_TG_RENDER_SERVER, self);
1880 
1881 	self->options |= TH_OPT_IPC_TG_BLOCKED;
1882 #endif
1883 }
1884 
1885 /*
1886  *	Routine:	ipc_port_thread_group_unblocked
1887  *	Purpose:
1888  *		Call thread_group_unblocked callout if the
1889  *		thread had previously made a thread_group_blocked
1890  *		callout before (indicated by TH_OPT_IPC_TG_BLOCKED
1891  *		flag on the thread).
1892  *
1893  *	Conditions:
1894  *		Nothing is locked.
1895  */
1896 void
ipc_port_thread_group_unblocked(void)1897 ipc_port_thread_group_unblocked(void)
1898 {
1899 #if CONFIG_THREAD_GROUPS
1900 	thread_t self = current_thread();
1901 
1902 	if (!(self->options & TH_OPT_IPC_TG_BLOCKED)) {
1903 		return;
1904 	}
1905 
1906 	machine_thread_group_unblocked(self->thread_group, NULL,
1907 	    PERFCONTROL_CALLOUT_BLOCKING_TG_RENDER_SERVER, self);
1908 
1909 	self->options &= ~TH_OPT_IPC_TG_BLOCKED;
1910 #endif
1911 }
1912 
1913 #if DEVELOPMENT || DEBUG
1914 inline void
ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port)1915 ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port)
1916 {
1917 	special_reply_port->ip_srp_lost_link = 0;
1918 	special_reply_port->ip_srp_msg_sent = 0;
1919 }
1920 
1921 static inline void
ipc_special_reply_port_msg_sent_reset(ipc_port_t special_reply_port)1922 ipc_special_reply_port_msg_sent_reset(ipc_port_t special_reply_port)
1923 {
1924 	if (special_reply_port->ip_specialreply == 1) {
1925 		special_reply_port->ip_srp_msg_sent = 0;
1926 	}
1927 }
1928 
1929 inline void
ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port)1930 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port)
1931 {
1932 	if (special_reply_port->ip_specialreply == 1) {
1933 		special_reply_port->ip_srp_msg_sent = 1;
1934 	}
1935 }
1936 
1937 static inline void
ipc_special_reply_port_lost_link(ipc_port_t special_reply_port)1938 ipc_special_reply_port_lost_link(ipc_port_t special_reply_port)
1939 {
1940 	if (special_reply_port->ip_specialreply == 1 && special_reply_port->ip_srp_msg_sent == 0) {
1941 		special_reply_port->ip_srp_lost_link = 1;
1942 	}
1943 }
1944 
1945 #else /* DEVELOPMENT || DEBUG */
1946 inline void
ipc_special_reply_port_bits_reset(__unused ipc_port_t special_reply_port)1947 ipc_special_reply_port_bits_reset(__unused ipc_port_t special_reply_port)
1948 {
1949 	return;
1950 }
1951 
1952 static inline void
ipc_special_reply_port_msg_sent_reset(__unused ipc_port_t special_reply_port)1953 ipc_special_reply_port_msg_sent_reset(__unused ipc_port_t special_reply_port)
1954 {
1955 	return;
1956 }
1957 
1958 inline void
ipc_special_reply_port_msg_sent(__unused ipc_port_t special_reply_port)1959 ipc_special_reply_port_msg_sent(__unused ipc_port_t special_reply_port)
1960 {
1961 	return;
1962 }
1963 
1964 static inline void
ipc_special_reply_port_lost_link(__unused ipc_port_t special_reply_port)1965 ipc_special_reply_port_lost_link(__unused ipc_port_t special_reply_port)
1966 {
1967 	return;
1968 }
1969 #endif /* DEVELOPMENT || DEBUG */
1970 
1971 /*
1972  *	Routine:	ipc_port_adjust_special_reply_port_locked
1973  *	Purpose:
1974  *		If the special port has a turnstile, update its inheritor.
1975  *	Condition:
1976  *		Special reply port locked on entry.
1977  *		Special reply port unlocked on return.
1978  *		The passed in port is a special reply port.
1979  *	Returns:
1980  *		None.
1981  */
1982 void
ipc_port_adjust_special_reply_port_locked(ipc_port_t special_reply_port,struct knote * kn,uint8_t flags,boolean_t get_turnstile)1983 ipc_port_adjust_special_reply_port_locked(
1984 	ipc_port_t special_reply_port,
1985 	struct knote *kn,
1986 	uint8_t flags,
1987 	boolean_t get_turnstile)
1988 {
1989 	ipc_port_t dest_port = IPC_PORT_NULL;
1990 	int sync_link_state = PORT_SYNC_LINK_NO_LINKAGE;
1991 	turnstile_inheritor_t inheritor = TURNSTILE_INHERITOR_NULL;
1992 	struct turnstile *ts = TURNSTILE_NULL;
1993 	struct turnstile *port_stashed_turnstile = TURNSTILE_NULL;
1994 
1995 	ip_mq_lock_held(special_reply_port); // ip_sync_link_state is touched
1996 
1997 	if (!special_reply_port->ip_specialreply) {
1998 		// only mach_msg_receive_results_complete() calls this with any port
1999 		assert(get_turnstile);
2000 		goto not_special;
2001 	}
2002 
2003 	if (flags & IPC_PORT_ADJUST_SR_RECEIVED_MSG) {
2004 		ipc_special_reply_port_msg_sent_reset(special_reply_port);
2005 	}
2006 
2007 	if (flags & IPC_PORT_ADJUST_UNLINK_THREAD) {
2008 		special_reply_port->ip_messages.imq_srp_owner_thread = NULL;
2009 	}
2010 
2011 	if (flags & IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN) {
2012 		special_reply_port->ip_sync_bootstrap_checkin = 0;
2013 	}
2014 
2015 	/* Check if the special reply port is marked non-special */
2016 	if (special_reply_port->ip_sync_link_state == PORT_SYNC_LINK_ANY) {
2017 not_special:
2018 		if (get_turnstile) {
2019 			turnstile_complete((uintptr_t)special_reply_port,
2020 			    port_rcv_turnstile_address(special_reply_port), NULL, TURNSTILE_SYNC_IPC);
2021 		}
2022 		ip_mq_unlock(special_reply_port);
2023 		if (get_turnstile) {
2024 			turnstile_cleanup();
2025 		}
2026 		return;
2027 	}
2028 
2029 	if (flags & IPC_PORT_ADJUST_SR_LINK_WORKLOOP) {
2030 		if (ITH_KNOTE_VALID(kn, MACH_MSG_TYPE_PORT_SEND_ONCE)) {
2031 			inheritor = filt_machport_stash_port(kn, special_reply_port,
2032 			    &sync_link_state);
2033 		}
2034 	} else if (flags & IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE) {
2035 		sync_link_state = PORT_SYNC_LINK_ANY;
2036 	}
2037 
2038 	/* Check if need to break linkage */
2039 	if (!get_turnstile && sync_link_state == PORT_SYNC_LINK_NO_LINKAGE &&
2040 	    special_reply_port->ip_sync_link_state == PORT_SYNC_LINK_NO_LINKAGE) {
2041 		ip_mq_unlock(special_reply_port);
2042 		return;
2043 	}
2044 
2045 	switch (special_reply_port->ip_sync_link_state) {
2046 	case PORT_SYNC_LINK_PORT:
2047 		dest_port = special_reply_port->ip_sync_inheritor_port;
2048 		special_reply_port->ip_sync_inheritor_port = IPC_PORT_NULL;
2049 		break;
2050 	case PORT_SYNC_LINK_WORKLOOP_KNOTE:
2051 		special_reply_port->ip_sync_inheritor_knote = NULL;
2052 		break;
2053 	case PORT_SYNC_LINK_WORKLOOP_STASH:
2054 		port_stashed_turnstile = special_reply_port->ip_sync_inheritor_ts;
2055 		special_reply_port->ip_sync_inheritor_ts = NULL;
2056 		break;
2057 	}
2058 
2059 	/*
2060 	 * Stash (or unstash) the server's PID in the ip_sorights field of the
2061 	 * special reply port, so that stackshot can later retrieve who the client
2062 	 * is blocked on.
2063 	 */
2064 	if (special_reply_port->ip_sync_link_state == PORT_SYNC_LINK_PORT &&
2065 	    sync_link_state == PORT_SYNC_LINK_NO_LINKAGE) {
2066 		ipc_special_reply_stash_pid_locked(special_reply_port, pid_from_task(current_task()));
2067 	} else if (special_reply_port->ip_sync_link_state == PORT_SYNC_LINK_NO_LINKAGE &&
2068 	    sync_link_state == PORT_SYNC_LINK_ANY) {
2069 		/* If we are resetting the special reply port, remove the stashed pid. */
2070 		ipc_special_reply_stash_pid_locked(special_reply_port, 0);
2071 	}
2072 
2073 	special_reply_port->ip_sync_link_state = sync_link_state;
2074 
2075 	switch (sync_link_state) {
2076 	case PORT_SYNC_LINK_WORKLOOP_KNOTE:
2077 		special_reply_port->ip_sync_inheritor_knote = kn;
2078 		break;
2079 	case PORT_SYNC_LINK_WORKLOOP_STASH:
2080 		turnstile_reference(inheritor);
2081 		special_reply_port->ip_sync_inheritor_ts = inheritor;
2082 		break;
2083 	case PORT_SYNC_LINK_NO_LINKAGE:
2084 		if (flags & IPC_PORT_ADJUST_SR_ENABLE_EVENT) {
2085 			ipc_special_reply_port_lost_link(special_reply_port);
2086 		}
2087 		break;
2088 	}
2089 
2090 	/* Get thread's turnstile donated to special reply port */
2091 	if (get_turnstile) {
2092 		turnstile_complete((uintptr_t)special_reply_port,
2093 		    port_rcv_turnstile_address(special_reply_port), NULL, TURNSTILE_SYNC_IPC);
2094 	} else {
2095 		ts = ipc_port_rcv_turnstile(special_reply_port);
2096 		if (ts) {
2097 			turnstile_reference(ts);
2098 			ipc_port_recv_update_inheritor(special_reply_port, ts,
2099 			    TURNSTILE_IMMEDIATE_UPDATE);
2100 		}
2101 	}
2102 
2103 	ip_mq_unlock(special_reply_port);
2104 
2105 	if (get_turnstile) {
2106 		turnstile_cleanup();
2107 	} else if (ts) {
2108 		/* Call turnstile cleanup after dropping the interlock */
2109 		turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
2110 		turnstile_deallocate_safe(ts);
2111 	}
2112 
2113 	if (port_stashed_turnstile) {
2114 		turnstile_deallocate_safe(port_stashed_turnstile);
2115 	}
2116 
2117 	/* Release the ref on the dest port and its turnstile */
2118 	if (dest_port) {
2119 		ipc_port_send_turnstile_complete(dest_port);
2120 		/* release the reference on the dest port, space lock might be held */
2121 		ip_release_safe(dest_port);
2122 	}
2123 }
2124 
2125 /*
2126  *	Routine:	ipc_port_adjust_special_reply_port
2127  *	Purpose:
2128  *		If the special port has a turnstile, update its inheritor.
2129  *	Condition:
2130  *		Nothing locked.
2131  *	Returns:
2132  *		None.
2133  */
2134 void
ipc_port_adjust_special_reply_port(ipc_port_t port,uint8_t flags)2135 ipc_port_adjust_special_reply_port(
2136 	ipc_port_t port,
2137 	uint8_t flags)
2138 {
2139 	if (port->ip_specialreply) {
2140 		ip_mq_lock(port);
2141 		ipc_port_adjust_special_reply_port_locked(port, NULL, flags, FALSE);
2142 	}
2143 }
2144 
2145 /*
2146  *	Routine:	ipc_port_adjust_sync_link_state_locked
2147  *	Purpose:
2148  *		Update the sync link state of the port and the
2149  *		turnstile inheritor.
2150  *	Condition:
2151  *		Port locked on entry.
2152  *		Port locked on return.
2153  *	Returns:
2154  *              None.
2155  */
2156 void
ipc_port_adjust_sync_link_state_locked(ipc_port_t port,int sync_link_state,turnstile_inheritor_t inheritor)2157 ipc_port_adjust_sync_link_state_locked(
2158 	ipc_port_t port,
2159 	int sync_link_state,
2160 	turnstile_inheritor_t inheritor)
2161 {
2162 	switch (port->ip_sync_link_state) {
2163 	case PORT_SYNC_LINK_RCV_THREAD:
2164 		/* deallocate the thread reference for the inheritor */
2165 		thread_deallocate_safe(port->ip_messages.imq_inheritor_thread_ref);
2166 		break;
2167 	case PORT_SYNC_LINK_WORKLOOP_STASH:
2168 		/* deallocate the turnstile reference for the inheritor */
2169 		turnstile_deallocate_safe(port->ip_messages.imq_inheritor_turnstile);
2170 		break;
2171 	}
2172 
2173 	klist_init(&port->ip_klist);
2174 
2175 	switch (sync_link_state) {
2176 	case PORT_SYNC_LINK_WORKLOOP_KNOTE:
2177 		port->ip_messages.imq_inheritor_knote = inheritor;
2178 		break;
2179 	case PORT_SYNC_LINK_WORKLOOP_STASH:
2180 		/* knote can be deleted by userspace, take a reference on turnstile */
2181 		turnstile_reference(inheritor);
2182 		port->ip_messages.imq_inheritor_turnstile = inheritor;
2183 		break;
2184 	case PORT_SYNC_LINK_RCV_THREAD:
2185 		/* The thread could exit without clearing port state, take a thread ref */
2186 		thread_reference((thread_t)inheritor);
2187 		port->ip_messages.imq_inheritor_thread_ref = inheritor;
2188 		break;
2189 	default:
2190 		klist_init(&port->ip_klist);
2191 		sync_link_state = PORT_SYNC_LINK_ANY;
2192 	}
2193 
2194 	port->ip_sync_link_state = sync_link_state;
2195 }
2196 
2197 
2198 /*
2199  *	Routine:	ipc_port_adjust_port_locked
2200  *	Purpose:
2201  *		If the port has a turnstile, update its inheritor.
2202  *	Condition:
2203  *		Port locked on entry.
2204  *		Port unlocked on return.
2205  *	Returns:
2206  *		None.
2207  */
2208 void
ipc_port_adjust_port_locked(ipc_port_t port,struct knote * kn,boolean_t sync_bootstrap_checkin)2209 ipc_port_adjust_port_locked(
2210 	ipc_port_t port,
2211 	struct knote *kn,
2212 	boolean_t sync_bootstrap_checkin)
2213 {
2214 	int sync_link_state = PORT_SYNC_LINK_ANY;
2215 	turnstile_inheritor_t inheritor = TURNSTILE_INHERITOR_NULL;
2216 
2217 	ip_mq_lock_held(port); // ip_sync_link_state is touched
2218 	assert(!port->ip_specialreply);
2219 
2220 	if (kn) {
2221 		inheritor = filt_machport_stash_port(kn, port, &sync_link_state);
2222 		if (sync_link_state == PORT_SYNC_LINK_WORKLOOP_KNOTE) {
2223 			inheritor = kn;
2224 		}
2225 	} else if (sync_bootstrap_checkin) {
2226 		inheritor = current_thread();
2227 		sync_link_state = PORT_SYNC_LINK_RCV_THREAD;
2228 	}
2229 
2230 	ipc_port_adjust_sync_link_state_locked(port, sync_link_state, inheritor);
2231 	port->ip_sync_bootstrap_checkin = 0;
2232 
2233 	ipc_port_send_turnstile_recompute_push_locked(port);
2234 	/* port unlocked */
2235 }
2236 
2237 /*
2238  *	Routine:	ipc_port_clear_sync_rcv_thread_boost_locked
2239  *	Purpose:
2240  *		If the port is pushing on rcv thread, clear it.
2241  *	Condition:
2242  *		Port locked on entry
2243  *		Port unlocked on return.
2244  *	Returns:
2245  *		None.
2246  */
2247 void
ipc_port_clear_sync_rcv_thread_boost_locked(ipc_port_t port)2248 ipc_port_clear_sync_rcv_thread_boost_locked(
2249 	ipc_port_t port)
2250 {
2251 	ip_mq_lock_held(port); // ip_sync_link_state is touched
2252 
2253 	if (port->ip_sync_link_state != PORT_SYNC_LINK_RCV_THREAD) {
2254 		ip_mq_unlock(port);
2255 		return;
2256 	}
2257 
2258 	ipc_port_adjust_sync_link_state_locked(port, PORT_SYNC_LINK_ANY, NULL);
2259 
2260 	ipc_port_send_turnstile_recompute_push_locked(port);
2261 	/* port unlocked */
2262 }
2263 
2264 /*
2265  *	Routine:	ipc_port_has_prdrequest
2266  *	Purpose:
2267  *		Returns whether a port has a port-destroyed request armed
2268  *	Condition:
2269  *		Port is locked.
2270  */
2271 bool
ipc_port_has_prdrequest(ipc_port_t port)2272 ipc_port_has_prdrequest(
2273 	ipc_port_t port)
2274 {
2275 	if (port->ip_specialreply) {
2276 		return false;
2277 	}
2278 	if (port->ip_has_watchport) {
2279 		return port->ip_twe->twe_pdrequest != IP_NULL;
2280 	}
2281 	return port->ip_pdrequest != IP_NULL;
2282 }
2283 
2284 /*
2285  *	Routine:	ipc_port_add_watchport_elem_locked
2286  *	Purpose:
2287  *		Transfer the turnstile boost of watchport to task calling exec.
2288  *	Condition:
2289  *		Port locked on entry.
2290  *		Port unlocked on return.
2291  *	Returns:
2292  *		KERN_SUCESS on success.
2293  *		KERN_FAILURE otherwise.
2294  */
2295 kern_return_t
ipc_port_add_watchport_elem_locked(ipc_port_t port,struct task_watchport_elem * watchport_elem,struct task_watchport_elem ** old_elem)2296 ipc_port_add_watchport_elem_locked(
2297 	ipc_port_t                 port,
2298 	struct task_watchport_elem *watchport_elem,
2299 	struct task_watchport_elem **old_elem)
2300 {
2301 	ip_mq_lock_held(port);
2302 
2303 	/* Watchport boost only works for non-special active ports mapped in an ipc space */
2304 	if (!ip_active(port) || port->ip_specialreply || !ip_in_a_space(port)) {
2305 		ip_mq_unlock(port);
2306 		return KERN_FAILURE;
2307 	}
2308 
2309 	if (port->ip_sync_link_state != PORT_SYNC_LINK_ANY) {
2310 		/* Sever the linkage if the port was pushing on knote */
2311 		ipc_port_adjust_sync_link_state_locked(port, PORT_SYNC_LINK_ANY, NULL);
2312 	}
2313 
2314 	*old_elem = ipc_port_update_watchport_elem(port, watchport_elem);
2315 
2316 	ipc_port_send_turnstile_recompute_push_locked(port);
2317 	/* port unlocked */
2318 	return KERN_SUCCESS;
2319 }
2320 
2321 /*
2322  *	Routine:	ipc_port_clear_watchport_elem_internal_conditional_locked
2323  *	Purpose:
2324  *		Remove the turnstile boost of watchport and recompute the push.
2325  *	Condition:
2326  *		Port locked on entry.
2327  *		Port unlocked on return.
2328  *	Returns:
2329  *		KERN_SUCESS on success.
2330  *		KERN_FAILURE otherwise.
2331  */
2332 kern_return_t
ipc_port_clear_watchport_elem_internal_conditional_locked(ipc_port_t port,struct task_watchport_elem * watchport_elem)2333 ipc_port_clear_watchport_elem_internal_conditional_locked(
2334 	ipc_port_t                 port,
2335 	struct task_watchport_elem *watchport_elem)
2336 {
2337 	ip_mq_lock_held(port);
2338 
2339 	if (ipc_port_watchport_elem(port) != watchport_elem) {
2340 		ip_mq_unlock(port);
2341 		return KERN_FAILURE;
2342 	}
2343 
2344 	ipc_port_clear_watchport_elem_internal(port);
2345 	ipc_port_send_turnstile_recompute_push_locked(port);
2346 	/* port unlocked */
2347 	return KERN_SUCCESS;
2348 }
2349 
2350 /*
2351  *	Routine:	ipc_port_replace_watchport_elem_conditional_locked
2352  *	Purpose:
2353  *		Replace the turnstile boost of watchport and recompute the push.
2354  *	Condition:
2355  *		Port locked on entry.
2356  *		Port unlocked on return.
2357  *	Returns:
2358  *		KERN_SUCESS on success.
2359  *		KERN_FAILURE otherwise.
2360  */
2361 kern_return_t
ipc_port_replace_watchport_elem_conditional_locked(ipc_port_t port,struct task_watchport_elem * old_watchport_elem,struct task_watchport_elem * new_watchport_elem)2362 ipc_port_replace_watchport_elem_conditional_locked(
2363 	ipc_port_t                 port,
2364 	struct task_watchport_elem *old_watchport_elem,
2365 	struct task_watchport_elem *new_watchport_elem)
2366 {
2367 	ip_mq_lock_held(port);
2368 
2369 	if (port->ip_specialreply ||
2370 	    ipc_port_watchport_elem(port) != old_watchport_elem) {
2371 		ip_mq_unlock(port);
2372 		return KERN_FAILURE;
2373 	}
2374 
2375 	ipc_port_update_watchport_elem(port, new_watchport_elem);
2376 	ipc_port_send_turnstile_recompute_push_locked(port);
2377 	/* port unlocked */
2378 	return KERN_SUCCESS;
2379 }
2380 
2381 /*
2382  *	Routine:	ipc_port_clear_watchport_elem_internal
2383  *	Purpose:
2384  *		Remove the turnstile boost of watchport.
2385  *	Condition:
2386  *		Port locked on entry.
2387  *		Port locked on return.
2388  *	Returns:
2389  *		Old task_watchport_elem returned.
2390  */
2391 struct task_watchport_elem *
ipc_port_clear_watchport_elem_internal(ipc_port_t port)2392 ipc_port_clear_watchport_elem_internal(
2393 	ipc_port_t                 port)
2394 {
2395 	ip_mq_lock_held(port);
2396 
2397 	if (!port->ip_has_watchport) {
2398 		return NULL;
2399 	}
2400 
2401 	return ipc_port_update_watchport_elem(port, NULL);
2402 }
2403 
2404 /*
2405  *	Routine:	ipc_port_send_turnstile_recompute_push_locked
2406  *	Purpose:
2407  *		Update send turnstile inheritor of port and recompute the push.
2408  *	Condition:
2409  *		Port locked on entry.
2410  *		Port unlocked on return.
2411  *	Returns:
2412  *		None.
2413  */
2414 static void
ipc_port_send_turnstile_recompute_push_locked(ipc_port_t port)2415 ipc_port_send_turnstile_recompute_push_locked(
2416 	ipc_port_t port)
2417 {
2418 	struct turnstile *send_turnstile = port_send_turnstile(port);
2419 	if (send_turnstile) {
2420 		turnstile_reference(send_turnstile);
2421 		ipc_port_send_update_inheritor(port, send_turnstile,
2422 		    TURNSTILE_IMMEDIATE_UPDATE);
2423 	}
2424 	ip_mq_unlock(port);
2425 
2426 	if (send_turnstile) {
2427 		turnstile_update_inheritor_complete(send_turnstile,
2428 		    TURNSTILE_INTERLOCK_NOT_HELD);
2429 		turnstile_deallocate_safe(send_turnstile);
2430 	}
2431 }
2432 
2433 /*
2434  *	Routine:	ipc_port_get_watchport_inheritor
2435  *	Purpose:
2436  *		Returns inheritor for watchport.
2437  *
2438  *	Conditions:
2439  *		mqueue locked.
2440  *	Returns:
2441  *		watchport inheritor.
2442  */
2443 static thread_t
ipc_port_get_watchport_inheritor(ipc_port_t port)2444 ipc_port_get_watchport_inheritor(
2445 	ipc_port_t port)
2446 {
2447 	ip_mq_lock_held(port);
2448 	return ipc_port_watchport_elem(port)->twe_task->watchports->tw_thread;
2449 }
2450 
2451 /*
2452  *	Routine:	ipc_port_get_receiver_task
2453  *	Purpose:
2454  *		Returns receiver task pointer and its pid (if any) for port.
2455  *
2456  *	Conditions:
2457  *		Assumes the port is locked.
2458  */
2459 pid_t
ipc_port_get_receiver_task_locked(ipc_port_t port,uintptr_t * task)2460 ipc_port_get_receiver_task_locked(ipc_port_t port, uintptr_t *task)
2461 {
2462 	task_t receiver = TASK_NULL;
2463 	pid_t pid = -1;
2464 
2465 	if (!port) {
2466 		goto out;
2467 	}
2468 
2469 	if (ip_in_a_space(port) &&
2470 	    !ip_in_space(port, ipc_space_kernel) &&
2471 	    !ip_in_space(port, ipc_space_reply)) {
2472 		receiver = port->ip_receiver->is_task;
2473 		pid = task_pid(receiver);
2474 	}
2475 
2476 out:
2477 	if (task) {
2478 		*task = (uintptr_t)receiver;
2479 	}
2480 	return pid;
2481 }
2482 
2483 /*
2484  *	Routine:	ipc_port_get_receiver_task
2485  *	Purpose:
2486  *		Returns receiver task pointer and its pid (if any) for port.
2487  *
2488  *	Conditions:
2489  *		Nothing locked. The routine takes port lock.
2490  */
2491 pid_t
ipc_port_get_receiver_task(ipc_port_t port,uintptr_t * task)2492 ipc_port_get_receiver_task(ipc_port_t port, uintptr_t *task)
2493 {
2494 	pid_t pid = -1;
2495 
2496 	if (!port) {
2497 		if (task) {
2498 			*task = (uintptr_t)TASK_NULL;
2499 		}
2500 		return pid;
2501 	}
2502 
2503 	ip_mq_lock(port);
2504 	pid = ipc_port_get_receiver_task_locked(port, task);
2505 	ip_mq_unlock(port);
2506 
2507 	return pid;
2508 }
2509 
2510 /*
2511  *	Routine:	ipc_port_impcount_delta
2512  *	Purpose:
2513  *		Adjust only the importance count associated with a port.
2514  *		If there are any adjustments to be made to receiver task,
2515  *		those are handled elsewhere.
2516  *
2517  *		For now, be defensive during deductions to make sure the
2518  *		impcount for the port doesn't underflow zero.  This will
2519  *		go away when the port boost addition is made atomic (see
2520  *		note in ipc_port_importance_delta()).
2521  *	Conditions:
2522  *		The port is referenced and locked.
2523  *		Nothing else is locked.
2524  */
2525 mach_port_delta_t
ipc_port_impcount_delta(ipc_port_t port,mach_port_delta_t delta,ipc_port_t __unused base)2526 ipc_port_impcount_delta(
2527 	ipc_port_t        port,
2528 	mach_port_delta_t delta,
2529 	ipc_port_t        __unused base)
2530 {
2531 	mach_port_delta_t absdelta;
2532 
2533 	if (!ip_active(port)) {
2534 		return 0;
2535 	}
2536 
2537 	/* adding/doing nothing is easy */
2538 	if (delta >= 0) {
2539 		port->ip_impcount += delta;
2540 		return delta;
2541 	}
2542 
2543 	absdelta = 0 - delta;
2544 	if (port->ip_impcount >= absdelta) {
2545 		port->ip_impcount -= absdelta;
2546 		return delta;
2547 	}
2548 
2549 #if (DEVELOPMENT || DEBUG)
2550 	if (ip_in_a_space(port)) {
2551 		task_t target_task = port->ip_receiver->is_task;
2552 		ipc_importance_task_t target_imp = target_task->task_imp_base;
2553 		const char *target_procname;
2554 		int target_pid;
2555 
2556 		if (target_imp != IIT_NULL) {
2557 			target_procname = target_imp->iit_procname;
2558 			target_pid = target_imp->iit_bsd_pid;
2559 		} else {
2560 			target_procname = "unknown";
2561 			target_pid = -1;
2562 		}
2563 		printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
2564 		    "dropping %d assertion(s) but port only has %d remaining.\n",
2565 		    ip_get_receiver_name(port),
2566 		    target_pid, target_procname,
2567 		    absdelta, port->ip_impcount);
2568 	} else if (base != IP_NULL) {
2569 		assert(ip_in_a_space(base));
2570 		task_t target_task = base->ip_receiver->is_task;
2571 		ipc_importance_task_t target_imp = target_task->task_imp_base;
2572 		const char *target_procname;
2573 		int target_pid;
2574 
2575 		if (target_imp != IIT_NULL) {
2576 			target_procname = target_imp->iit_procname;
2577 			target_pid = target_imp->iit_bsd_pid;
2578 		} else {
2579 			target_procname = "unknown";
2580 			target_pid = -1;
2581 		}
2582 		printf("Over-release of importance assertions for port 0x%lx "
2583 		    "enqueued on port 0x%x with receiver pid %d (%s), "
2584 		    "dropping %d assertion(s) but port only has %d remaining.\n",
2585 		    (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port),
2586 		    ip_get_receiver_name(base),
2587 		    target_pid, target_procname,
2588 		    absdelta, port->ip_impcount);
2589 	}
2590 #endif
2591 
2592 	delta = 0 - port->ip_impcount;
2593 	port->ip_impcount = 0;
2594 	return delta;
2595 }
2596 
2597 /*
2598  *	Routine:	ipc_port_importance_delta_internal
2599  *	Purpose:
2600  *		Adjust the importance count through the given port.
2601  *		If the port is in transit, apply the delta throughout
2602  *		the chain. Determine if the there is a task at the
2603  *		base of the chain that wants/needs to be adjusted,
2604  *		and if so, apply the delta.
2605  *	Conditions:
2606  *		The port is referenced and locked on entry.
2607  *		Importance may be locked.
2608  *		Nothing else is locked.
2609  *		The lock may be dropped on exit.
2610  *		Returns TRUE if lock was dropped.
2611  */
2612 #if IMPORTANCE_INHERITANCE
2613 
2614 boolean_t
ipc_port_importance_delta_internal(ipc_port_t port,natural_t options,mach_port_delta_t * deltap,ipc_importance_task_t * imp_task)2615 ipc_port_importance_delta_internal(
2616 	ipc_port_t              port,
2617 	natural_t               options,
2618 	mach_port_delta_t       *deltap,
2619 	ipc_importance_task_t   *imp_task)
2620 {
2621 	ipc_port_t next, base;
2622 	bool dropped = false;
2623 	bool took_base_ref = false;
2624 
2625 	*imp_task = IIT_NULL;
2626 
2627 	if (*deltap == 0) {
2628 		return FALSE;
2629 	}
2630 
2631 	assert(options == IPID_OPTION_NORMAL || options == IPID_OPTION_SENDPOSSIBLE);
2632 
2633 	base = port;
2634 
2635 	/* if port is in transit, have to search for end of chain */
2636 	if (ip_in_transit(port)) {
2637 		dropped = true;
2638 
2639 
2640 		ip_mq_unlock(port);
2641 		ipc_port_multiple_lock(); /* massive serialization */
2642 
2643 		took_base_ref = ipc_port_destination_chain_lock(port, &base);
2644 		/* all ports in chain from port to base, inclusive, are locked */
2645 
2646 		ipc_port_multiple_unlock();
2647 	}
2648 
2649 	/*
2650 	 * If the port lock is dropped b/c the port is in transit, there is a
2651 	 * race window where another thread can drain messages and/or fire a
2652 	 * send possible notification before we get here.
2653 	 *
2654 	 * We solve this race by checking to see if our caller armed the send
2655 	 * possible notification, whether or not it's been fired yet, and
2656 	 * whether or not we've already set the port's ip_spimportant bit. If
2657 	 * we don't need a send-possible boost, then we'll just apply a
2658 	 * harmless 0-boost to the port.
2659 	 */
2660 	if (options & IPID_OPTION_SENDPOSSIBLE) {
2661 		assert(*deltap == 1);
2662 		if (port->ip_sprequests && port->ip_spimportant == 0) {
2663 			port->ip_spimportant = 1;
2664 		} else {
2665 			*deltap = 0;
2666 		}
2667 	}
2668 
2669 	/* unlock down to the base, adjusting boost(s) at each level */
2670 	for (;;) {
2671 		*deltap = ipc_port_impcount_delta(port, *deltap, base);
2672 
2673 		if (port == base) {
2674 			break;
2675 		}
2676 
2677 		/* port is in transit */
2678 		assert(port->ip_tempowner == 0);
2679 		assert(ip_in_transit(port));
2680 		next = ip_get_destination(port);
2681 		ip_mq_unlock(port);
2682 		port = next;
2683 	}
2684 
2685 	/* find the task (if any) to boost according to the base */
2686 	if (ip_active(base)) {
2687 		if (base->ip_tempowner != 0) {
2688 			if (IIT_NULL != ip_get_imp_task(base)) {
2689 				*imp_task = ip_get_imp_task(base);
2690 			}
2691 			/* otherwise don't boost */
2692 		} else if (ip_in_a_space(base)) {
2693 			ipc_space_t space = ip_get_receiver(base);
2694 
2695 			/* only spaces with boost-accepting tasks */
2696 			if (space->is_task != TASK_NULL &&
2697 			    ipc_importance_task_is_any_receiver_type(space->is_task->task_imp_base)) {
2698 				*imp_task = space->is_task->task_imp_base;
2699 			}
2700 		}
2701 	}
2702 
2703 	/*
2704 	 * Only the base is locked.  If we have to hold or drop task
2705 	 * importance assertions, we'll have to drop that lock as well.
2706 	 */
2707 	if (*imp_task != IIT_NULL) {
2708 		/* take a reference before unlocking base */
2709 		ipc_importance_task_reference(*imp_task);
2710 	}
2711 
2712 	if (dropped) {
2713 		ip_mq_unlock(base);
2714 		if (took_base_ref) {
2715 			/* importance lock might be held */
2716 			ip_release_safe(base);
2717 		}
2718 	}
2719 
2720 	return dropped;
2721 }
2722 #endif /* IMPORTANCE_INHERITANCE */
2723 
2724 /*
2725  *	Routine:	ipc_port_importance_delta
2726  *	Purpose:
2727  *		Adjust the importance count through the given port.
2728  *		If the port is in transit, apply the delta throughout
2729  *		the chain.
2730  *
2731  *		If there is a task at the base of the chain that wants/needs
2732  *		to be adjusted, apply the delta.
2733  *	Conditions:
2734  *		The port is referenced and locked on entry.
2735  *		Nothing else is locked.
2736  *		The lock may be dropped on exit.
2737  *		Returns TRUE if lock was dropped.
2738  */
2739 #if IMPORTANCE_INHERITANCE
2740 
2741 boolean_t
ipc_port_importance_delta(ipc_port_t port,natural_t options,mach_port_delta_t delta)2742 ipc_port_importance_delta(
2743 	ipc_port_t              port,
2744 	natural_t               options,
2745 	mach_port_delta_t       delta)
2746 {
2747 	ipc_importance_task_t imp_task = IIT_NULL;
2748 	boolean_t dropped;
2749 
2750 	dropped = ipc_port_importance_delta_internal(port, options, &delta, &imp_task);
2751 
2752 	if (IIT_NULL == imp_task || delta == 0) {
2753 		if (imp_task) {
2754 			ipc_importance_task_release(imp_task);
2755 		}
2756 		return dropped;
2757 	}
2758 
2759 	if (!dropped) {
2760 		ip_mq_unlock(port);
2761 	}
2762 
2763 	assert(ipc_importance_task_is_any_receiver_type(imp_task));
2764 
2765 	if (delta > 0) {
2766 		ipc_importance_task_hold_internal_assertion(imp_task, delta);
2767 	} else {
2768 		ipc_importance_task_drop_internal_assertion(imp_task, -delta);
2769 	}
2770 
2771 	ipc_importance_task_release(imp_task);
2772 	return TRUE;
2773 }
2774 #endif /* IMPORTANCE_INHERITANCE */
2775 
2776 ipc_port_t
ipc_port_make_send_any_locked(ipc_port_t port)2777 ipc_port_make_send_any_locked(
2778 	ipc_port_t      port)
2779 {
2780 	require_ip_active(port);
2781 	port->ip_mscount++;
2782 	ip_srights_inc(port);
2783 	ip_reference(port);
2784 	return port;
2785 }
2786 
2787 ipc_port_t
ipc_port_make_send_any(ipc_port_t port)2788 ipc_port_make_send_any(
2789 	ipc_port_t      port)
2790 {
2791 	ipc_port_t sright = port;
2792 
2793 	if (IP_VALID(port)) {
2794 		ip_mq_lock(port);
2795 		if (ip_active(port)) {
2796 			ipc_port_make_send_any_locked(port);
2797 		} else {
2798 			sright = IP_DEAD;
2799 		}
2800 		ip_mq_unlock(port);
2801 	}
2802 
2803 	return sright;
2804 }
2805 
2806 ipc_port_t
ipc_port_make_send_mqueue(ipc_port_t port)2807 ipc_port_make_send_mqueue(
2808 	ipc_port_t      port)
2809 {
2810 	ipc_port_t sright = port;
2811 	ipc_kobject_type_t kotype;
2812 
2813 	if (IP_VALID(port)) {
2814 		kotype = ip_kotype(port);
2815 
2816 		ip_mq_lock(port);
2817 		if (__improbable(!ip_active(port))) {
2818 			sright = IP_DEAD;
2819 		} else if (kotype == IKOT_NONE) {
2820 			ipc_port_make_send_any_locked(port);
2821 		} else if (kotype == IKOT_TIMER) {
2822 			ipc_kobject_mktimer_require_locked(port);
2823 			ipc_port_make_send_any_locked(port);
2824 		} else {
2825 			sright = IP_NULL;
2826 		}
2827 		ip_mq_unlock(port);
2828 	}
2829 
2830 	return sright;
2831 }
2832 
2833 void
ipc_port_copy_send_any_locked(ipc_port_t port)2834 ipc_port_copy_send_any_locked(
2835 	ipc_port_t      port)
2836 {
2837 	assert(port->ip_srights > 0);
2838 	ip_srights_inc(port);
2839 	ip_reference(port);
2840 }
2841 
2842 ipc_port_t
ipc_port_copy_send_any(ipc_port_t port)2843 ipc_port_copy_send_any(
2844 	ipc_port_t      port)
2845 {
2846 	ipc_port_t sright = port;
2847 
2848 	if (IP_VALID(port)) {
2849 		ip_mq_lock(port);
2850 		if (ip_active(port)) {
2851 			ipc_port_copy_send_any_locked(port);
2852 		} else {
2853 			sright = IP_DEAD;
2854 		}
2855 		ip_mq_unlock(port);
2856 	}
2857 
2858 	return sright;
2859 }
2860 
2861 ipc_port_t
ipc_port_copy_send_mqueue(ipc_port_t port)2862 ipc_port_copy_send_mqueue(
2863 	ipc_port_t      port)
2864 {
2865 	ipc_port_t sright = port;
2866 	ipc_kobject_type_t kotype;
2867 
2868 	if (IP_VALID(port)) {
2869 		kotype = ip_kotype(port);
2870 
2871 		ip_mq_lock(port);
2872 		if (__improbable(!ip_active(port))) {
2873 			sright = IP_DEAD;
2874 		} else if (kotype == IKOT_NONE) {
2875 			ipc_port_copy_send_any_locked(port);
2876 		} else if (kotype == IKOT_TIMER) {
2877 			ipc_kobject_mktimer_require_locked(port);
2878 			ipc_port_copy_send_any_locked(port);
2879 		} else {
2880 			sright = IP_NULL;
2881 		}
2882 		ip_mq_unlock(port);
2883 	}
2884 
2885 	return sright;
2886 }
2887 
2888 /*
2889  *	Routine:	ipc_port_copyout_send
2890  *	Purpose:
2891  *		Copyout a naked send right (possibly null/dead),
2892  *		or if that fails, destroy the right.
2893  *	Conditions:
2894  *		Nothing locked.
2895  */
2896 
2897 static mach_port_name_t
ipc_port_copyout_send_internal(ipc_port_t sright,ipc_space_t space,ipc_object_copyout_flags_t flags)2898 ipc_port_copyout_send_internal(
2899 	ipc_port_t      sright,
2900 	ipc_space_t     space,
2901 	ipc_object_copyout_flags_t flags)
2902 {
2903 	mach_port_name_t name;
2904 
2905 	if (IP_VALID(sright)) {
2906 		kern_return_t kr;
2907 
2908 		kr = ipc_object_copyout(space, ip_to_object(sright),
2909 		    MACH_MSG_TYPE_PORT_SEND, flags, NULL, NULL, &name);
2910 		if (kr != KERN_SUCCESS) {
2911 			if (kr == KERN_INVALID_CAPABILITY) {
2912 				name = MACH_PORT_DEAD;
2913 			} else {
2914 				name = MACH_PORT_NULL;
2915 			}
2916 		}
2917 	} else {
2918 		name = CAST_MACH_PORT_TO_NAME(sright);
2919 	}
2920 
2921 	return name;
2922 }
2923 
2924 mach_port_name_t
ipc_port_copyout_send(ipc_port_t sright,ipc_space_t space)2925 ipc_port_copyout_send(
2926 	ipc_port_t      sright, /* can be invalid */
2927 	ipc_space_t     space)
2928 {
2929 	return ipc_port_copyout_send_internal(sright, space, IPC_OBJECT_COPYOUT_FLAGS_NONE);
2930 }
2931 
2932 /* Used by pthread kext to copyout thread port only */
2933 mach_port_name_t
ipc_port_copyout_send_pinned(ipc_port_t sright,ipc_space_t space)2934 ipc_port_copyout_send_pinned(
2935 	ipc_port_t      sright, /* can be invalid */
2936 	ipc_space_t     space)
2937 {
2938 	assert(space->is_task != TASK_NULL);
2939 
2940 	if (IP_VALID(sright)) {
2941 		assert(ip_kotype(sright) == IKOT_THREAD_CONTROL);
2942 	}
2943 
2944 	if (task_is_pinned(space->is_task)) {
2945 		return ipc_port_copyout_send_internal(sright, space, IPC_OBJECT_COPYOUT_FLAGS_PINNED);
2946 	} else {
2947 		return ipc_port_copyout_send_internal(sright, space, IPC_OBJECT_COPYOUT_FLAGS_NONE);
2948 	}
2949 }
2950 
2951 /*
2952  *	Routine:	ipc_port_release_send_and_unlock
2953  *	Purpose:
2954  *		Release a naked send right.
2955  *		Consumes a ref for the port.
2956  *	Conditions:
2957  *		Port is valid and locked on entry
2958  *		Port is unlocked on exit.
2959  */
2960 void
ipc_port_release_send_and_unlock(ipc_port_t port)2961 ipc_port_release_send_and_unlock(
2962 	ipc_port_t      port)
2963 {
2964 	ipc_notify_nsenders_t nsrequest = { };
2965 
2966 	ip_srights_dec(port);
2967 
2968 	if (ip_active(port) && port->ip_srights == 0) {
2969 		nsrequest = ipc_notify_no_senders_prepare(port);
2970 	}
2971 
2972 	ip_mq_unlock(port);
2973 	ip_release(port);
2974 
2975 	ipc_notify_no_senders_emit(nsrequest);
2976 }
2977 
2978 /*
2979  *	Routine:	ipc_port_release_send
2980  *	Purpose:
2981  *		Release a naked send right.
2982  *		Consumes a ref for the port.
2983  *	Conditions:
2984  *		Nothing locked.
2985  */
2986 
2987 __attribute__((flatten, noinline))
2988 void
ipc_port_release_send(ipc_port_t port)2989 ipc_port_release_send(
2990 	ipc_port_t      port)
2991 {
2992 	if (IP_VALID(port)) {
2993 		ip_mq_lock(port);
2994 		ipc_port_release_send_and_unlock(port);
2995 	}
2996 }
2997 
2998 /*
2999  *	Routine:	ipc_port_make_sonce_locked
3000  *	Purpose:
3001  *		Make a naked send-once right from a receive right.
3002  *	Conditions:
3003  *		The port is locked and active.
3004  */
3005 
3006 ipc_port_t
ipc_port_make_sonce_locked(ipc_port_t port)3007 ipc_port_make_sonce_locked(
3008 	ipc_port_t      port)
3009 {
3010 	require_ip_active(port);
3011 	ip_sorights_inc(port);
3012 	ip_reference(port);
3013 	return port;
3014 }
3015 
3016 /*
3017  *	Routine:	ipc_port_make_sonce
3018  *	Purpose:
3019  *		Make a naked send-once right from a receive right.
3020  *	Conditions:
3021  *		The port is not locked.
3022  */
3023 
3024 ipc_port_t
ipc_port_make_sonce(ipc_port_t port)3025 ipc_port_make_sonce(
3026 	ipc_port_t      port)
3027 {
3028 	if (!IP_VALID(port)) {
3029 		return port;
3030 	}
3031 
3032 	ip_mq_lock(port);
3033 	if (ip_active(port)) {
3034 		ipc_port_make_sonce_locked(port);
3035 		ip_mq_unlock(port);
3036 		return port;
3037 	}
3038 	ip_mq_unlock(port);
3039 	return IP_DEAD;
3040 }
3041 
3042 /*
3043  *	Routine:	ipc_port_release_sonce
3044  *	Purpose:
3045  *		Release a naked send-once right.
3046  *		Consumes a ref for the port.
3047  *
3048  *		In normal situations, this is never used.
3049  *		Send-once rights are only consumed when
3050  *		a message (possibly a send-once notification)
3051  *		is sent to them.
3052  *	Conditions:
3053  *		The port is locked, possibly a space too.
3054  */
3055 void
ipc_port_release_sonce_and_unlock(ipc_port_t port)3056 ipc_port_release_sonce_and_unlock(
3057 	ipc_port_t      port)
3058 {
3059 	ip_mq_lock_held(port);
3060 
3061 	ip_sorights_dec(port);
3062 
3063 	if (port->ip_specialreply) {
3064 		ipc_port_adjust_special_reply_port_locked(port, NULL,
3065 		    IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN, FALSE);
3066 	} else {
3067 		ip_mq_unlock(port);
3068 	}
3069 
3070 	ip_release(port);
3071 }
3072 
3073 /*
3074  *	Routine:	ipc_port_release_sonce
3075  *	Purpose:
3076  *		Release a naked send-once right.
3077  *		Consumes a ref for the port.
3078  *
3079  *		In normal situations, this is never used.
3080  *		Send-once rights are only consumed when
3081  *		a message (possibly a send-once notification)
3082  *		is sent to them.
3083  *	Conditions:
3084  *		Nothing locked except possibly a space.
3085  */
3086 void
ipc_port_release_sonce(ipc_port_t port)3087 ipc_port_release_sonce(
3088 	ipc_port_t      port)
3089 {
3090 	if (IP_VALID(port)) {
3091 		ip_mq_lock(port);
3092 		ipc_port_release_sonce_and_unlock(port);
3093 	}
3094 }
3095 
3096 /*
3097  *	Routine:	ipc_port_release_receive
3098  *	Purpose:
3099  *		Release a naked (in limbo or in transit) receive right.
3100  *		Consumes a ref for the port; destroys the port.
3101  *	Conditions:
3102  *		Nothing locked.
3103  */
3104 
3105 void
ipc_port_release_receive(ipc_port_t port)3106 ipc_port_release_receive(
3107 	ipc_port_t      port)
3108 {
3109 	ipc_port_t dest;
3110 
3111 	if (!IP_VALID(port)) {
3112 		return;
3113 	}
3114 
3115 	ip_mq_lock(port);
3116 	require_ip_active(port);
3117 	assert(!ip_in_a_space(port));
3118 	dest = ip_get_destination(port);
3119 
3120 	ipc_port_destroy(port); /* consumes ref, unlocks */
3121 
3122 	if (dest != IP_NULL) {
3123 		ipc_port_send_turnstile_complete(dest);
3124 		ip_release(dest);
3125 	}
3126 }
3127 
3128 /*
3129  *	Routine:	ipc_port_alloc_special
3130  *	Purpose:
3131  *		Allocate a port in a special space.
3132  *		The new port is returned with one ref.
3133  *		If unsuccessful, IP_NULL is returned.
3134  *	Conditions:
3135  *		Nothing locked.
3136  */
3137 
3138 ipc_port_t
ipc_port_alloc_special(ipc_space_t space,ipc_port_init_flags_t flags)3139 ipc_port_alloc_special(
3140 	ipc_space_t             space,
3141 	ipc_port_init_flags_t   flags)
3142 {
3143 	ipc_port_t port;
3144 
3145 	kern_return_t kr = ipc_port_init_validate_flags(flags);
3146 	if (kr != KERN_SUCCESS) {
3147 		return IP_NULL;
3148 	}
3149 
3150 	port = ip_object_to_port(io_alloc(IOT_PORT, Z_WAITOK | Z_ZERO));
3151 	if (port == IP_NULL) {
3152 		return IP_NULL;
3153 	}
3154 
3155 	os_atomic_init(&port->ip_object.io_bits, io_makebits(IOT_PORT));
3156 	os_atomic_init(&port->ip_object.io_references, 1);
3157 
3158 	ipc_port_init(port, space, flags, MACH_PORT_SPECIAL_DEFAULT);
3159 	return port;
3160 }
3161 
3162 /*
3163  *	Routine:	ipc_port_dealloc_special_and_unlock
3164  *	Purpose:
3165  *		Deallocate a port in a special space.
3166  *		Consumes one ref for the port.
3167  *	Conditions:
3168  *		Port is locked.
3169  */
3170 
3171 void
ipc_port_dealloc_special_and_unlock(ipc_port_t port,__assert_only ipc_space_t space)3172 ipc_port_dealloc_special_and_unlock(
3173 	ipc_port_t                      port,
3174 	__assert_only ipc_space_t       space)
3175 {
3176 	require_ip_active(port);
3177 //	assert(port->ip_receiver_name != MACH_PORT_NULL);
3178 	assert(ip_in_space(port, space));
3179 
3180 	/*
3181 	 *	We clear ip_receiver_name and ip_receiver to simplify
3182 	 *	the ipc_space_kernel check in ipc_mqueue_send.
3183 	 */
3184 
3185 	/* port transtions to IN-LIMBO state */
3186 	port->ip_receiver_name = MACH_PORT_NULL;
3187 	port->ip_receiver = IS_NULL;
3188 
3189 	/* relevant part of ipc_port_clear_receiver */
3190 	port->ip_mscount = 0;
3191 	port->ip_messages.imq_seqno = 0;
3192 
3193 	ipc_port_destroy(port);
3194 }
3195 
3196 /*
3197  *	Routine:	ipc_port_dealloc_special
3198  *	Purpose:
3199  *		Deallocate a port in a special space.
3200  *		Consumes one ref for the port.
3201  *	Conditions:
3202  *		Nothing locked.
3203  */
3204 
3205 void
ipc_port_dealloc_special(ipc_port_t port,ipc_space_t space)3206 ipc_port_dealloc_special(
3207 	ipc_port_t        port,
3208 	ipc_space_t       space)
3209 {
3210 	ip_mq_lock(port);
3211 	ipc_port_dealloc_special_and_unlock(port, space);
3212 }
3213 
3214 /*
3215  *	Routine:	ipc_port_finalize
3216  *	Purpose:
3217  *		Called on last reference deallocate to
3218  *		free any remaining data associated with the
3219  *		port.
3220  *	Conditions:
3221  *		Nothing locked.
3222  */
3223 void
ipc_port_finalize(ipc_port_t port)3224 ipc_port_finalize(
3225 	ipc_port_t              port)
3226 {
3227 	ipc_port_request_table_t requests = port->ip_requests;
3228 
3229 	assert(port_send_turnstile(port) == TURNSTILE_NULL);
3230 
3231 	if (waitq_type(&port->ip_waitq) == WQT_PORT) {
3232 		assert(ipc_port_rcv_turnstile(port) == TURNSTILE_NULL);
3233 	}
3234 
3235 	if (ip_active(port)) {
3236 		panic("Trying to free an active port. port %p", port);
3237 	}
3238 
3239 	if (requests) {
3240 		port->ip_requests = NULL;
3241 		ipc_port_request_table_free_noclear(requests);
3242 	}
3243 
3244 	/*
3245 	 * (81997111) now it is safe to deallocate the prealloc message.
3246 	 * Keep the IP_BIT_PREALLOC bit, it has to be sticky as the turnstile
3247 	 * code looks at it without holding locks.
3248 	 */
3249 	if (IP_PREALLOC(port)) {
3250 		ipc_kmsg_t kmsg = port->ip_premsg;
3251 
3252 		if (kmsg == IKM_NULL || ikm_prealloc_inuse_port(kmsg)) {
3253 			panic("port(%p, %p): prealloc message in an invalid state",
3254 			    port, kmsg);
3255 		}
3256 
3257 		port->ip_premsg = IKM_NULL;
3258 		ipc_kmsg_free(kmsg);
3259 	}
3260 
3261 	waitq_deinit(&port->ip_waitq);
3262 #if MACH_ASSERT
3263 	if (port->ip_made_bt) {
3264 		btref_put(port->ip_made_bt);
3265 	}
3266 #endif
3267 }
3268 
3269 /*
3270  *	Routine:	kdp_mqueue_send_find_owner
3271  *	Purpose:
3272  *		Discover the owner of the ipc object that contains the input
3273  *		waitq object. The thread blocked on the waitq should be
3274  *		waiting for an IPC_MQUEUE_FULL event.
3275  *	Conditions:
3276  *		The 'waitinfo->wait_type' value should already be set to
3277  *		kThreadWaitPortSend.
3278  *	Note:
3279  *		If we find out that the containing port is actually in
3280  *		transit, we reset the wait_type field to reflect this.
3281  */
3282 void
kdp_mqueue_send_find_owner(struct waitq * waitq,__assert_only event64_t event,thread_waitinfo_v2_t * waitinfo,struct ipc_service_port_label ** isplp)3283 kdp_mqueue_send_find_owner(
3284 	struct waitq                   *waitq,
3285 	__assert_only event64_t         event,
3286 	thread_waitinfo_v2_t           *waitinfo,
3287 	struct ipc_service_port_label **isplp)
3288 {
3289 	struct turnstile *turnstile;
3290 	assert(waitinfo->wait_type == kThreadWaitPortSend);
3291 	assert(event == IPC_MQUEUE_FULL);
3292 	assert(waitq_type(waitq) == WQT_TURNSTILE);
3293 
3294 	turnstile = waitq_to_turnstile(waitq);
3295 	ipc_port_t port = (ipc_port_t)turnstile->ts_proprietor; /* we are blocking on send */
3296 
3297 	zone_id_require(ZONE_ID_IPC_PORT, sizeof(struct ipc_port), port);
3298 
3299 	waitinfo->owner = 0;
3300 	waitinfo->context  = VM_KERNEL_UNSLIDE_OR_PERM(port);
3301 	if (ip_mq_lock_held_kdp(port)) {
3302 		/*
3303 		 * someone has the port locked: it may be in an
3304 		 * inconsistent state: bail
3305 		 */
3306 		waitinfo->owner = STACKSHOT_WAITOWNER_PORT_LOCKED;
3307 		return;
3308 	}
3309 
3310 	/* now we are the only one accessing the port */
3311 	if (ip_active(port)) {
3312 		/*
3313 		 * In kdp context, port must be left unlocked throughout.
3314 		 * Therefore can't use union field accessor helpers, manually strip PAC
3315 		 * and compare raw pointer.
3316 		 */
3317 		void *raw_ptr = ip_get_receiver_ptr_noauth(port);
3318 
3319 		if (port->ip_tempowner) {
3320 			ipc_importance_task_t imp_task = ip_get_imp_task(port);
3321 			if (imp_task != IIT_NULL && imp_task->iit_task != NULL) {
3322 				/* port is held by a tempowner */
3323 				waitinfo->owner = pid_from_task(port->ip_imp_task->iit_task);
3324 			} else {
3325 				waitinfo->owner = STACKSHOT_WAITOWNER_INTRANSIT;
3326 			}
3327 		} else if (ip_in_a_space(port)) { /* no port lock needed */
3328 			if ((ipc_space_t)raw_ptr == ipc_space_kernel) { /* access union field as ip_receiver */
3329 				/*
3330 				 * The kernel pid is 0, make this
3331 				 * distinguishable from no-owner and
3332 				 * inconsistent port state.
3333 				 */
3334 				waitinfo->owner = STACKSHOT_WAITOWNER_KERNEL;
3335 			} else {
3336 				waitinfo->owner = pid_from_task(((ipc_space_t)raw_ptr)->is_task);
3337 			}
3338 		} else if ((ipc_port_t)raw_ptr != IP_NULL) { /* access union field as ip_destination */
3339 			waitinfo->wait_type = kThreadWaitPortSendInTransit;
3340 			waitinfo->owner     = VM_KERNEL_UNSLIDE_OR_PERM((ipc_port_t)raw_ptr);
3341 		}
3342 		if (port->ip_service_port && port->ip_splabel != NULL) {
3343 			*isplp = (struct ipc_service_port_label *)port->ip_splabel;
3344 		}
3345 	}
3346 }
3347 
3348 /*
3349  *	Routine:	kdp_mqueue_recv_find_owner
3350  *	Purpose:
3351  *		Discover the "owner" of the ipc object that contains the input
3352  *		waitq object. The thread blocked on the waitq is trying to
3353  *		receive on the mqueue.
3354  *	Conditions:
3355  *		The 'waitinfo->wait_type' value should already be set to
3356  *		kThreadWaitPortReceive.
3357  *	Note:
3358  *		If we find that we are actualy waiting on a port set, we reset
3359  *		the wait_type field to reflect this.
3360  */
3361 void
kdp_mqueue_recv_find_owner(struct waitq * waitq,__assert_only event64_t event,thread_waitinfo_v2_t * waitinfo,struct ipc_service_port_label ** isplp)3362 kdp_mqueue_recv_find_owner(
3363 	struct waitq                   *waitq,
3364 	__assert_only event64_t         event,
3365 	thread_waitinfo_v2_t           *waitinfo,
3366 	struct ipc_service_port_label **isplp)
3367 {
3368 	assert(waitinfo->wait_type == kThreadWaitPortReceive);
3369 	assert(event == IPC_MQUEUE_RECEIVE);
3370 
3371 	waitinfo->owner = 0;
3372 
3373 	if (waitq_type(waitq) == WQT_PORT_SET) {
3374 		ipc_pset_t set = ips_from_waitq(waitq);
3375 
3376 		zone_id_require(ZONE_ID_IPC_PORT_SET, sizeof(struct ipc_pset), set);
3377 
3378 		/* Reset wait type to specify waiting on port set receive */
3379 		waitinfo->wait_type = kThreadWaitPortSetReceive;
3380 		waitinfo->context   = VM_KERNEL_UNSLIDE_OR_PERM(set);
3381 		if (ips_mq_lock_held_kdp(set)) {
3382 			waitinfo->owner = STACKSHOT_WAITOWNER_PSET_LOCKED;
3383 		}
3384 		/* There is no specific owner "at the other end" of a port set, so leave unset. */
3385 	} else if (waitq_type(waitq) == WQT_PORT) {
3386 		ipc_port_t port = ip_from_waitq(waitq);
3387 
3388 		zone_id_require(ZONE_ID_IPC_PORT, sizeof(struct ipc_port), port);
3389 
3390 		waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(port);
3391 		if (ip_mq_lock_held_kdp(port)) {
3392 			waitinfo->owner = STACKSHOT_WAITOWNER_PORT_LOCKED;
3393 			return;
3394 		}
3395 
3396 		if (ip_active(port)) {
3397 			if (ip_in_a_space(port)) { /* no port lock needed */
3398 				waitinfo->owner = ip_get_receiver_name(port);
3399 			} else {
3400 				waitinfo->owner = STACKSHOT_WAITOWNER_INTRANSIT;
3401 			}
3402 			if (port->ip_specialreply) {
3403 				waitinfo->wait_flags |= STACKSHOT_WAITINFO_FLAGS_SPECIALREPLY;
3404 			}
3405 			if (port->ip_splabel != NULL) {
3406 				*isplp = (struct ipc_service_port_label *)port->ip_splabel;
3407 			}
3408 		}
3409 	}
3410 }
3411 
3412 void
ipc_port_set_label(ipc_port_t port,ipc_label_t label)3413 ipc_port_set_label(
3414 	ipc_port_t              port,
3415 	ipc_label_t             label)
3416 {
3417 	ipc_kobject_label_t labelp;
3418 
3419 	assert(!ip_is_kolabeled(port));
3420 
3421 	labelp = zalloc_flags(ipc_kobject_label_zone, Z_WAITOK | Z_ZERO | Z_NOFAIL);
3422 	labelp->ikol_label = label;
3423 
3424 	port->ip_kolabel = labelp;
3425 	io_bits_or(ip_to_object(port), IO_BITS_KOLABEL);
3426 }
3427 
3428 kern_return_t
ipc_port_reset_thread_attr(ipc_port_t port)3429 ipc_port_reset_thread_attr(
3430 	ipc_port_t port)
3431 {
3432 	uint8_t iotier = THROTTLE_LEVEL_END;
3433 	uint8_t qos = THREAD_QOS_UNSPECIFIED;
3434 
3435 	return ipc_port_update_qos_n_iotier(port, qos, iotier);
3436 }
3437 
3438 kern_return_t
ipc_port_propagate_thread_attr(ipc_port_t port,struct thread_attr_for_ipc_propagation attr)3439 ipc_port_propagate_thread_attr(
3440 	ipc_port_t port,
3441 	struct thread_attr_for_ipc_propagation attr)
3442 {
3443 	uint8_t iotier = attr.tafip_iotier;
3444 	uint8_t qos = attr.tafip_qos;
3445 
3446 	return ipc_port_update_qos_n_iotier(port, qos, iotier);
3447 }
3448 
3449 static kern_return_t
ipc_port_update_qos_n_iotier(ipc_port_t port,uint8_t qos,uint8_t iotier)3450 ipc_port_update_qos_n_iotier(
3451 	ipc_port_t port,
3452 	uint8_t    qos,
3453 	uint8_t    iotier)
3454 {
3455 	if (port == IPC_PORT_NULL) {
3456 		return KERN_INVALID_ARGUMENT;
3457 	}
3458 
3459 	ip_mq_lock(port);
3460 
3461 	if (!ip_active(port)) {
3462 		ip_mq_unlock(port);
3463 		return KERN_TERMINATED;
3464 	}
3465 
3466 	if (port->ip_specialreply) {
3467 		ip_mq_unlock(port);
3468 		return KERN_INVALID_ARGUMENT;
3469 	}
3470 
3471 	port->ip_kernel_iotier_override = iotier;
3472 	port->ip_kernel_qos_override = qos;
3473 
3474 	if (ip_in_a_space(port) &&
3475 	    is_active(ip_get_receiver(port)) &&
3476 	    ipc_port_has_klist(port)) {
3477 		KNOTE(&port->ip_klist, 0);
3478 	}
3479 
3480 	ip_mq_unlock(port);
3481 	return KERN_SUCCESS;
3482 }
3483 
3484 /* Returns true if a rigid reply port violation should be enforced (by killing the process) */
3485 static bool
__ip_rigid_reply_port_semantics_violation(ipc_port_t reply_port,int * reply_port_semantics_violation)3486 __ip_rigid_reply_port_semantics_violation(ipc_port_t reply_port, int *reply_port_semantics_violation)
3487 {
3488 	bool hardened_runtime = csproc_hardened_runtime(current_proc());
3489 
3490 	if (proc_is_simulated(current_proc())
3491 #if CONFIG_ROSETTA
3492 	    || task_is_translated(current_task())
3493 #endif
3494 	    ) {
3495 		return FALSE;
3496 	}
3497 
3498 	if (task_get_platform_binary(current_task())) {
3499 		return TRUE;
3500 	}
3501 	if (!ip_is_provisional_reply_port(reply_port)) {
3502 		/* record telemetry for when third party fails to use a provisional reply port */
3503 		*reply_port_semantics_violation = hardened_runtime ? RRP_HARDENED_RUNTIME_VIOLATOR : RRP_3P_VIOLATOR;
3504 	}
3505 	return FALSE;
3506 }
3507 
3508 bool
ip_violates_reply_port_semantics(ipc_port_t dest_port,ipc_port_t reply_port,int * reply_port_semantics_violation)3509 ip_violates_reply_port_semantics(ipc_port_t dest_port, ipc_port_t reply_port,
3510     int *reply_port_semantics_violation)
3511 {
3512 	if (ip_require_reply_port_semantics(dest_port)
3513 	    && !ip_is_reply_port(reply_port)
3514 	    && !ip_is_provisional_reply_port(reply_port)) {
3515 		*reply_port_semantics_violation = REPLY_PORT_SEMANTICS_VIOLATOR;
3516 		return TRUE;
3517 	}
3518 	return FALSE;
3519 }
3520 
3521 /* Rigid reply port semantics don't allow for provisional reply ports */
3522 bool
ip_violates_rigid_reply_port_semantics(ipc_port_t dest_port,ipc_port_t reply_port,int * violates_3p)3523 ip_violates_rigid_reply_port_semantics(ipc_port_t dest_port, ipc_port_t reply_port, int *violates_3p)
3524 {
3525 	return ip_require_rigid_reply_port_semantics(dest_port)
3526 	       && !ip_is_reply_port(reply_port)
3527 	       && __ip_rigid_reply_port_semantics_violation(reply_port, violates_3p);
3528 }
3529 
3530 #if MACH_ASSERT
3531 #include <kern/machine.h>
3532 
3533 unsigned long   port_count = 0;
3534 unsigned long   port_count_warning = 20000;
3535 unsigned long   port_timestamp = 0;
3536 
3537 void            db_port_stack_trace(
3538 	ipc_port_t      port);
3539 void            db_ref(
3540 	int             refs);
3541 int             db_port_walk(
3542 	unsigned int    verbose,
3543 	unsigned int    display,
3544 	unsigned int    ref_search,
3545 	unsigned int    ref_target);
3546 
3547 #ifdef MACH_BSD
3548 extern int proc_pid(struct proc*);
3549 #endif /* MACH_BSD */
3550 
3551 /*
3552  *	Initialize all of the debugging state in a port.
3553  *	Insert the port into a global list of all allocated ports.
3554  */
3555 void
ipc_port_init_debug(ipc_port_t port,void * fp)3556 ipc_port_init_debug(ipc_port_t port, void *fp)
3557 {
3558 	port->ip_timetrack = port_timestamp++;
3559 
3560 	if (ipc_portbt) {
3561 		port->ip_made_bt = btref_get(fp, 0);
3562 	}
3563 
3564 #ifdef MACH_BSD
3565 	task_t task = current_task_early();
3566 	if (task != TASK_NULL) {
3567 		struct proc *proc = get_bsdtask_info(task);
3568 		if (proc) {
3569 			port->ip_made_pid = proc_pid(proc);
3570 		}
3571 	}
3572 #endif /* MACH_BSD */
3573 }
3574 
3575 #endif  /* MACH_ASSERT */
3576