xref: /xnu-12377.81.4/osfmk/ipc/mach_msg.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58  * support for mandatory and extensible security protections.  This notice
59  * is included in support of clause 2.2 (b) of the Apple Public License,
60  * Version 2.0.
61  * Copyright (c) 2005 SPARTA, Inc.
62  */
63 /*
64  */
65 /*
66  *  File:   ipc/mach_msg.c
67  *  Author: Rich Draves
68  *  Date:   1989
69  *
70  *  Exported message traps.  See mach/message.h.
71  */
72 
73 #include <mach/mach_types.h>
74 #include <mach/kern_return.h>
75 #include <mach/port.h>
76 #include <mach/message.h>
77 #include <mach/mig_errors.h>
78 #include <mach/mach_traps.h>
79 
80 #include <kern/kern_types.h>
81 #include <kern/assert.h>
82 #include <kern/cpu_number.h>
83 #include <kern/ipc_kobject.h>
84 #include <kern/ipc_mig.h>
85 #include <kern/task.h>
86 #include <kern/thread.h>
87 #include <kern/sched_prim.h>
88 #include <kern/exception.h>
89 #include <kern/misc_protos.h>
90 #include <kern/processor.h>
91 #include <kern/syscall_subr.h>
92 #include <kern/policy_internal.h>
93 #include <kern/mach_filter.h>
94 
95 #include <vm/vm_map_xnu.h>
96 
97 #include <ipc/port.h>
98 #include <ipc/ipc_types.h>
99 #include <ipc/ipc_kmsg.h>
100 #include <ipc/ipc_mqueue.h>
101 #include <ipc/ipc_object.h>
102 #include <ipc/ipc_notify.h>
103 #include <ipc/ipc_policy.h>
104 #include <ipc/ipc_port.h>
105 #include <ipc/ipc_pset.h>
106 #include <ipc/ipc_space.h>
107 #include <ipc/ipc_entry.h>
108 #include <ipc/ipc_importance.h>
109 #include <ipc/ipc_voucher.h>
110 
111 #include <machine/machine_routines.h>
112 #include <security/mac_mach_internal.h>
113 
114 #include <sys/kdebug.h>
115 #include <sys/proc_ro.h>
116 
117 #ifndef offsetof
118 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
119 #endif /* offsetof */
120 
121 /*
122  * Forward declarations - kernel internal routines
123  */
124 
125 static mach_msg_return_t
126 mach_msg_rcv_link_special_reply_port(
127 	ipc_port_t special_reply_port,
128 	mach_port_name_t dest_name_port);
129 
130 void
131 mach_msg_receive_results_complete(ipc_object_t object);
132 
133 const security_token_t KERNEL_SECURITY_TOKEN = KERNEL_SECURITY_TOKEN_VALUE;
134 const audit_token_t KERNEL_AUDIT_TOKEN = KERNEL_AUDIT_TOKEN_VALUE;
135 
136 #define mach_copyout_field(kaddr, uaddr, type_t, field) \
137 	mach_copyout((kaddr), (uaddr) + offsetof(type_t, field), \
138 	    sizeof(((type_t *)0)->field))
139 
140 /*
141  *	Routine:	mach_msg_receive_too_large()
142  *	Purpose:
143  *		Helper for mach_msg_receive_results() to handle
144  *		the MACH_RCV_TOO_LARGE error when the MACH64_RCV_LARGE
145  *		option is set.
146  *	Returns:
147  *		MACH_RCV_INVALID_DATA or the passed in mr.
148  */
149 static mach_msg_return_t
mach_msg_receive_too_large(mach_msg_recv_bufs_t * recv_bufs,mach_msg_recv_result_t * msgr,mach_msg_option64_t options,mach_msg_return_t mr)150 mach_msg_receive_too_large(
151 	mach_msg_recv_bufs_t   *recv_bufs,
152 	mach_msg_recv_result_t *msgr,
153 	mach_msg_option64_t     options,
154 	mach_msg_return_t       mr)
155 {
156 	assert(mr == MACH_RCV_TOO_LARGE);
157 	assert(options & MACH_RCV_LARGE);
158 
159 	if (options & MACH64_RCV_LINEAR_VECTOR) {
160 		/*
161 		 * If MACH64_RCV_LINEAR_VECTOR is set, kevent is calling
162 		 * from filt_machportprocess() and the reporting of name
163 		 * and sizes happen via the knote in filt_machportprocess()
164 		 * rather than a receive operation, there's nothing for us
165 		 * to do here.
166 		 */
167 		return mr;
168 	}
169 
170 	/*
171 	 * For the regular case, just copyout the size
172 	 * (and optional port name) in a fake header.
173 	 */
174 	if ((options & MACH64_RCV_LARGE_IDENTITY) &&
175 	    recv_bufs->recv_msg_size >=
176 	    offsetof(mach_msg_user_header_t, msgh_voucher_port)) {
177 		/*
178 		 * If MACH64_RCV_LARGE_IDENTITY is set,
179 		 * we monkey patch the msgh_local_port field.
180 		 */
181 		if (mach_copyout_field(&msgr->msgr_recv_name,
182 		    recv_bufs->recv_msg_addr,
183 		    mach_msg_user_header_t, msgh_local_port)) {
184 			mr = MACH_RCV_INVALID_DATA;
185 		}
186 	}
187 
188 	if (recv_bufs->recv_msg_size >=
189 	    offsetof(mach_msg_user_header_t, msgh_remote_port)) {
190 		/*
191 		 * For all cases, we monkey patch the msgh_size
192 		 * field with how much size is needed.
193 		 */
194 		if (mach_copyout_field(&msgr->msgr_msg_size,
195 		    recv_bufs->recv_msg_addr,
196 		    mach_msg_user_header_t, msgh_size)) {
197 			mr = MACH_RCV_INVALID_DATA;
198 		}
199 	}
200 
201 	if (recv_bufs->recv_aux_addr) {
202 		/*
203 		 * Then we report the incoming aux size as well,
204 		 * if the caller has an aux buffer.
205 		 */
206 		assert(recv_bufs->recv_aux_size >= sizeof(mach_msg_aux_header_t));
207 		if (mach_copyout_field(&msgr->msgr_aux_size,
208 		    recv_bufs->recv_aux_addr,
209 		    mach_msg_aux_header_t, msgdh_size)) {
210 			mr = MACH_RCV_INVALID_DATA;
211 		}
212 	}
213 
214 	return mr;
215 }
216 
217 /*
218  *  Routine:    mach_msg_receive_error   [internal]
219  *  Purpose:
220  *      Builds a minimal header/trailer and copies it to
221  *      the user message buffer.  Invoked when in the case of a
222  *      MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error.
223  *  Conditions:
224  *      Nothing locked. kmsg is freed upon return.
225  *  Returns:
226  *      the incoming "mr"       minimal header/trailer copied
227  *      MACH_RCV_INVALID_DATA   copyout to user buffer failed
228  */
229 static mach_msg_return_t
mach_msg_receive_error(ipc_kmsg_t kmsg,mach_msg_recv_bufs_t * recv_bufs,mach_msg_recv_result_t * msgr,mach_msg_option64_t options,ipc_space_t space,vm_map_t map,mach_msg_return_t mr)230 mach_msg_receive_error(
231 	ipc_kmsg_t              kmsg,
232 	mach_msg_recv_bufs_t   *recv_bufs,
233 	mach_msg_recv_result_t *msgr,
234 	mach_msg_option64_t     options,
235 	ipc_space_t             space,
236 	vm_map_t                map,
237 	mach_msg_return_t       mr)
238 {
239 	/*
240 	 * Copy out the destination port in the message.
241 	 *
242 	 * Destroy all other rights and memory in the message,
243 	 * and turn it into a very simple bare message with just a header.
244 	 */
245 	ipc_kmsg_copyout_dest_to_user(kmsg, space);
246 
247 	/*
248 	 * Copy the message to user space and return the size
249 	 * (note that ipc_kmsg_put_to_user may also adjust the actual
250 	 * msg and aux size copied out to user-space).
251 	 */
252 	return ipc_kmsg_put_to_user(kmsg, recv_bufs, msgr, options, map, mr);
253 }
254 
255 /*
256  *  Routine:    mach_msg_receive_results
257  *  Purpose:
258  *      Receive a message.
259  *  Conditions:
260  *      Nothing locked.
261  *
262  *  Returns:
263  *          sizep (out): copied out size of message proper
264  *          aux_sizep (out): copied out size of aux data
265  *      MACH_MSG_SUCCESS    Received a message.
266  *      MACH_RCV_INVALID_NAME   The name doesn't denote a right,
267  *          or the denoted right is not receive or port set.
268  *      MACH_RCV_IN_SET     Receive right is a member of a set.
269  *      MACH_RCV_TOO_LARGE  Message wouldn't fit into buffer.
270  *      MACH_RCV_TIMED_OUT  Timeout expired without a message.
271  *      MACH_RCV_INTERRUPTED    Reception interrupted.
272  *      MACH_RCV_PORT_DIED  Port/set died while receiving.
273  *      MACH_RCV_PORT_CHANGED   Port moved into set while receiving.
274  *      MACH_RCV_INVALID_DATA   Couldn't copy to user buffer.
275  *      MACH_RCV_INVALID_NOTIFY Bad notify port.
276  *      MACH_RCV_HEADER_ERROR
277  */
278 mach_msg_return_t
mach_msg_receive_results(mach_msg_recv_result_t * msgr_out)279 mach_msg_receive_results(
280 	mach_msg_recv_result_t *msgr_out)
281 {
282 	thread_t                self    = current_thread();
283 	ipc_space_t             space   = current_space();
284 	vm_map_t                map     = current_map();
285 
286 	/*
287 	 * /!\IMPORTANT/!\: Pull out values we stashed on thread struct now.
288 	 * Values may be stomped over if copyio operations in this function
289 	 * trigger kernel IPC calls.
290 	 */
291 	ipc_object_t            object  = self->ith_object;
292 	mach_msg_return_t       mr      = self->ith_state;
293 	mach_msg_option64_t     options = self->ith_option;
294 	ipc_kmsg_t              kmsg    = self->ith_kmsg;
295 
296 	mach_msg_recv_bufs_t recv_bufs = self->ith_recv_bufs;
297 	mach_msg_recv_result_t msgr = {
298 		.msgr_seqno     = self->ith_seqno,
299 		.msgr_context   = 0,
300 	};
301 
302 	/*
303 	 * unlink the special_reply_port before releasing reference to object.
304 	 * get the thread's turnstile, if the thread donated it's turnstile to the port
305 	 */
306 	mach_msg_receive_results_complete(object);
307 	io_release(object);
308 
309 	if (options & MACH64_RCV_LINEAR_VECTOR) {
310 		assert(recv_bufs.recv_aux_addr == 0);
311 		assert(recv_bufs.recv_aux_size == 0);
312 	}
313 
314 	if (mr == MACH_RCV_TOO_LARGE) {
315 		/* these ith_* fields are only set for MACH_RCV_TOO_LARGE */
316 		msgr.msgr_msg_size  = self->ith_msize;
317 		msgr.msgr_aux_size  = self->ith_asize;
318 		msgr.msgr_recv_name = self->ith_receiver_name;
319 
320 		/*
321 		 * If the receive operation occurs with MACH_RCV_LARGE set
322 		 * then no message was extracted from the queue, and the size
323 		 * and (optionally) receiver names were the only thing captured.
324 		 */
325 		if (options & MACH64_RCV_LARGE) {
326 			mr = mach_msg_receive_too_large(&recv_bufs, &msgr,
327 			    options, mr);
328 		} else {
329 			/* discard importance in message */
330 			ipc_importance_clean(kmsg);
331 			mr = mach_msg_receive_error(kmsg, &recv_bufs, &msgr,
332 			    options, space, map, mr);
333 			/* kmsg freed */
334 		}
335 	}
336 	if (mr != MACH_MSG_SUCCESS) {
337 		goto out;
338 	}
339 
340 #if IMPORTANCE_INHERITANCE
341 
342 	/* adopt/transform any importance attributes carried in the message */
343 	ipc_importance_receive(kmsg, options);
344 
345 #endif  /* IMPORTANCE_INHERITANCE */
346 
347 	/* auto redeem the voucher in the message */
348 	ipc_voucher_receive_postprocessing(kmsg, options);
349 
350 	/* Save destination port context for the trailer before copyout */
351 	msgr.msgr_context = ikm_header(kmsg)->msgh_remote_port->ip_context;
352 
353 	/*
354 	 * restore the recv_bufs values that are used by
355 	 * ipc_kmsg_copyout_guarded_port_descriptor()
356 	 */
357 	self->ith_recv_bufs = recv_bufs;
358 	mr = ipc_kmsg_copyout(kmsg, space, map, options);
359 
360 	if (mr != MACH_MSG_SUCCESS) {
361 		/* already received importance, so have to undo that here */
362 		ipc_importance_unreceive(kmsg, options);
363 
364 		/* if we had a body error copyout what we have, otherwise a simple header/trailer */
365 		if ((mr & ~MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
366 			mr = ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr,
367 			    options, map, mr);
368 		} else {
369 			mr = mach_msg_receive_error(kmsg, &recv_bufs, &msgr,
370 			    options, space, map, mr);
371 		}
372 	} else {
373 		msgr.msgr_priority = kmsg->ikm_ppriority;
374 		msgr.msgr_qos_ovrd = kmsg->ikm_qos_override;
375 		mr = ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr,
376 		    options, map, mr);
377 	}
378 	/* kmsg freed */
379 
380 out:
381 	if (msgr_out) {
382 		*msgr_out = msgr;
383 	}
384 	return mr;
385 }
386 
387 void
mach_msg_receive_continue(void)388 mach_msg_receive_continue(void)
389 {
390 	mach_msg_return_t mr;
391 
392 	ipc_port_thread_group_unblocked();
393 
394 	mr = mach_msg_receive_results(NULL);
395 	thread_syscall_return(mr);
396 }
397 
398 /*
399  *  Routine:    mach_msg_validate_data_vectors
400  *  Purpose:
401  *      Perform validations on message and auxiliary data vectors
402  *      we have copied in.
403  */
404 static mach_msg_return_t
mach_msg_validate_data_vectors(mach_msg_vector_t * msg_vec,mach_msg_vector_t * aux_vec,mach_msg_size_t vec_count,__unused mach_msg_option64_t option64,bool sending)405 mach_msg_validate_data_vectors(
406 	mach_msg_vector_t       *msg_vec,
407 	mach_msg_vector_t       *aux_vec,
408 	mach_msg_size_t         vec_count,
409 	__unused mach_msg_option64_t     option64,
410 	bool                    sending)
411 {
412 	mach_msg_size_t msg_size = 0, aux_size = 0; /* user size */
413 
414 	assert(vec_count <= MACH_MSGV_MAX_COUNT);
415 	assert(option64 & MACH64_MSG_VECTOR);
416 
417 	assert(msg_vec != NULL);
418 	assert(aux_vec != NULL);
419 
420 	if (vec_count == 0) {
421 		/*
422 		 * can't use MACH_RCV_TOO_LARGE or MACH_RCV_INVALID_DATA here because
423 		 * they imply a message has been dropped. use a new error code that
424 		 * suggests an early error and that message is still queued.
425 		 */
426 		return sending ? MACH_SEND_MSG_TOO_SMALL : MACH_RCV_INVALID_ARGUMENTS;
427 	}
428 
429 	/*
430 	 * Validate first (message proper) data vector.
431 	 *
432 	 * Since we are using mach_msg2_trap() to shim existing mach_msg() calls,
433 	 * we unfortunately cannot validate message rcv address or message rcv size
434 	 * at this point for compatibility reasons.
435 	 *
436 	 * (1) If rcv address is invalid, we will destroy the incoming message during
437 	 * ipc_kmsg_put_to_user(), instead of returning an error before receive
438 	 * is attempted.
439 	 * (2) If rcv size is smaller than the minimal message header and trailer
440 	 * that mach_msg_receive_error() builds, we will truncate the message
441 	 * and copy out a partial message.
442 	 *
443 	 * See: ipc_kmsg_put_vector_to_user().
444 	 */
445 	if (sending) {
446 		if (msg_vec->msgv_data == 0) {
447 			return MACH_SEND_INVALID_DATA;
448 		}
449 		msg_size = msg_vec->msgv_send_size;
450 		if ((msg_size < sizeof(mach_msg_user_header_t)) || (msg_size & 3)) {
451 			return MACH_SEND_MSG_TOO_SMALL;
452 		}
453 		if (msg_size > IPC_KMSG_MAX_BODY_SPACE) {
454 			return MACH_SEND_TOO_LARGE;
455 		}
456 	}
457 
458 	/* Validate second (optional auxiliary) data vector */
459 	if (vec_count == MACH_MSGV_MAX_COUNT) {
460 		if (sending) {
461 			aux_size = aux_vec->msgv_send_size;
462 			if (aux_size != 0 && aux_vec->msgv_data == 0) {
463 				return MACH_SEND_INVALID_DATA;
464 			}
465 			if (aux_size != 0 && aux_size < sizeof(mach_msg_aux_header_t)) {
466 				return MACH_SEND_AUX_TOO_SMALL;
467 			}
468 			if (aux_size > IPC_KMSG_MAX_AUX_DATA_SPACE) {
469 				return MACH_SEND_AUX_TOO_LARGE;
470 			}
471 		} else {
472 			mach_vm_address_t rcv_addr = aux_vec->msgv_rcv_addr ?
473 			    aux_vec->msgv_rcv_addr : aux_vec->msgv_data;
474 
475 			if (rcv_addr == 0) {
476 				return MACH_RCV_INVALID_ARGUMENTS;
477 			}
478 			/*
479 			 * We are using this aux vector to receive, kernel will at
480 			 * least copy out an empty aux data header.
481 			 *
482 			 * See: ipc_kmsg_put_vector_to_user()
483 			 */
484 			aux_size = aux_vec->msgv_rcv_size;
485 			if (aux_size < sizeof(mach_msg_aux_header_t)) {
486 				return MACH_RCV_INVALID_ARGUMENTS;
487 			}
488 		}
489 	} else {
490 		if (sending) {
491 			/*
492 			 * Not sending aux data vector, but we still might have copied it
493 			 * in if doing a combined send/receive. Nil out the send size.
494 			 */
495 			aux_vec->msgv_send_size = 0;
496 		} else {
497 			/* Do the same for receive */
498 			aux_vec->msgv_rcv_size = 0;
499 		}
500 	}
501 
502 	return MACH_MSG_SUCCESS;
503 }
504 
505 /*
506  *  Routine:    mach_msg_copyin_data_vectors
507  *  Purpose:
508  *      Copy in and message user data vectors.
509  */
510 static mach_msg_return_t
mach_msg_copyin_data_vectors(mach_vm_address_t data_addr,mach_msg_size_t cpin_count,mach_msg_option64_t option64,mach_msg_vector_t * msg_vecp,mach_msg_vector_t * aux_vecp)511 mach_msg_copyin_data_vectors(
512 	mach_vm_address_t   data_addr,/* user address */
513 	mach_msg_size_t     cpin_count,
514 	mach_msg_option64_t option64,
515 	mach_msg_vector_t   *msg_vecp,/* out */
516 	mach_msg_vector_t   *aux_vecp)/* out */
517 {
518 	mach_msg_vector_t data_vecs[MACH_MSGV_MAX_COUNT] = {};
519 
520 	static_assert(MACH_MSGV_MAX_COUNT == 2);
521 	assert(option64 & MACH64_MSG_VECTOR);
522 
523 	if (cpin_count > MACH_MSGV_MAX_COUNT) {
524 		return (option64 & MACH64_SEND_MSG) ?
525 		       MACH_SEND_INVALID_DATA : MACH_RCV_INVALID_ARGUMENTS;
526 	}
527 
528 	if (cpin_count == 0) {
529 		return (option64 & MACH64_SEND_MSG) ?
530 		       MACH_SEND_MSG_TOO_SMALL : MACH_RCV_INVALID_ARGUMENTS;
531 	}
532 
533 	if (mach_copyin(data_addr, data_vecs,
534 	    cpin_count * sizeof(mach_msg_vector_t))) {
535 		return (option64 & MACH64_SEND_MSG) ?
536 		       MACH_SEND_INVALID_DATA : MACH_RCV_INVALID_ARGUMENTS;
537 	}
538 
539 	memcpy(msg_vecp, &data_vecs[MACH_MSGV_IDX_MSG], sizeof(mach_msg_vector_t));
540 
541 	if (cpin_count == MACH_MSGV_MAX_COUNT) {
542 		memcpy(aux_vecp, &data_vecs[MACH_MSGV_IDX_AUX], sizeof(mach_msg_vector_t));
543 	}
544 
545 	return MACH_MSG_SUCCESS;
546 }
547 
548 #if IPC_HAS_LEGACY_MACH_MSG_TRAP
549 /*
550  *  Routine:    mach_msg_copyin_user_header
551  *  Purpose:
552  *      Copy in the message header, or up until message body if message is
553  *      large enough. Returns the header of the message and number of descriptors.
554  *      Used for mach_msg_overwrite_trap() only. Not available on embedded.
555  *  Returns:
556  *      MACH_MSG_SUCCESS - Copyin succeeded, msg_addr and msg_size are validated.
557  *      MACH_SEND_MSG_TOO_SMALL
558  *      MACH_SEND_TOO_LARGE
559  *      MACH_SEND_INVALID_DATA
560  */
561 static mach_msg_return_t
mach_msg_copyin_user_header(mach_msg_send_uctx_t * send_uctx,mach_msg_option64_t options)562 mach_msg_copyin_user_header(
563 	mach_msg_send_uctx_t   *send_uctx,
564 	mach_msg_option64_t     options)
565 {
566 	mach_msg_return_t       mr = MACH_MSG_SUCCESS;
567 
568 	if (send_uctx->send_msg_size < sizeof(mach_msg_user_header_t) ||
569 	    (send_uctx->send_msg_size & 3)) {
570 		return MACH_SEND_MSG_TOO_SMALL;
571 	}
572 
573 	if (send_uctx->send_msg_size > IPC_KMSG_MAX_BODY_SPACE) {
574 		return MACH_SEND_TOO_LARGE;
575 	}
576 
577 	if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) {
578 		static_assert(offsetof(mach_msg_send_uctx_t, send_dsc_count) ==
579 		    offsetof(mach_msg_user_base_t, body.msgh_descriptor_count));
580 
581 		mr = copyinmsg(send_uctx->send_msg_addr,
582 		    &send_uctx->send_header, sizeof(mach_msg_user_header_t));
583 	} else {
584 		mr = copyinmsg(send_uctx->send_msg_addr,
585 		    &send_uctx->send_header, sizeof(mach_msg_user_base_t));
586 	}
587 	if (mr != KERN_SUCCESS) {
588 		return MACH_SEND_INVALID_DATA;
589 	}
590 
591 	/*
592 	 * If the message claims to be complex, it must at least
593 	 * have the length of a "base" message (header + dsc_count).
594 	 */
595 	if (send_uctx->send_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
596 		if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) {
597 			return MACH_SEND_MSG_TOO_SMALL;
598 		}
599 	} else {
600 		send_uctx->send_dsc_count = 0;
601 	}
602 	send_uctx->send_header.msgh_size = send_uctx->send_msg_size;
603 
604 	return ipc_policy_allow_legacy_send_trap(send_uctx->send_header.msgh_id,
605 	           options);
606 }
607 #endif /* IPC_HAS_LEGACY_MACH_MSG_TRAP */
608 
609 
610 __attribute__((noinline, cold))
611 static mach_msg_return_t
mach_msg_receive_pseudo(ipc_kmsg_t kmsg,mach_msg_send_uctx_t * send_uctx,mach_msg_option64_t options,ipc_space_t space,vm_map_t map)612 mach_msg_receive_pseudo(
613 	ipc_kmsg_t              kmsg,
614 	mach_msg_send_uctx_t   *send_uctx,
615 	mach_msg_option64_t     options,
616 	ipc_space_t             space,
617 	vm_map_t                map)
618 {
619 	mach_msg_recv_bufs_t recv_bufs = {
620 		.recv_msg_addr  = send_uctx->send_msg_addr,
621 		.recv_aux_addr  = send_uctx->send_aux_addr,
622 		.recv_msg_size  = send_uctx->send_msg_size,
623 		.recv_aux_size  = send_uctx->send_aux_size,
624 	};
625 	mach_msg_recv_result_t msgr = {
626 		.msgr_recv_name = MSGR_PSEUDO_RECEIVE,
627 	};
628 	mach_msg_return_t mr;
629 
630 	/*
631 	 * set the recv_bufs values that are used by
632 	 * ipc_kmsg_copyout_guarded_port_descriptor()
633 	 */
634 	current_thread()->ith_recv_bufs = recv_bufs;
635 	mr = ipc_kmsg_copyout_pseudo(kmsg, space, map);
636 	(void)ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr, options, map, mr);
637 	return mr;
638 }
639 
640 /*
641  *  Routine:    mach_msg_trap_send [internal]
642  *  Purpose:
643  *      Send a message.
644  *  Conditions:
645  *      MACH_SEND_MSG is set. aux_send_size is bound checked.
646  *      send_aux_{addr, size} are 0 if not vector send.
647  *      send_msg_size needs additional bound checks.
648  *  Returns:
649  *      All of mach_msg_send error codes.
650  */
651 static mach_msg_return_t
mach_msg_trap_send(mach_msg_send_uctx_t * send_uctx,mach_msg_option64_t options,mach_msg_timeout_t msg_timeout,mach_msg_priority_t priority)652 mach_msg_trap_send(
653 	/* shared args between send and receive */
654 	mach_msg_send_uctx_t   *send_uctx,
655 	mach_msg_option64_t     options,
656 	mach_msg_timeout_t      msg_timeout,
657 	mach_msg_priority_t     priority)
658 {
659 	ipc_space_t       space = current_space();
660 	vm_map_t          map = current_map();
661 	ipc_kmsg_t        kmsg;
662 	mach_msg_return_t mr;
663 
664 	assert(options & MACH64_SEND_MSG);
665 
666 	/*
667 	 * Validate the send sizes and header carefuly.
668 	 */
669 	if (send_uctx->send_msg_size < sizeof(mach_msg_user_header_t) ||
670 	    (send_uctx->send_msg_size & 3)) {
671 		return MACH_SEND_MSG_TOO_SMALL;
672 	}
673 	if (send_uctx->send_msg_size > IPC_KMSG_MAX_BODY_SPACE) {
674 		return MACH_SEND_TOO_LARGE;
675 	}
676 	/*
677 	 * Complex message must have a body, also do a bound check on descriptor count
678 	 * (more in ikm_check_descriptors()).
679 	 */
680 	if (send_uctx->send_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
681 		if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) {
682 			return MACH_SEND_MSG_TOO_SMALL;
683 		}
684 		if (send_uctx->send_dsc_count >
685 		    (send_uctx->send_msg_size - sizeof(mach_msg_user_base_t)) /
686 		    sizeof(mach_msg_type_descriptor_t)) {
687 			return MACH_SEND_MSG_TOO_SMALL;
688 		}
689 	} else if (send_uctx->send_dsc_count != 0) {
690 		/*
691 		 * Simple message cannot contain descriptors.
692 		 *
693 		 * This invalid config can only happen from mach_msg2_trap()
694 		 * since desc_count is passed as its own trap argument.
695 		 */
696 		assert(options & MACH64_MACH_MSG2);
697 		return MACH_SEND_TOO_LARGE;
698 	}
699 
700 	/*
701 	 * Now that we have validated send_msg_size, send_aux_size and
702 	 * send_dsc_count, copy in the message.
703 	 */
704 	kmsg = ipc_kmsg_alloc(send_uctx->send_msg_size, send_uctx->send_aux_size,
705 	    send_uctx->send_dsc_count, IPC_KMSG_ALLOC_USER);
706 	if (kmsg == IKM_NULL) {
707 		return MACH_SEND_NO_BUFFER;
708 	}
709 
710 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
711 	    (uintptr_t)send_uctx->send_msg_addr, VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
712 	    0, 0, 0);
713 
714 	/*
715 	 * holding kmsg ref
716 	 * may add MACH64_SEND_ALWAYS to options
717 	 */
718 	mr = ipc_kmsg_copyin_from_user(kmsg, send_uctx, space, map, priority, &options);
719 	if (mr != MACH_MSG_SUCCESS) {
720 		ipc_kmsg_free(kmsg);
721 		return mr;
722 	}
723 
724 	mr = ipc_kmsg_send(kmsg, options, msg_timeout);
725 
726 	if (mr != MACH_MSG_SUCCESS) {
727 		mr |= mach_msg_receive_pseudo(kmsg, send_uctx, options, space, map);
728 		/* kmsg is freed */
729 	}
730 
731 	return mr;
732 }
733 
734 /*
735  *  Routine:    mach_msg_trap_receive [internal]
736  *  Purpose:
737  *      Receive a message.
738  *  Conditions:
739  *      MACH_RCV_MSG is set.
740  *      max_{msg, aux}_rcv_size are already validated.
741  *  Returns:
742  *      All of mach_msg_receive error codes.
743  */
744 static mach_msg_return_t
mach_msg_trap_receive(mach_vm_address_t msg_addr,mach_vm_address_t aux_addr,mach_msg_option64_t option64,mach_msg_timeout_t msg_timeout,mach_port_name_t sync_send,mach_msg_size_t max_msg_rcv_size,mach_msg_size_t max_aux_rcv_size,mach_port_name_t rcv_name)745 mach_msg_trap_receive(
746 	/* shared args between send and receive */
747 	mach_vm_address_t   msg_addr,
748 	mach_vm_address_t   aux_addr,        /* 0 if not vector send/rcv */
749 	mach_msg_option64_t option64,
750 	mach_msg_timeout_t  msg_timeout,
751 	mach_port_name_t    sync_send,
752 	/* msg receive args */
753 	mach_msg_size_t     max_msg_rcv_size,
754 	mach_msg_size_t     max_aux_rcv_size,        /* 0 if not vector send/rcv */
755 	mach_port_name_t    rcv_name)
756 {
757 	ipc_object_t object;
758 
759 	thread_t           self = current_thread();
760 	ipc_space_t        space = current_space();
761 	mach_msg_return_t  mr = MACH_MSG_SUCCESS;
762 
763 	assert(option64 & MACH64_RCV_MSG);
764 
765 	mr = ipc_mqueue_copyin(space, rcv_name, &object);
766 	if (mr != MACH_MSG_SUCCESS) {
767 		return mr;
768 	}
769 	/* hold ref for object */
770 
771 	if (sync_send != MACH_PORT_NULL) {
772 		ipc_port_t special_reply_port = ip_object_to_port(object);
773 		/* link the special reply port to the destination */
774 		mr = mach_msg_rcv_link_special_reply_port(special_reply_port, sync_send);
775 		if (mr != MACH_MSG_SUCCESS) {
776 			io_release(object);
777 			return mr;
778 		}
779 	}
780 
781 	/* Set up message proper receive params on thread */
782 	bzero(&self->ith_receive, sizeof(self->ith_receive));
783 	self->ith_recv_bufs = (mach_msg_recv_bufs_t){
784 		.recv_msg_addr = msg_addr,
785 		.recv_msg_size = max_msg_rcv_size,
786 		.recv_aux_addr = max_aux_rcv_size ? aux_addr : 0,
787 		.recv_aux_size = max_aux_rcv_size,
788 	};
789 	self->ith_object = object;
790 	self->ith_option = option64;
791 	self->ith_knote  = ITH_KNOTE_NULL; /* not part of ith_receive */
792 
793 	ipc_mqueue_receive(io_waitq(object), msg_timeout, THREAD_ABORTSAFE,
794 	    self, /* continuation ? */ true);
795 	/* NOTREACHED if thread started waiting */
796 
797 	if ((option64 & MACH_RCV_TIMEOUT) && msg_timeout == 0) {
798 		thread_poll_yield(self);
799 	}
800 
801 	mr = mach_msg_receive_results(NULL);
802 	/* release ref on ith_object */
803 
804 	return mr;
805 }
806 
807 /*
808  *  Routine:    mach_msg_overwrite_trap [mach trap]
809  *  Purpose:
810  *      Possibly send a message; possibly receive a message.
811  *
812  *		/!\ Deprecated /!\
813  *      No longer supported on embedded and will be removed from macOS.
814  *      Use mach_msg2_trap() instead.
815  *  Conditions:
816  *      Nothing locked.
817  *      The 'priority' is only a QoS if MACH_SEND_OVERRIDE is passed -
818  *      otherwise, it is a port name.
819  *  Returns:
820  *      All of mach_msg_send and mach_msg_receive error codes.
821  */
822 mach_msg_return_t
mach_msg_overwrite_trap(struct mach_msg_overwrite_trap_args * args)823 mach_msg_overwrite_trap(
824 	struct mach_msg_overwrite_trap_args *args)
825 {
826 #if IPC_HAS_LEGACY_MACH_MSG_TRAP
827 	mach_msg_option64_t     options = args->option;
828 	mach_msg_return_t       mr = MACH_MSG_SUCCESS;
829 
830 	options = ipc_current_msg_options(current_task(), options);
831 
832 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
833 
834 	if (options & MACH_SEND_MSG) {
835 		mach_msg_send_uctx_t send_uctx = {
836 			.send_msg_addr = args->msg,
837 			.send_msg_size = args->send_size,
838 		};
839 
840 		/*
841 		 * Perform a crude copyin of the mach_msg_user_header_t,
842 		 * including the next 4 bytes in case it is a complex message,
843 		 * in order to mimic mach_msg2_trap() behavior of synthesizing
844 		 * the mach_msg_user_base_t via the trap arguments.
845 		 *
846 		 * mach_msg_trap_send() will do more thorough validation of
847 		 * sizes and header.
848 		 */
849 
850 		mr = mach_msg_copyin_user_header(&send_uctx, options);
851 		if (mr != MACH_MSG_SUCCESS) {
852 			return mr;
853 		}
854 
855 		mr = mach_msg_trap_send(&send_uctx, options,
856 		    args->timeout, args->priority);
857 	}
858 
859 	/* If send failed, skip receive */
860 	if (mr != MACH_MSG_SUCCESS) {
861 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
862 		goto end;
863 	}
864 
865 	if (options & MACH_RCV_MSG) {
866 		mach_vm_address_t msg_addr = args->rcv_msg ?: args->msg;
867 		mach_port_name_t  sync_send = MACH_PORT_NULL;
868 
869 		/*
870 		 * Although just the presence of MACH_RCV_SYNC_WAIT and absence of
871 		 * MACH_SEND_OVERRIDE should imply that 'priority' is a valid port name
872 		 * to link, in practice older userspace is dependent on
873 		 * MACH_SEND_SYNC_OVERRIDE also excluding this path.
874 		 */
875 		if ((options & MACH_RCV_SYNC_WAIT) &&
876 		    !(options & (MACH_SEND_OVERRIDE | MACH_SEND_MSG)) &&
877 		    !(options & MACH_SEND_SYNC_OVERRIDE)) {
878 			sync_send = (mach_port_name_t)args->priority;
879 		}
880 
881 		mr = mach_msg_trap_receive(msg_addr, 0, options, args->timeout,
882 		    sync_send, args->rcv_size, 0, args->rcv_name);
883 	}
884 
885 end:
886 	/* unblock call is idempotent */
887 	ipc_port_thread_group_unblocked();
888 	return mr;
889 #else
890 	(void)args;
891 	return KERN_NOT_SUPPORTED;
892 #endif /* IPC_HAS_LEGACY_MACH_MSG_TRAP */
893 }
894 
895 /*
896  *  Routine:    mach_msg_trap [mach trap]
897  *  Purpose:
898  *      Possibly send a message; possibly receive a message.
899  *
900  *		/!\ Deprecated /!\
901  *      No longer supported on embedded and will be removed from macOS.
902  *      Use mach_msg2_trap() instead.
903  *  Conditions:
904  *      Nothing locked.
905  *  Returns:
906  *      All of mach_msg_send and mach_msg_receive error codes.
907  */
908 mach_msg_return_t
mach_msg_trap(struct mach_msg_overwrite_trap_args * args)909 mach_msg_trap(
910 	struct mach_msg_overwrite_trap_args *args)
911 {
912 	args->rcv_msg = (mach_vm_address_t)0;
913 
914 	return mach_msg_overwrite_trap(args);
915 }
916 
917 /*
918  *  Routine:    mach_msg2_trap [mach trap]
919  *  Purpose:
920  *      Modern mach_msg_trap() with vector message and CFI support.
921  *  Conditions:
922  *      Nothing locked.
923  *  Returns:
924  *      All of mach_msg_send and mach_msg_receive error codes.
925  */
926 mach_msg_return_t
mach_msg2_trap(struct mach_msg2_trap_args * args)927 mach_msg2_trap(
928 	struct mach_msg2_trap_args *args)
929 {
930 	/* packed arguments, LO_BITS_and_HI_BITS */
931 	uint64_t mb_ss = args->msgh_bits_and_send_size;
932 	uint64_t mr_lp = args->msgh_remote_and_local_port;
933 	uint64_t mv_id = args->msgh_voucher_and_id;
934 	uint64_t dc_rn = args->desc_count_and_rcv_name;
935 	uint64_t rs_pr = args->rcv_size_and_priority;
936 
937 	mach_msg_timeout_t  msg_timeout = (mach_msg_timeout_t)args->timeout;
938 	mach_msg_vector_t   msg_vec = {}, aux_vec = {};
939 	mach_msg_size_t     vec_snd_count = 0;
940 	mach_msg_size_t     vec_rcv_count = 0;
941 	mach_msg_option64_t option64;
942 	mach_msg_return_t   mr = MACH_MSG_SUCCESS;
943 
944 	option64 = ipc_current_msg_options(current_task(),
945 	    args->options) | MACH64_MACH_MSG2;
946 
947 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
948 
949 	mr = ipc_preflight_msg_option64(option64);
950 	if (mr != MACH_MSG_SUCCESS) {
951 		goto send_fail;
952 	}
953 
954 	if (option64 & MACH64_MSG_VECTOR) {
955 		vec_snd_count = (mach_msg_size_t)(mb_ss >> 32);
956 		vec_rcv_count = (mach_msg_size_t)rs_pr;
957 
958 		mr = mach_msg_copyin_data_vectors(args->data,
959 		    MAX(vec_snd_count, vec_rcv_count), option64, &msg_vec, &aux_vec);
960 		if (mr != MACH_MSG_SUCCESS) {
961 			goto send_fail;
962 		}
963 	}
964 
965 	if (option64 & MACH64_SEND_MSG) {
966 		mach_msg_send_uctx_t send_uctx = {
967 			.send_header = {
968 				.msgh_bits         = (mach_msg_bits_t) (mb_ss),
969 				.msgh_size         = 0,
970 				.msgh_remote_port  = (mach_port_name_t)(mr_lp),
971 				.msgh_local_port   = (mach_port_name_t)(mr_lp >> 32),
972 				.msgh_voucher_port = (mach_port_name_t)(mv_id),
973 				.msgh_id           = (mach_msg_id_t)   (mv_id >> 32),
974 			},
975 			.send_dsc_count = (mach_msg_size_t)dc_rn,
976 		};
977 		mach_msg_priority_t priority = (mach_msg_priority_t)(rs_pr >> 32);
978 
979 		if (option64 & MACH64_MSG_VECTOR) {
980 			/*
981 			 * only validate msg send related arguments.
982 			 *
983 			 * bad receive args do not stop us from sending
984 			 * during combined send/rcv.
985 			 */
986 			mr = mach_msg_validate_data_vectors(&msg_vec, &aux_vec,
987 			    vec_snd_count, option64, /* sending? */ TRUE);
988 			if (mr != MACH_MSG_SUCCESS) {
989 				goto send_fail;
990 			}
991 
992 			send_uctx.send_msg_addr = msg_vec.msgv_data;
993 			send_uctx.send_msg_size = msg_vec.msgv_send_size;
994 			send_uctx.send_aux_size = aux_vec.msgv_send_size;
995 			if (send_uctx.send_aux_size) {
996 				send_uctx.send_aux_addr = aux_vec.msgv_data;
997 			}
998 		} else {
999 			send_uctx.send_msg_addr = args->data;
1000 			send_uctx.send_msg_size = (mach_msg_size_t)(mb_ss >> 32);
1001 		}
1002 
1003 		mr = mach_msg_trap_send(&send_uctx, option64, msg_timeout, priority);
1004 	}
1005 
1006 send_fail:
1007 	/* if send failed, skip receive */
1008 	if (mr != MACH_MSG_SUCCESS) {
1009 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
1010 		goto end;
1011 	}
1012 
1013 	if (option64 & MACH64_RCV_MSG) {
1014 		mach_port_name_t    rcv_name   = (mach_port_name_t)(dc_rn >> 32);
1015 		mach_port_name_t    sync_send  = MACH_PORT_NULL;
1016 		mach_vm_address_t   msg_addr   = 0;
1017 		mach_msg_size_t     msg_size   = 0;
1018 		mach_vm_address_t   aux_addr   = 0;
1019 		mach_msg_size_t     aux_size   = 0;
1020 
1021 		if (option64 & MACH64_MSG_VECTOR) {
1022 			/* only validate msg receive related arguments */
1023 			mr = mach_msg_validate_data_vectors(&msg_vec, &aux_vec,
1024 			    vec_rcv_count, option64, /* sending? */ FALSE);
1025 			if (mr != MACH_MSG_SUCCESS) {
1026 				goto end;
1027 			}
1028 
1029 			msg_addr = msg_vec.msgv_rcv_addr ?: msg_vec.msgv_data;
1030 			msg_size = msg_vec.msgv_rcv_size;
1031 			aux_size = aux_vec.msgv_rcv_size;
1032 			if (aux_size) {
1033 				aux_addr = aux_vec.msgv_rcv_addr ?: aux_vec.msgv_data;
1034 			}
1035 		} else {
1036 			msg_addr = args->data;
1037 			msg_size = (mach_msg_size_t)rs_pr;
1038 		}
1039 
1040 		if (option64 & MACH64_RCV_SYNC_WAIT) {
1041 			/* use msgh_remote_port as sync send boosting port */
1042 			sync_send = (mach_port_name_t)mr_lp;
1043 		}
1044 
1045 		mr = mach_msg_trap_receive(msg_addr, aux_addr, option64,
1046 		    msg_timeout, sync_send, msg_size, aux_size, rcv_name);
1047 	}
1048 
1049 end:
1050 	/* unblock call is idempotent */
1051 	ipc_port_thread_group_unblocked();
1052 	return mr;
1053 }
1054 
1055 /*
1056  *  Routine:    mach_msg_rcv_link_special_reply_port
1057  *  Purpose:
1058  *      Link the special reply port(rcv right) to the
1059  *      other end of the sync ipc channel.
1060  *  Conditions:
1061  *      Nothing locked.
1062  *  Returns:
1063  *      None.
1064  */
1065 static mach_msg_return_t
mach_msg_rcv_link_special_reply_port(ipc_port_t special_reply_port,mach_port_name_t dest_name_port)1066 mach_msg_rcv_link_special_reply_port(
1067 	ipc_port_t special_reply_port,
1068 	mach_port_name_t dest_name_port)
1069 {
1070 	ipc_port_t dest_port = IP_NULL;
1071 	kern_return_t kr;
1072 
1073 	if (current_thread()->ith_special_reply_port != special_reply_port) {
1074 		return MACH_RCV_INVALID_NOTIFY;
1075 	}
1076 
1077 	/* Copyin the destination port */
1078 	if (!MACH_PORT_VALID(dest_name_port)) {
1079 		return MACH_RCV_INVALID_NOTIFY;
1080 	}
1081 
1082 	kr = ipc_port_translate_send(current_space(), dest_name_port, &dest_port);
1083 	if (kr == KERN_SUCCESS) {
1084 		ip_reference(dest_port);
1085 		ip_mq_unlock(dest_port);
1086 
1087 		/*
1088 		 * The receive right of dest port might have gone away,
1089 		 * do not fail the receive in that case.
1090 		 */
1091 		ipc_port_link_special_reply_port(special_reply_port,
1092 		    dest_port, FALSE);
1093 
1094 		ip_release(dest_port);
1095 	}
1096 	return MACH_MSG_SUCCESS;
1097 }
1098 
1099 /*
1100  *  Routine:    mach_msg_receive_results_complete
1101  *  Purpose:
1102  *      Get thread's turnstile back from the object and
1103  *              if object is a special reply port then reset its
1104  *      linkage.
1105  *  Condition:
1106  *      Nothing locked.
1107  *  Returns:
1108  *      None.
1109  */
1110 void
mach_msg_receive_results_complete(ipc_object_t object)1111 mach_msg_receive_results_complete(ipc_object_t object)
1112 {
1113 	thread_t self = current_thread();
1114 	ipc_port_t port = IP_NULL;
1115 	boolean_t get_turnstile = (self->turnstile == TURNSTILE_NULL);
1116 
1117 	if (io_is_pset(object)) {
1118 		assert(self->turnstile != TURNSTILE_NULL);
1119 		return;
1120 	}
1121 
1122 	uint8_t flags = IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE;
1123 
1124 	/*
1125 	 * Don't clear the ip_srp_msg_sent bit if...
1126 	 */
1127 	if (!((self->ith_state == MACH_RCV_TOO_LARGE && self->ith_option & MACH_RCV_LARGE) ||         //msg was too large and the next receive will get it
1128 	    self->ith_state == MACH_RCV_INTERRUPTED ||
1129 	    self->ith_state == MACH_RCV_TIMED_OUT ||
1130 	    self->ith_state == MACH_RCV_PORT_CHANGED)) {
1131 		flags |= IPC_PORT_ADJUST_SR_RECEIVED_MSG;
1132 	}
1133 
1134 	port = ip_object_to_port(object);
1135 	if (ip_is_special_reply_port(port) || get_turnstile) {
1136 		ip_mq_lock(port);
1137 		ipc_port_adjust_special_reply_port_locked(port, NULL,
1138 		    flags, get_turnstile);
1139 		/* port unlocked */
1140 	}
1141 	assert(self->turnstile != TURNSTILE_NULL);
1142 	/* thread now has a turnstile */
1143 }
1144 
1145 
1146 SECURITY_READ_ONLY_LATE(struct mach_msg_filter_callbacks) mach_msg_filter_callbacks;
1147 
1148 kern_return_t
mach_msg_filter_register_callback(const struct mach_msg_filter_callbacks * callbacks)1149 mach_msg_filter_register_callback(
1150 	const struct mach_msg_filter_callbacks *callbacks)
1151 {
1152 	size_t size = 0;
1153 
1154 	if (callbacks == NULL) {
1155 		return KERN_INVALID_ARGUMENT;
1156 	}
1157 	if (mach_msg_filter_callbacks.fetch_filter_policy != NULL) {
1158 		/* double init */
1159 		return KERN_FAILURE;
1160 	}
1161 
1162 	if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_0) {
1163 		/* check for missing v0 callbacks */
1164 		if (callbacks->fetch_filter_policy == NULL) {
1165 			return KERN_INVALID_ARGUMENT;
1166 		}
1167 		size = offsetof(struct mach_msg_filter_callbacks, alloc_service_port_sblabel);
1168 	}
1169 
1170 	if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_1) {
1171 		if (callbacks->alloc_service_port_sblabel == NULL ||
1172 		    callbacks->dealloc_service_port_sblabel == NULL ||
1173 		    callbacks->derive_sblabel_from_service_port == NULL ||
1174 		    callbacks->get_connection_port_filter_policy == NULL ||
1175 		    callbacks->retain_sblabel == NULL) {
1176 			return KERN_INVALID_ARGUMENT;
1177 		}
1178 		size = sizeof(struct mach_msg_filter_callbacks);
1179 	}
1180 
1181 	if (callbacks->version > MACH_MSG_FILTER_CALLBACKS_VERSION_1) {
1182 		/* invalid version */
1183 		return KERN_INVALID_ARGUMENT;
1184 	}
1185 
1186 	memcpy(&mach_msg_filter_callbacks, callbacks, size);
1187 	return KERN_SUCCESS;
1188 }
1189 
1190 /* This function should only be called if the task and port allow message filtering */
1191 boolean_t
mach_msg_fetch_filter_policy(void * port_label,mach_msg_id_t msgh_id,mach_msg_filter_id * fid)1192 mach_msg_fetch_filter_policy(
1193 	void *port_label,
1194 	mach_msg_id_t msgh_id,
1195 	mach_msg_filter_id *fid)
1196 {
1197 	boolean_t ret = TRUE;
1198 
1199 	if (mach_msg_fetch_filter_policy_callback == NULL) {
1200 		*fid = MACH_MSG_FILTER_POLICY_ALLOW;
1201 		return true;
1202 	}
1203 	ret = mach_msg_fetch_filter_policy_callback(current_task(), port_label, msgh_id, fid);
1204 
1205 	return ret;
1206 }
1207