xref: /xnu-8020.140.41/osfmk/ipc/mach_msg.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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.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_port.h>
104 #include <ipc/ipc_pset.h>
105 #include <ipc/ipc_space.h>
106 #include <ipc/ipc_entry.h>
107 #include <ipc/ipc_importance.h>
108 #include <ipc/ipc_voucher.h>
109 
110 #include <machine/machine_routines.h>
111 #include <security/mac_mach_internal.h>
112 
113 #include <sys/kdebug.h>
114 
115 #ifndef offsetof
116 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
117 #endif /* offsetof */
118 
119 /*
120  * Forward declarations - kernel internal routines
121  */
122 
123 static mach_msg_return_t msg_receive_error(
124 	ipc_kmsg_t              kmsg,
125 	mach_msg_option_t       option,
126 	mach_vm_address_t       rcv_addr,
127 	mach_msg_size_t         rcv_size,
128 	mach_port_seqno_t       seqno,
129 	ipc_space_t             space,
130 	mach_msg_size_t         *out_size);
131 
132 static mach_msg_return_t
133 mach_msg_rcv_link_special_reply_port(
134 	ipc_port_t special_reply_port,
135 	mach_port_name_t dest_name_port);
136 
137 void
138 mach_msg_receive_results_complete(ipc_object_t object);
139 
140 const security_token_t KERNEL_SECURITY_TOKEN = KERNEL_SECURITY_TOKEN_VALUE;
141 const audit_token_t KERNEL_AUDIT_TOKEN = KERNEL_AUDIT_TOKEN_VALUE;
142 
143 /*
144  *	Routine:	mach_msg_receive_results
145  *	Purpose:
146  *		Receive a message.
147  *	Conditions:
148  *		Nothing locked.
149  *	Returns:
150  *		MACH_MSG_SUCCESS	Received a message.
151  *		MACH_RCV_INVALID_NAME	The name doesn't denote a right,
152  *			or the denoted right is not receive or port set.
153  *		MACH_RCV_IN_SET		Receive right is a member of a set.
154  *		MACH_RCV_TOO_LARGE	Message wouldn't fit into buffer.
155  *		MACH_RCV_TIMED_OUT	Timeout expired without a message.
156  *		MACH_RCV_INTERRUPTED	Reception interrupted.
157  *		MACH_RCV_PORT_DIED	Port/set died while receiving.
158  *		MACH_RCV_PORT_CHANGED	Port moved into set while receiving.
159  *		MACH_RCV_INVALID_DATA	Couldn't copy to user buffer.
160  *		MACH_RCV_INVALID_NOTIFY	Bad notify port.
161  *		MACH_RCV_HEADER_ERROR
162  */
163 
164 mach_msg_return_t
mach_msg_receive_results(mach_msg_size_t * sizep)165 mach_msg_receive_results(
166 	mach_msg_size_t *sizep)
167 {
168 	thread_t          self = current_thread();
169 	ipc_space_t       space = current_space();
170 	vm_map_t          map = current_map();
171 
172 	ipc_object_t      object = self->ith_object;
173 	mach_msg_return_t mr = self->ith_state;
174 	mach_vm_address_t rcv_addr = self->ith_msg_addr;
175 	mach_msg_size_t   rcv_size = self->ith_rsize;
176 	mach_msg_option_t option = self->ith_option;
177 	ipc_kmsg_t        kmsg = self->ith_kmsg;
178 	mach_port_seqno_t seqno = self->ith_seqno;
179 
180 	mach_msg_trailer_size_t trailer_size;
181 	mach_vm_address_t context;
182 	mach_msg_size_t   size = 0;
183 
184 	/*
185 	 * unlink the special_reply_port before releasing reference to object.
186 	 * get the thread's turnstile, if the thread donated it's turnstile to the port
187 	 */
188 	mach_msg_receive_results_complete(object);
189 	io_release(object);
190 
191 	if (mr != MACH_MSG_SUCCESS) {
192 		if (mr == MACH_RCV_TOO_LARGE) {
193 			/*
194 			 * If the receive operation occurs with MACH_RCV_LARGE set
195 			 * then no message was extracted from the queue, and the size
196 			 * and (optionally) receiver names were the only thing captured.
197 			 * Just copyout the size (and optional port name) in a fake
198 			 * header.
199 			 */
200 			if (option & MACH_RCV_LARGE) {
201 				if ((option & MACH_RCV_STACK) == 0 &&
202 				    rcv_size >= offsetof(mach_msg_user_header_t, msgh_reserved)) {
203 					/*
204 					 * We need to inform the user-level code that it needs more
205 					 * space.  The value for how much space was returned in the
206 					 * msize save area instead of the message (which was left on
207 					 * the queue).
208 					 */
209 					if (option & MACH_RCV_LARGE_IDENTITY) {
210 						if (copyout((char *) &self->ith_receiver_name,
211 						    rcv_addr + offsetof(mach_msg_user_header_t, msgh_local_port),
212 						    sizeof(mach_port_name_t))) {
213 							mr = MACH_RCV_INVALID_DATA;
214 						}
215 					}
216 					if (copyout((char *) &self->ith_msize,
217 					    rcv_addr + offsetof(mach_msg_user_header_t, msgh_size),
218 					    sizeof(mach_msg_size_t))) {
219 						mr = MACH_RCV_INVALID_DATA;
220 					}
221 				}
222 			} else {
223 				/* discard importance in message */
224 				ipc_importance_clean(kmsg);
225 
226 				if (msg_receive_error(kmsg, option, rcv_addr, rcv_size, seqno, space, &size)
227 				    == MACH_RCV_INVALID_DATA) {
228 					mr = MACH_RCV_INVALID_DATA;
229 				}
230 			}
231 		}
232 
233 		if (sizep) {
234 			*sizep = size;
235 		}
236 		return mr;
237 	}
238 
239 	/* MACH_MSG_SUCCESS */
240 
241 #if IMPORTANCE_INHERITANCE
242 
243 	/* adopt/transform any importance attributes carried in the message */
244 	ipc_importance_receive(kmsg, option);
245 
246 #endif  /* IMPORTANCE_INHERITANCE */
247 
248 	/* auto redeem the voucher in the message */
249 	ipc_voucher_receive_postprocessing(kmsg, option);
250 
251 	/* Save destination port context for the trailer before copyout */
252 	context = kmsg->ikm_header->msgh_remote_port->ip_context;
253 
254 	mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL, option);
255 
256 	trailer_size = ipc_kmsg_trailer_size(option, self);
257 
258 	if (mr != MACH_MSG_SUCCESS) {
259 		/* already received importance, so have to undo that here */
260 		ipc_importance_unreceive(kmsg, option);
261 
262 		/* if we had a body error copyout what we have, otherwise a simple header/trailer */
263 		if ((mr & ~MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
264 			ipc_kmsg_add_trailer(kmsg, space, option, self, seqno, FALSE, context);
265 			if (ipc_kmsg_put_to_user(kmsg, option, rcv_addr, rcv_size,
266 			    trailer_size, &size) == MACH_RCV_INVALID_DATA) {
267 				mr = MACH_RCV_INVALID_DATA;
268 			}
269 		} else {
270 			if (msg_receive_error(kmsg, option, rcv_addr, rcv_size, seqno, space, &size)
271 			    == MACH_RCV_INVALID_DATA) {
272 				mr = MACH_RCV_INVALID_DATA;
273 			}
274 		}
275 	} else {
276 		/* capture ksmg QoS values to the thread continuation state */
277 		self->ith_ppriority = kmsg->ikm_ppriority;
278 		self->ith_qos_override = kmsg->ikm_qos_override;
279 		ipc_kmsg_add_trailer(kmsg, space, option, self, seqno, FALSE, context);
280 		mr = ipc_kmsg_put_to_user(kmsg, option, rcv_addr, rcv_size,
281 		    trailer_size, &size);
282 	}
283 
284 	if (sizep) {
285 		*sizep = size;
286 	}
287 	return mr;
288 }
289 
290 void
mach_msg_receive_continue(void)291 mach_msg_receive_continue(void)
292 {
293 	mach_msg_return_t mr;
294 	thread_t self = current_thread();
295 
296 	ipc_port_thread_group_unblocked();
297 	if (self->ith_state == MACH_PEEK_READY) {
298 		mr = MACH_PEEK_READY;
299 	} else {
300 		mr = mach_msg_receive_results(NULL);
301 	}
302 	(*self->ith_continuation)(mr);
303 }
304 
305 
306 /*
307  *	Routine:	mach_msg_overwrite_trap [mach trap]
308  *	Purpose:
309  *		Possibly send a message; possibly receive a message.
310  *	Conditions:
311  *		Nothing locked.
312  *		The 'priority' is only a QoS if MACH_SEND_OVERRIDE is passed -
313  *		otherwise, it is a port name.
314  *	Returns:
315  *		All of mach_msg_send and mach_msg_receive error codes.
316  */
317 
318 mach_msg_return_t
mach_msg_overwrite_trap(struct mach_msg_overwrite_trap_args * args)319 mach_msg_overwrite_trap(
320 	struct mach_msg_overwrite_trap_args *args)
321 {
322 	mach_vm_address_t       msg_addr = args->msg;
323 	mach_msg_option_t       option = args->option;
324 	mach_msg_size_t         send_size = args->send_size;
325 	mach_msg_size_t         rcv_size = args->rcv_size;
326 	mach_port_name_t        rcv_name = args->rcv_name;
327 	mach_msg_timeout_t      msg_timeout = args->timeout;
328 	mach_msg_priority_t     priority = args->priority;
329 	mach_vm_address_t       rcv_msg_addr = args->rcv_msg;
330 	__unused mach_port_seqno_t temp_seqno = 0;
331 
332 	mach_msg_return_t  mr = MACH_MSG_SUCCESS;
333 	vm_map_t map = current_map();
334 
335 	/*
336 	 * Only accept options allowed by the user.  Extract user-only options up
337 	 * front, as they are not included in MACH_MSG_OPTION_USER.
338 	 */
339 	bool filter_nonfatal = (option & MACH_SEND_FILTER_NONFATAL);
340 
341 	option &= MACH_MSG_OPTION_USER;
342 
343 	if (option & MACH_SEND_MSG) {
344 		ipc_space_t space = current_space();
345 		ipc_kmsg_t kmsg;
346 
347 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
348 
349 		mr = ipc_kmsg_get_from_user(msg_addr, send_size, &kmsg);
350 
351 		if (mr != MACH_MSG_SUCCESS) {
352 			KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
353 			return mr;
354 		}
355 
356 		KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
357 		    (uintptr_t)msg_addr,
358 		    VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
359 		    0, 0,
360 		    0);
361 
362 		mr = ipc_kmsg_copyin_from_user(kmsg, space, map, priority, &option,
363 		    filter_nonfatal);
364 
365 		if (mr != MACH_MSG_SUCCESS) {
366 			ipc_kmsg_free(kmsg);
367 			KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
368 			goto end;
369 		}
370 
371 		mr = ipc_kmsg_send(kmsg, option, msg_timeout);
372 
373 		if (mr != MACH_MSG_SUCCESS) {
374 			mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL);
375 			(void) ipc_kmsg_put_to_user(kmsg, option, msg_addr, send_size, 0, NULL);
376 			KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
377 			goto end;
378 		}
379 	}
380 
381 	if (option & MACH_RCV_MSG) {
382 		thread_t self = current_thread();
383 		ipc_space_t space = current_space();
384 		ipc_object_t object;
385 
386 		mr = ipc_mqueue_copyin(space, rcv_name, &object);
387 		if (mr != MACH_MSG_SUCCESS) {
388 			goto end;
389 		}
390 		/* hold ref for object */
391 
392 		/*
393 		 * Although just the presence of MACH_RCV_SYNC_WAIT and absence of
394 		 * MACH_SEND_OVERRIDE should imply that 'priority' is a valid port name
395 		 * to link, in practice older userspace is dependent on
396 		 * MACH_SEND_SYNC_OVERRIDE also excluding this path.
397 		 */
398 		if ((option & MACH_RCV_SYNC_WAIT) &&
399 		    !(option & (MACH_SEND_OVERRIDE | MACH_SEND_MSG)) &&
400 		    !(option & MACH_SEND_SYNC_OVERRIDE)) {
401 			ipc_port_t special_reply_port;
402 			special_reply_port = ip_object_to_port(object);
403 			/* link the special reply port to the destination */
404 			mr = mach_msg_rcv_link_special_reply_port(special_reply_port,
405 			    (mach_port_name_t)priority);
406 			if (mr != MACH_MSG_SUCCESS) {
407 				io_release(object);
408 				goto end;
409 			}
410 		}
411 
412 		if (rcv_msg_addr != (mach_vm_address_t)0) {
413 			self->ith_msg_addr = rcv_msg_addr;
414 		} else {
415 			self->ith_msg_addr = msg_addr;
416 		}
417 		self->ith_object = object;
418 		self->ith_rsize = rcv_size;
419 		self->ith_msize = 0;
420 		self->ith_option = option;
421 		self->ith_receiver_name = MACH_PORT_NULL;
422 		self->ith_continuation = thread_syscall_return;
423 		self->ith_knote = ITH_KNOTE_NULL;
424 
425 		ipc_mqueue_receive(io_waitq(object), option, rcv_size, msg_timeout,
426 		    THREAD_ABORTSAFE);
427 		if ((option & MACH_RCV_TIMEOUT) && msg_timeout == 0) {
428 			thread_poll_yield(self);
429 		}
430 		mr = mach_msg_receive_results(NULL);
431 		goto end;
432 	}
433 
434 end:
435 	ipc_port_thread_group_unblocked();
436 	return mr;
437 }
438 
439 /*
440  *	Routine:	mach_msg_rcv_link_special_reply_port
441  *	Purpose:
442  *		Link the special reply port(rcv right) to the
443  *		other end of the sync ipc channel.
444  *	Conditions:
445  *		Nothing locked.
446  *	Returns:
447  *		None.
448  */
449 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)450 mach_msg_rcv_link_special_reply_port(
451 	ipc_port_t special_reply_port,
452 	mach_port_name_t dest_name_port)
453 {
454 	ipc_port_t dest_port = IP_NULL;
455 	kern_return_t kr;
456 
457 	if (current_thread()->ith_special_reply_port != special_reply_port) {
458 		return MACH_RCV_INVALID_NOTIFY;
459 	}
460 
461 	/* Copyin the destination port */
462 	if (!MACH_PORT_VALID(dest_name_port)) {
463 		return MACH_RCV_INVALID_NOTIFY;
464 	}
465 
466 	kr = ipc_port_translate_send(current_space(), dest_name_port, &dest_port);
467 	if (kr == KERN_SUCCESS) {
468 		ip_reference(dest_port);
469 		ip_mq_unlock(dest_port);
470 
471 		/*
472 		 * The receive right of dest port might have gone away,
473 		 * do not fail the receive in that case.
474 		 */
475 		ipc_port_link_special_reply_port(special_reply_port,
476 		    dest_port, FALSE);
477 
478 		ip_release(dest_port);
479 	}
480 	return MACH_MSG_SUCCESS;
481 }
482 
483 /*
484  *	Routine:	mach_msg_receive_results_complete
485  *	Purpose:
486  *		Get thread's turnstile back from the object and
487  *              if object is a special reply port then reset its
488  *		linkage.
489  *	Condition:
490  *		Nothing locked.
491  *	Returns:
492  *		None.
493  */
494 void
mach_msg_receive_results_complete(ipc_object_t object)495 mach_msg_receive_results_complete(ipc_object_t object)
496 {
497 	thread_t self = current_thread();
498 	ipc_port_t port = IPC_PORT_NULL;
499 	boolean_t get_turnstile = (self->turnstile == TURNSTILE_NULL);
500 
501 	if (io_otype(object) == IOT_PORT) {
502 		port = ip_object_to_port(object);
503 	} else {
504 		assert(self->turnstile != TURNSTILE_NULL);
505 		return;
506 	}
507 
508 	uint8_t flags = IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE;
509 
510 	/*
511 	 * Don't clear the ip_srp_msg_sent bit if...
512 	 */
513 	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
514 	    self->ith_state == MACH_RCV_INTERRUPTED ||
515 	    self->ith_state == MACH_RCV_TIMED_OUT ||
516 	    self->ith_state == MACH_RCV_PORT_CHANGED ||
517 	    self->ith_state == MACH_PEEK_READY)) {
518 		flags |= IPC_PORT_ADJUST_SR_RECEIVED_MSG;
519 	}
520 
521 	if (port->ip_specialreply || get_turnstile) {
522 		ip_mq_lock(port);
523 		ipc_port_adjust_special_reply_port_locked(port, NULL,
524 		    flags, get_turnstile);
525 		/* port unlocked */
526 	}
527 	assert(self->turnstile != TURNSTILE_NULL);
528 	/* thread now has a turnstile */
529 }
530 
531 /*
532  *	Routine:	mach_msg_trap [mach trap]
533  *	Purpose:
534  *		Possibly send a message; possibly receive a message.
535  *	Conditions:
536  *		Nothing locked.
537  *	Returns:
538  *		All of mach_msg_send and mach_msg_receive error codes.
539  */
540 
541 mach_msg_return_t
mach_msg_trap(struct mach_msg_overwrite_trap_args * args)542 mach_msg_trap(
543 	struct mach_msg_overwrite_trap_args *args)
544 {
545 	kern_return_t kr;
546 	args->rcv_msg = (mach_vm_address_t)0;
547 
548 	kr = mach_msg_overwrite_trap(args);
549 	return kr;
550 }
551 
552 
553 /*
554  *	Routine:	msg_receive_error	[internal]
555  *	Purpose:
556  *		Builds a minimal header/trailer and copies it to
557  *		the user message buffer.  Invoked when in the case of a
558  *		MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error.
559  *	Conditions:
560  *		Nothing locked.
561  *		size - maximum buffer size on input,
562  *		       actual copied-out size on output
563  *	Returns:
564  *		MACH_MSG_SUCCESS	minimal header/trailer copied
565  *		MACH_RCV_INVALID_DATA	copyout to user buffer failed
566  */
567 
568 static mach_msg_return_t
msg_receive_error(ipc_kmsg_t kmsg,mach_msg_option_t option,mach_vm_address_t rcv_addr,mach_msg_size_t rcv_size,mach_port_seqno_t seqno,ipc_space_t space,mach_msg_size_t * sizep)569 msg_receive_error(
570 	ipc_kmsg_t              kmsg,
571 	mach_msg_option_t       option,
572 	mach_vm_address_t       rcv_addr,
573 	mach_msg_size_t         rcv_size,
574 	mach_port_seqno_t       seqno,
575 	ipc_space_t             space,
576 	mach_msg_size_t         *sizep)
577 {
578 	mach_vm_address_t       context;
579 	mach_msg_trailer_size_t trailer_size;
580 	thread_t                self = current_thread();
581 
582 	context = kmsg->ikm_header->msgh_remote_port->ip_context;
583 
584 	/*
585 	 * Copy out the destination port in the message.
586 	 * Destroy all other rights and memory in the message.
587 	 */
588 	ipc_kmsg_copyout_dest_to_user(kmsg, space);
589 
590 	/*
591 	 * Build a minimal message with the requested trailer.
592 	 */
593 	kmsg->ikm_header->msgh_size = sizeof(mach_msg_header_t);
594 	ipc_kmsg_init_trailer(kmsg, sizeof(mach_msg_header_t), TASK_NULL);
595 
596 	trailer_size = ipc_kmsg_trailer_size(option, self);
597 	ipc_kmsg_add_trailer(kmsg, space, option, self,
598 	    seqno, TRUE, context);
599 
600 	/*
601 	 * Copy the message to user space and return the size
602 	 * (note that ipc_kmsg_put_to_user may also adjust the actual
603 	 * size copied out to user-space).
604 	 */
605 	if (ipc_kmsg_put_to_user(kmsg, option, rcv_addr, rcv_size,
606 	    trailer_size, sizep) == MACH_RCV_INVALID_DATA) {
607 		return MACH_RCV_INVALID_DATA;
608 	} else {
609 		return MACH_MSG_SUCCESS;
610 	}
611 }
612 
613 
614 SECURITY_READ_ONLY_LATE(struct mach_msg_filter_callbacks) mach_msg_filter_callbacks;
615 
616 kern_return_t
mach_msg_filter_register_callback(const struct mach_msg_filter_callbacks * callbacks)617 mach_msg_filter_register_callback(
618 	const struct mach_msg_filter_callbacks *callbacks)
619 {
620 	size_t size = 0;
621 
622 	if (callbacks == NULL) {
623 		return KERN_INVALID_ARGUMENT;
624 	}
625 	if (mach_msg_filter_callbacks.fetch_filter_policy != NULL) {
626 		/* double init */
627 		return KERN_FAILURE;
628 	}
629 
630 	if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_0) {
631 		/* check for missing v0 callbacks */
632 		if (callbacks->fetch_filter_policy == NULL) {
633 			return KERN_INVALID_ARGUMENT;
634 		}
635 		size = offsetof(struct mach_msg_filter_callbacks, alloc_service_port_sblabel);
636 	}
637 
638 	if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_1) {
639 		if (callbacks->alloc_service_port_sblabel == NULL ||
640 		    callbacks->dealloc_service_port_sblabel == NULL ||
641 		    callbacks->derive_sblabel_from_service_port == NULL ||
642 		    callbacks->get_connection_port_filter_policy == NULL ||
643 		    callbacks->retain_sblabel == NULL) {
644 			return KERN_INVALID_ARGUMENT;
645 		}
646 		size = sizeof(struct mach_msg_filter_callbacks);
647 	}
648 
649 	if (callbacks->version > MACH_MSG_FILTER_CALLBACKS_VERSION_1) {
650 		/* invalid version */
651 		return KERN_INVALID_ARGUMENT;
652 	}
653 
654 	memcpy(&mach_msg_filter_callbacks, callbacks, size);
655 	return KERN_SUCCESS;
656 }
657 
658 /* This function should only be called if the task and port allow message filtering */
659 boolean_t
mach_msg_fetch_filter_policy(void * port_label,mach_msg_id_t msgh_id,mach_msg_filter_id * fid)660 mach_msg_fetch_filter_policy(
661 	void *port_label,
662 	mach_msg_id_t msgh_id,
663 	mach_msg_filter_id *fid)
664 {
665 	boolean_t ret = TRUE;
666 
667 	if (mach_msg_fetch_filter_policy_callback == NULL) {
668 		*fid = MACH_MSG_FILTER_POLICY_ALLOW;
669 		return true;
670 	}
671 	ret = mach_msg_fetch_filter_policy_callback(current_task(), port_label, msgh_id, fid);
672 
673 	return ret;
674 }
675