xref: /xnu-11215/osfmk/ipc/mach_msg.c (revision 8d741a5d)
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 #if MACH_FLIPC
395 	if (current_thread()->ith_state == MACH_PEEK_READY) {
396 		thread_syscall_return(MACH_PEEK_READY);
397 		__builtin_unreachable();
398 	}
399 #endif /* MACH_FLIPC */
400 
401 	mr = mach_msg_receive_results(NULL);
402 	thread_syscall_return(mr);
403 }
404 
405 /*
406  *  Routine:    mach_msg_validate_data_vectors
407  *  Purpose:
408  *      Perform validations on message and auxiliary data vectors
409  *      we have copied in.
410  */
411 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)412 mach_msg_validate_data_vectors(
413 	mach_msg_vector_t       *msg_vec,
414 	mach_msg_vector_t       *aux_vec,
415 	mach_msg_size_t         vec_count,
416 	__unused mach_msg_option64_t     option64,
417 	bool                    sending)
418 {
419 	mach_msg_size_t msg_size = 0, aux_size = 0; /* user size */
420 
421 	assert(vec_count <= MACH_MSGV_MAX_COUNT);
422 	assert(option64 & MACH64_MSG_VECTOR);
423 
424 	assert(msg_vec != NULL);
425 	assert(aux_vec != NULL);
426 
427 	if (vec_count == 0) {
428 		/*
429 		 * can't use MACH_RCV_TOO_LARGE or MACH_RCV_INVALID_DATA here because
430 		 * they imply a message has been dropped. use a new error code that
431 		 * suggests an early error and that message is still queued.
432 		 */
433 		return sending ? MACH_SEND_MSG_TOO_SMALL : MACH_RCV_INVALID_ARGUMENTS;
434 	}
435 
436 	/*
437 	 * Validate first (message proper) data vector.
438 	 *
439 	 * Since we are using mach_msg2_trap() to shim existing mach_msg() calls,
440 	 * we unfortunately cannot validate message rcv address or message rcv size
441 	 * at this point for compatibility reasons.
442 	 *
443 	 * (1) If rcv address is invalid, we will destroy the incoming message during
444 	 * ipc_kmsg_put_to_user(), instead of returning an error before receive
445 	 * is attempted.
446 	 * (2) If rcv size is smaller than the minimal message header and trailer
447 	 * that mach_msg_receive_error() builds, we will truncate the message
448 	 * and copy out a partial message.
449 	 *
450 	 * See: ipc_kmsg_put_vector_to_user().
451 	 */
452 	if (sending) {
453 		if (msg_vec->msgv_data == 0) {
454 			return MACH_SEND_INVALID_DATA;
455 		}
456 		msg_size = msg_vec->msgv_send_size;
457 		if ((msg_size < sizeof(mach_msg_user_header_t)) || (msg_size & 3)) {
458 			return MACH_SEND_MSG_TOO_SMALL;
459 		}
460 		if (msg_size > IPC_KMSG_MAX_BODY_SPACE) {
461 			return MACH_SEND_TOO_LARGE;
462 		}
463 	}
464 
465 	/* Validate second (optional auxiliary) data vector */
466 	if (vec_count == MACH_MSGV_MAX_COUNT) {
467 		if (sending) {
468 			aux_size = aux_vec->msgv_send_size;
469 			if (aux_size != 0 && aux_vec->msgv_data == 0) {
470 				return MACH_SEND_INVALID_DATA;
471 			}
472 			if (aux_size != 0 && aux_size < sizeof(mach_msg_aux_header_t)) {
473 				return MACH_SEND_AUX_TOO_SMALL;
474 			}
475 			if (aux_size > IPC_KMSG_MAX_AUX_DATA_SPACE) {
476 				return MACH_SEND_AUX_TOO_LARGE;
477 			}
478 		} else {
479 			mach_vm_address_t rcv_addr = aux_vec->msgv_rcv_addr ?
480 			    aux_vec->msgv_rcv_addr : aux_vec->msgv_data;
481 
482 			if (rcv_addr == 0) {
483 				return MACH_RCV_INVALID_ARGUMENTS;
484 			}
485 			/*
486 			 * We are using this aux vector to receive, kernel will at
487 			 * least copy out an empty aux data header.
488 			 *
489 			 * See: ipc_kmsg_put_vector_to_user()
490 			 */
491 			aux_size = aux_vec->msgv_rcv_size;
492 			if (aux_size < sizeof(mach_msg_aux_header_t)) {
493 				return MACH_RCV_INVALID_ARGUMENTS;
494 			}
495 		}
496 	} else {
497 		if (sending) {
498 			/*
499 			 * Not sending aux data vector, but we still might have copied it
500 			 * in if doing a combined send/receive. Nil out the send size.
501 			 */
502 			aux_vec->msgv_send_size = 0;
503 		} else {
504 			/* Do the same for receive */
505 			aux_vec->msgv_rcv_size = 0;
506 		}
507 	}
508 
509 	return MACH_MSG_SUCCESS;
510 }
511 
512 /*
513  *  Routine:    mach_msg_copyin_data_vectors
514  *  Purpose:
515  *      Copy in and message user data vectors.
516  */
517 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)518 mach_msg_copyin_data_vectors(
519 	mach_vm_address_t   data_addr,/* user address */
520 	mach_msg_size_t     cpin_count,
521 	mach_msg_option64_t option64,
522 	mach_msg_vector_t   *msg_vecp,/* out */
523 	mach_msg_vector_t   *aux_vecp)/* out */
524 {
525 	mach_msg_vector_t data_vecs[MACH_MSGV_MAX_COUNT] = {};
526 
527 	static_assert(MACH_MSGV_MAX_COUNT == 2);
528 	assert(option64 & MACH64_MSG_VECTOR);
529 
530 	if (cpin_count > MACH_MSGV_MAX_COUNT) {
531 		return (option64 & MACH64_SEND_MSG) ?
532 		       MACH_SEND_INVALID_DATA : MACH_RCV_INVALID_ARGUMENTS;
533 	}
534 
535 	if (cpin_count == 0) {
536 		return (option64 & MACH64_SEND_MSG) ?
537 		       MACH_SEND_MSG_TOO_SMALL : MACH_RCV_INVALID_ARGUMENTS;
538 	}
539 
540 	if (mach_copyin(data_addr, data_vecs,
541 	    cpin_count * sizeof(mach_msg_vector_t))) {
542 		return (option64 & MACH64_SEND_MSG) ?
543 		       MACH_SEND_INVALID_DATA : MACH_RCV_INVALID_ARGUMENTS;
544 	}
545 
546 	memcpy(msg_vecp, &data_vecs[MACH_MSGV_IDX_MSG], sizeof(mach_msg_vector_t));
547 
548 	if (cpin_count == MACH_MSGV_MAX_COUNT) {
549 		memcpy(aux_vecp, &data_vecs[MACH_MSGV_IDX_AUX], sizeof(mach_msg_vector_t));
550 	}
551 
552 	return MACH_MSG_SUCCESS;
553 }
554 
555 #if IPC_HAS_LEGACY_MACH_MSG_TRAP
556 /*
557  *  Routine:    mach_msg_copyin_user_header
558  *  Purpose:
559  *      Copy in the message header, or up until message body if message is
560  *      large enough. Returns the header of the message and number of descriptors.
561  *      Used for mach_msg_overwrite_trap() only. Not available on embedded.
562  *  Returns:
563  *      MACH_MSG_SUCCESS - Copyin succeeded, msg_addr and msg_size are validated.
564  *      MACH_SEND_MSG_TOO_SMALL
565  *      MACH_SEND_TOO_LARGE
566  *      MACH_SEND_INVALID_DATA
567  */
568 static mach_msg_return_t
mach_msg_copyin_user_header(mach_msg_send_uctx_t * send_uctx,mach_msg_option64_t options)569 mach_msg_copyin_user_header(
570 	mach_msg_send_uctx_t   *send_uctx,
571 	mach_msg_option64_t     options)
572 {
573 	mach_msg_return_t       mr = MACH_MSG_SUCCESS;
574 
575 	if (send_uctx->send_msg_size < sizeof(mach_msg_user_header_t) ||
576 	    (send_uctx->send_msg_size & 3)) {
577 		return MACH_SEND_MSG_TOO_SMALL;
578 	}
579 
580 	if (send_uctx->send_msg_size > IPC_KMSG_MAX_BODY_SPACE) {
581 		return MACH_SEND_TOO_LARGE;
582 	}
583 
584 	if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) {
585 		static_assert(offsetof(mach_msg_send_uctx_t, send_dsc_count) ==
586 		    offsetof(mach_msg_user_base_t, body.msgh_descriptor_count));
587 
588 		mr = copyinmsg(send_uctx->send_msg_addr,
589 		    &send_uctx->send_header, sizeof(mach_msg_user_header_t));
590 	} else {
591 		mr = copyinmsg(send_uctx->send_msg_addr,
592 		    &send_uctx->send_header, sizeof(mach_msg_user_base_t));
593 	}
594 	if (mr != KERN_SUCCESS) {
595 		return MACH_SEND_INVALID_DATA;
596 	}
597 
598 	/*
599 	 * If the message claims to be complex, it must at least
600 	 * have the length of a "base" message (header + dsc_count).
601 	 */
602 	if (send_uctx->send_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
603 		if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) {
604 			return MACH_SEND_MSG_TOO_SMALL;
605 		}
606 	} else {
607 		send_uctx->send_dsc_count = 0;
608 	}
609 	send_uctx->send_header.msgh_size = send_uctx->send_msg_size;
610 
611 	return ipc_policy_allow_legacy_send_trap(send_uctx->send_header.msgh_id,
612 	           options);
613 }
614 #endif /* IPC_HAS_LEGACY_MACH_MSG_TRAP */
615 
616 
617 __attribute__((noinline, cold))
618 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)619 mach_msg_receive_pseudo(
620 	ipc_kmsg_t              kmsg,
621 	mach_msg_send_uctx_t   *send_uctx,
622 	mach_msg_option64_t     options,
623 	ipc_space_t             space,
624 	vm_map_t                map)
625 {
626 	mach_msg_recv_bufs_t recv_bufs = {
627 		.recv_msg_addr  = send_uctx->send_msg_addr,
628 		.recv_aux_addr  = send_uctx->send_aux_addr,
629 		.recv_msg_size  = send_uctx->send_msg_size,
630 		.recv_aux_size  = send_uctx->send_aux_size,
631 	};
632 	mach_msg_recv_result_t msgr = {
633 		.msgr_recv_name = MSGR_PSEUDO_RECEIVE,
634 	};
635 	mach_msg_return_t mr;
636 
637 	/*
638 	 * set the recv_bufs values that are used by
639 	 * ipc_kmsg_copyout_guarded_port_descriptor()
640 	 */
641 	current_thread()->ith_recv_bufs = recv_bufs;
642 	mr = ipc_kmsg_copyout_pseudo(kmsg, space, map);
643 	(void)ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr, options, map, mr);
644 	return mr;
645 }
646 
647 /*
648  *  Routine:    mach_msg_trap_send [internal]
649  *  Purpose:
650  *      Send a message.
651  *  Conditions:
652  *      MACH_SEND_MSG is set. aux_send_size is bound checked.
653  *      send_aux_{addr, size} are 0 if not vector send.
654  *      send_msg_size needs additional bound checks.
655  *  Returns:
656  *      All of mach_msg_send error codes.
657  */
658 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)659 mach_msg_trap_send(
660 	/* shared args between send and receive */
661 	mach_msg_send_uctx_t   *send_uctx,
662 	mach_msg_option64_t     options,
663 	mach_msg_timeout_t      msg_timeout,
664 	mach_msg_priority_t     priority)
665 {
666 	ipc_space_t       space = current_space();
667 	vm_map_t          map = current_map();
668 	ipc_kmsg_t        kmsg;
669 	mach_msg_return_t mr;
670 
671 	assert(options & MACH64_SEND_MSG);
672 
673 	/*
674 	 * Validate the send sizes and header carefuly.
675 	 */
676 	if (send_uctx->send_msg_size < sizeof(mach_msg_user_header_t) ||
677 	    (send_uctx->send_msg_size & 3)) {
678 		return MACH_SEND_MSG_TOO_SMALL;
679 	}
680 	if (send_uctx->send_msg_size > IPC_KMSG_MAX_BODY_SPACE) {
681 		return MACH_SEND_TOO_LARGE;
682 	}
683 	/*
684 	 * Complex message must have a body, also do a bound check on descriptor count
685 	 * (more in ikm_check_descriptors()).
686 	 */
687 	if (send_uctx->send_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
688 		if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) {
689 			return MACH_SEND_MSG_TOO_SMALL;
690 		}
691 		if (send_uctx->send_dsc_count >
692 		    (send_uctx->send_msg_size - sizeof(mach_msg_user_base_t)) /
693 		    sizeof(mach_msg_type_descriptor_t)) {
694 			return MACH_SEND_MSG_TOO_SMALL;
695 		}
696 	} else if (send_uctx->send_dsc_count != 0) {
697 		/*
698 		 * Simple message cannot contain descriptors.
699 		 *
700 		 * This invalid config can only happen from mach_msg2_trap()
701 		 * since desc_count is passed as its own trap argument.
702 		 */
703 		assert(options & MACH64_MACH_MSG2);
704 		return MACH_SEND_TOO_LARGE;
705 	}
706 
707 	/*
708 	 * Now that we have validated send_msg_size, send_aux_size and
709 	 * send_dsc_count, copy in the message.
710 	 */
711 	kmsg = ipc_kmsg_alloc(send_uctx->send_msg_size, send_uctx->send_aux_size,
712 	    send_uctx->send_dsc_count, IPC_KMSG_ALLOC_USER);
713 	if (kmsg == IKM_NULL) {
714 		return MACH_SEND_NO_BUFFER;
715 	}
716 
717 	KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
718 	    (uintptr_t)send_uctx->send_msg_addr, VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
719 	    0, 0, 0);
720 
721 	/*
722 	 * holding kmsg ref
723 	 * may add MACH64_SEND_ALWAYS to options
724 	 */
725 	mr = ipc_kmsg_copyin_from_user(kmsg, send_uctx, space, map, priority, &options);
726 	if (mr != MACH_MSG_SUCCESS) {
727 		ipc_kmsg_free(kmsg);
728 		return mr;
729 	}
730 
731 	mr = ipc_kmsg_send(kmsg, options, msg_timeout);
732 
733 	if (mr != MACH_MSG_SUCCESS) {
734 		mr |= mach_msg_receive_pseudo(kmsg, send_uctx, options, space, map);
735 		/* kmsg is freed */
736 	}
737 
738 	return mr;
739 }
740 
741 /*
742  *  Routine:    mach_msg_trap_receive [internal]
743  *  Purpose:
744  *      Receive a message.
745  *  Conditions:
746  *      MACH_RCV_MSG is set.
747  *      max_{msg, aux}_rcv_size are already validated.
748  *  Returns:
749  *      All of mach_msg_receive error codes.
750  */
751 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)752 mach_msg_trap_receive(
753 	/* shared args between send and receive */
754 	mach_vm_address_t   msg_addr,
755 	mach_vm_address_t   aux_addr,        /* 0 if not vector send/rcv */
756 	mach_msg_option64_t option64,
757 	mach_msg_timeout_t  msg_timeout,
758 	mach_port_name_t    sync_send,
759 	/* msg receive args */
760 	mach_msg_size_t     max_msg_rcv_size,
761 	mach_msg_size_t     max_aux_rcv_size,        /* 0 if not vector send/rcv */
762 	mach_port_name_t    rcv_name)
763 {
764 	ipc_object_t object;
765 
766 	thread_t           self = current_thread();
767 	ipc_space_t        space = current_space();
768 	mach_msg_return_t  mr = MACH_MSG_SUCCESS;
769 
770 	assert(option64 & MACH64_RCV_MSG);
771 
772 	mr = ipc_mqueue_copyin(space, rcv_name, &object);
773 	if (mr != MACH_MSG_SUCCESS) {
774 		return mr;
775 	}
776 	/* hold ref for object */
777 
778 	if (sync_send != MACH_PORT_NULL) {
779 		ipc_port_t special_reply_port = ip_object_to_port(object);
780 		/* link the special reply port to the destination */
781 		mr = mach_msg_rcv_link_special_reply_port(special_reply_port, sync_send);
782 		if (mr != MACH_MSG_SUCCESS) {
783 			io_release(object);
784 			return mr;
785 		}
786 	}
787 
788 	/* Set up message proper receive params on thread */
789 	bzero(&self->ith_receive, sizeof(self->ith_receive));
790 	self->ith_recv_bufs = (mach_msg_recv_bufs_t){
791 		.recv_msg_addr = msg_addr,
792 		.recv_msg_size = max_msg_rcv_size,
793 		.recv_aux_addr = max_aux_rcv_size ? aux_addr : 0,
794 		.recv_aux_size = max_aux_rcv_size,
795 	};
796 	self->ith_object = object;
797 	self->ith_option = option64;
798 	self->ith_knote  = ITH_KNOTE_NULL; /* not part of ith_receive */
799 
800 	ipc_mqueue_receive(io_waitq(object), msg_timeout, THREAD_ABORTSAFE,
801 	    self, /* continuation ? */ true);
802 	/* NOTREACHED if thread started waiting */
803 
804 	if ((option64 & MACH_RCV_TIMEOUT) && msg_timeout == 0) {
805 		thread_poll_yield(self);
806 	}
807 
808 	mr = mach_msg_receive_results(NULL);
809 	/* release ref on ith_object */
810 
811 	return mr;
812 }
813 
814 /*
815  *  Routine:    mach_msg_overwrite_trap [mach trap]
816  *  Purpose:
817  *      Possibly send a message; possibly receive a message.
818  *
819  *		/!\ Deprecated /!\
820  *      No longer supported on embedded and will be removed from macOS.
821  *      Use mach_msg2_trap() instead.
822  *  Conditions:
823  *      Nothing locked.
824  *      The 'priority' is only a QoS if MACH_SEND_OVERRIDE is passed -
825  *      otherwise, it is a port name.
826  *  Returns:
827  *      All of mach_msg_send and mach_msg_receive error codes.
828  */
829 mach_msg_return_t
mach_msg_overwrite_trap(struct mach_msg_overwrite_trap_args * args)830 mach_msg_overwrite_trap(
831 	struct mach_msg_overwrite_trap_args *args)
832 {
833 #if IPC_HAS_LEGACY_MACH_MSG_TRAP
834 	mach_msg_option64_t     options = args->option;
835 	mach_msg_return_t       mr = MACH_MSG_SUCCESS;
836 
837 	options = ipc_current_user_policy(current_task(), options);
838 
839 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
840 
841 	if (options & MACH_SEND_MSG) {
842 		mach_msg_send_uctx_t send_uctx = {
843 			.send_msg_addr = args->msg,
844 			.send_msg_size = args->send_size,
845 		};
846 
847 		/*
848 		 * Perform a crude copyin of the mach_msg_user_header_t,
849 		 * including the next 4 bytes in case it is a complex message,
850 		 * in order to mimic mach_msg2_trap() behavior of synthesizing
851 		 * the mach_msg_user_base_t via the trap arguments.
852 		 *
853 		 * mach_msg_trap_send() will do more thorough validation of
854 		 * sizes and header.
855 		 */
856 
857 		mr = mach_msg_copyin_user_header(&send_uctx, options);
858 		if (mr != MACH_MSG_SUCCESS) {
859 			return mr;
860 		}
861 
862 		mr = mach_msg_trap_send(&send_uctx, options,
863 		    args->timeout, args->priority);
864 	}
865 
866 	/* If send failed, skip receive */
867 	if (mr != MACH_MSG_SUCCESS) {
868 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
869 		goto end;
870 	}
871 
872 	if (options & MACH_RCV_MSG) {
873 		mach_vm_address_t msg_addr = args->rcv_msg ?: args->msg;
874 		mach_port_name_t  sync_send = MACH_PORT_NULL;
875 
876 		/*
877 		 * Although just the presence of MACH_RCV_SYNC_WAIT and absence of
878 		 * MACH_SEND_OVERRIDE should imply that 'priority' is a valid port name
879 		 * to link, in practice older userspace is dependent on
880 		 * MACH_SEND_SYNC_OVERRIDE also excluding this path.
881 		 */
882 		if ((options & MACH_RCV_SYNC_WAIT) &&
883 		    !(options & (MACH_SEND_OVERRIDE | MACH_SEND_MSG)) &&
884 		    !(options & MACH_SEND_SYNC_OVERRIDE)) {
885 			sync_send = (mach_port_name_t)args->priority;
886 		}
887 
888 		mr = mach_msg_trap_receive(msg_addr, 0, options, args->timeout,
889 		    sync_send, args->rcv_size, 0, args->rcv_name);
890 	}
891 
892 end:
893 	/* unblock call is idempotent */
894 	ipc_port_thread_group_unblocked();
895 	return mr;
896 #else
897 	(void)args;
898 	return KERN_NOT_SUPPORTED;
899 #endif /* IPC_HAS_LEGACY_MACH_MSG_TRAP */
900 }
901 
902 /*
903  *  Routine:    mach_msg_trap [mach trap]
904  *  Purpose:
905  *      Possibly send a message; possibly receive a message.
906  *
907  *		/!\ Deprecated /!\
908  *      No longer supported on embedded and will be removed from macOS.
909  *      Use mach_msg2_trap() instead.
910  *  Conditions:
911  *      Nothing locked.
912  *  Returns:
913  *      All of mach_msg_send and mach_msg_receive error codes.
914  */
915 mach_msg_return_t
mach_msg_trap(struct mach_msg_overwrite_trap_args * args)916 mach_msg_trap(
917 	struct mach_msg_overwrite_trap_args *args)
918 {
919 	args->rcv_msg = (mach_vm_address_t)0;
920 
921 	return mach_msg_overwrite_trap(args);
922 }
923 
924 /*
925  *  Routine:    mach_msg2_trap [mach trap]
926  *  Purpose:
927  *      Modern mach_msg_trap() with vector message and CFI support.
928  *  Conditions:
929  *      Nothing locked.
930  *  Returns:
931  *      All of mach_msg_send and mach_msg_receive error codes.
932  */
933 mach_msg_return_t
mach_msg2_trap(struct mach_msg2_trap_args * args)934 mach_msg2_trap(
935 	struct mach_msg2_trap_args *args)
936 {
937 	/* packed arguments, LO_BITS_and_HI_BITS */
938 	uint64_t mb_ss = args->msgh_bits_and_send_size;
939 	uint64_t mr_lp = args->msgh_remote_and_local_port;
940 	uint64_t mv_id = args->msgh_voucher_and_id;
941 	uint64_t dc_rn = args->desc_count_and_rcv_name;
942 	uint64_t rs_pr = args->rcv_size_and_priority;
943 
944 	mach_msg_timeout_t  msg_timeout = (mach_msg_timeout_t)args->timeout;
945 	mach_msg_vector_t   msg_vec = {}, aux_vec = {};
946 	mach_msg_size_t     vec_snd_count = 0;
947 	mach_msg_size_t     vec_rcv_count = 0;
948 	mach_msg_option64_t option64;
949 	mach_msg_return_t   mr = MACH_MSG_SUCCESS;
950 
951 	option64 = ipc_current_user_policy(current_task(),
952 	    args->options) | MACH64_MACH_MSG2;
953 
954 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
955 
956 	mr = ipc_preflight_msg_option64(option64);
957 	if (mr != MACH_MSG_SUCCESS) {
958 		goto send_fail;
959 	}
960 
961 	if (option64 & MACH64_MSG_VECTOR) {
962 		vec_snd_count = (mach_msg_size_t)(mb_ss >> 32);
963 		vec_rcv_count = (mach_msg_size_t)rs_pr;
964 
965 		mr = mach_msg_copyin_data_vectors(args->data,
966 		    MAX(vec_snd_count, vec_rcv_count), option64, &msg_vec, &aux_vec);
967 		if (mr != MACH_MSG_SUCCESS) {
968 			goto send_fail;
969 		}
970 	}
971 
972 	if (option64 & MACH64_SEND_MSG) {
973 		mach_msg_send_uctx_t send_uctx = {
974 			.send_header = {
975 				.msgh_bits         = (mach_msg_bits_t) (mb_ss),
976 				.msgh_size         = 0,
977 				.msgh_remote_port  = (mach_port_name_t)(mr_lp),
978 				.msgh_local_port   = (mach_port_name_t)(mr_lp >> 32),
979 				.msgh_voucher_port = (mach_port_name_t)(mv_id),
980 				.msgh_id           = (mach_msg_id_t)   (mv_id >> 32),
981 			},
982 			.send_dsc_count = (mach_msg_size_t)dc_rn,
983 		};
984 		mach_msg_priority_t priority = (mach_msg_priority_t)(rs_pr >> 32);
985 
986 		if (option64 & MACH64_MSG_VECTOR) {
987 			/*
988 			 * only validate msg send related arguments.
989 			 *
990 			 * bad receive args do not stop us from sending
991 			 * during combined send/rcv.
992 			 */
993 			mr = mach_msg_validate_data_vectors(&msg_vec, &aux_vec,
994 			    vec_snd_count, option64, /* sending? */ TRUE);
995 			if (mr != MACH_MSG_SUCCESS) {
996 				goto send_fail;
997 			}
998 
999 			send_uctx.send_msg_addr = msg_vec.msgv_data;
1000 			send_uctx.send_msg_size = msg_vec.msgv_send_size;
1001 			send_uctx.send_aux_size = aux_vec.msgv_send_size;
1002 			if (send_uctx.send_aux_size) {
1003 				send_uctx.send_aux_addr = aux_vec.msgv_data;
1004 			}
1005 		} else {
1006 			send_uctx.send_msg_addr = args->data;
1007 			send_uctx.send_msg_size = (mach_msg_size_t)(mb_ss >> 32);
1008 		}
1009 
1010 		mr = mach_msg_trap_send(&send_uctx, option64, msg_timeout, priority);
1011 	}
1012 
1013 send_fail:
1014 	/* if send failed, skip receive */
1015 	if (mr != MACH_MSG_SUCCESS) {
1016 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
1017 		goto end;
1018 	}
1019 
1020 	if (option64 & MACH64_RCV_MSG) {
1021 		mach_port_name_t    rcv_name   = (mach_port_name_t)(dc_rn >> 32);
1022 		mach_port_name_t    sync_send  = MACH_PORT_NULL;
1023 		mach_vm_address_t   msg_addr   = 0;
1024 		mach_msg_size_t     msg_size   = 0;
1025 		mach_vm_address_t   aux_addr   = 0;
1026 		mach_msg_size_t     aux_size   = 0;
1027 
1028 		if (option64 & MACH64_MSG_VECTOR) {
1029 			/* only validate msg receive related arguments */
1030 			mr = mach_msg_validate_data_vectors(&msg_vec, &aux_vec,
1031 			    vec_rcv_count, option64, /* sending? */ FALSE);
1032 			if (mr != MACH_MSG_SUCCESS) {
1033 				goto end;
1034 			}
1035 
1036 			msg_addr = msg_vec.msgv_rcv_addr ?: msg_vec.msgv_data;
1037 			msg_size = msg_vec.msgv_rcv_size;
1038 			aux_size = aux_vec.msgv_rcv_size;
1039 			if (aux_size) {
1040 				aux_addr = aux_vec.msgv_rcv_addr ?: aux_vec.msgv_data;
1041 			}
1042 		} else {
1043 			msg_addr = args->data;
1044 			msg_size = (mach_msg_size_t)rs_pr;
1045 		}
1046 
1047 		if (option64 & MACH64_RCV_SYNC_WAIT) {
1048 			/* use msgh_remote_port as sync send boosting port */
1049 			sync_send = (mach_port_name_t)mr_lp;
1050 		}
1051 
1052 		mr = mach_msg_trap_receive(msg_addr, aux_addr, option64,
1053 		    msg_timeout, sync_send, msg_size, aux_size, rcv_name);
1054 	}
1055 
1056 end:
1057 	/* unblock call is idempotent */
1058 	ipc_port_thread_group_unblocked();
1059 	return mr;
1060 }
1061 
1062 /*
1063  *  Routine:    mach_msg_rcv_link_special_reply_port
1064  *  Purpose:
1065  *      Link the special reply port(rcv right) to the
1066  *      other end of the sync ipc channel.
1067  *  Conditions:
1068  *      Nothing locked.
1069  *  Returns:
1070  *      None.
1071  */
1072 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)1073 mach_msg_rcv_link_special_reply_port(
1074 	ipc_port_t special_reply_port,
1075 	mach_port_name_t dest_name_port)
1076 {
1077 	ipc_port_t dest_port = IP_NULL;
1078 	kern_return_t kr;
1079 
1080 	if (current_thread()->ith_special_reply_port != special_reply_port) {
1081 		return MACH_RCV_INVALID_NOTIFY;
1082 	}
1083 
1084 	/* Copyin the destination port */
1085 	if (!MACH_PORT_VALID(dest_name_port)) {
1086 		return MACH_RCV_INVALID_NOTIFY;
1087 	}
1088 
1089 	kr = ipc_port_translate_send(current_space(), dest_name_port, &dest_port);
1090 	if (kr == KERN_SUCCESS) {
1091 		ip_reference(dest_port);
1092 		ip_mq_unlock(dest_port);
1093 
1094 		/*
1095 		 * The receive right of dest port might have gone away,
1096 		 * do not fail the receive in that case.
1097 		 */
1098 		ipc_port_link_special_reply_port(special_reply_port,
1099 		    dest_port, FALSE);
1100 
1101 		ip_release(dest_port);
1102 	}
1103 	return MACH_MSG_SUCCESS;
1104 }
1105 
1106 /*
1107  *  Routine:    mach_msg_receive_results_complete
1108  *  Purpose:
1109  *      Get thread's turnstile back from the object and
1110  *              if object is a special reply port then reset its
1111  *      linkage.
1112  *  Condition:
1113  *      Nothing locked.
1114  *  Returns:
1115  *      None.
1116  */
1117 void
mach_msg_receive_results_complete(ipc_object_t object)1118 mach_msg_receive_results_complete(ipc_object_t object)
1119 {
1120 	thread_t self = current_thread();
1121 	ipc_port_t port = IP_NULL;
1122 	boolean_t get_turnstile = (self->turnstile == TURNSTILE_NULL);
1123 
1124 	if (io_otype(object) == IOT_PORT) {
1125 		port = ip_object_to_port(object);
1126 	} else {
1127 		assert(self->turnstile != TURNSTILE_NULL);
1128 		return;
1129 	}
1130 
1131 	uint8_t flags = IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE;
1132 
1133 	/*
1134 	 * Don't clear the ip_srp_msg_sent bit if...
1135 	 */
1136 	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
1137 	    self->ith_state == MACH_RCV_INTERRUPTED ||
1138 	    self->ith_state == MACH_RCV_TIMED_OUT ||
1139 #if MACH_FLIPC
1140 	    self->ith_state == MACH_PEEK_READY ||
1141 #endif /* MACH_FLIPC */
1142 	    self->ith_state == MACH_RCV_PORT_CHANGED)) {
1143 		flags |= IPC_PORT_ADJUST_SR_RECEIVED_MSG;
1144 	}
1145 
1146 	if (port->ip_specialreply || get_turnstile) {
1147 		ip_mq_lock(port);
1148 		ipc_port_adjust_special_reply_port_locked(port, NULL,
1149 		    flags, get_turnstile);
1150 		/* port unlocked */
1151 	}
1152 	assert(self->turnstile != TURNSTILE_NULL);
1153 	/* thread now has a turnstile */
1154 }
1155 
1156 
1157 SECURITY_READ_ONLY_LATE(struct mach_msg_filter_callbacks) mach_msg_filter_callbacks;
1158 
1159 kern_return_t
mach_msg_filter_register_callback(const struct mach_msg_filter_callbacks * callbacks)1160 mach_msg_filter_register_callback(
1161 	const struct mach_msg_filter_callbacks *callbacks)
1162 {
1163 	size_t size = 0;
1164 
1165 	if (callbacks == NULL) {
1166 		return KERN_INVALID_ARGUMENT;
1167 	}
1168 	if (mach_msg_filter_callbacks.fetch_filter_policy != NULL) {
1169 		/* double init */
1170 		return KERN_FAILURE;
1171 	}
1172 
1173 	if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_0) {
1174 		/* check for missing v0 callbacks */
1175 		if (callbacks->fetch_filter_policy == NULL) {
1176 			return KERN_INVALID_ARGUMENT;
1177 		}
1178 		size = offsetof(struct mach_msg_filter_callbacks, alloc_service_port_sblabel);
1179 	}
1180 
1181 	if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_1) {
1182 		if (callbacks->alloc_service_port_sblabel == NULL ||
1183 		    callbacks->dealloc_service_port_sblabel == NULL ||
1184 		    callbacks->derive_sblabel_from_service_port == NULL ||
1185 		    callbacks->get_connection_port_filter_policy == NULL ||
1186 		    callbacks->retain_sblabel == NULL) {
1187 			return KERN_INVALID_ARGUMENT;
1188 		}
1189 		size = sizeof(struct mach_msg_filter_callbacks);
1190 	}
1191 
1192 	if (callbacks->version > MACH_MSG_FILTER_CALLBACKS_VERSION_1) {
1193 		/* invalid version */
1194 		return KERN_INVALID_ARGUMENT;
1195 	}
1196 
1197 	memcpy(&mach_msg_filter_callbacks, callbacks, size);
1198 	return KERN_SUCCESS;
1199 }
1200 
1201 /* This function should only be called if the task and port allow message filtering */
1202 boolean_t
mach_msg_fetch_filter_policy(void * port_label,mach_msg_id_t msgh_id,mach_msg_filter_id * fid)1203 mach_msg_fetch_filter_policy(
1204 	void *port_label,
1205 	mach_msg_id_t msgh_id,
1206 	mach_msg_filter_id *fid)
1207 {
1208 	boolean_t ret = TRUE;
1209 
1210 	if (mach_msg_fetch_filter_policy_callback == NULL) {
1211 		*fid = MACH_MSG_FILTER_POLICY_ALLOW;
1212 		return true;
1213 	}
1214 	ret = mach_msg_fetch_filter_policy_callback(current_task(), port_label, msgh_id, fid);
1215 
1216 	return ret;
1217 }
1218