xref: /xnu-8792.61.2/osfmk/kern/ipc_mig.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * Copyright (c) 2000-2004 Apple Computer, 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 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  */
58 
59 #include <mach/boolean.h>
60 #include <mach/port.h>
61 #include <mach/mig.h>
62 #include <mach/mig_errors.h>
63 #include <mach/mach_types.h>
64 #include <mach/mach_traps.h>
65 
66 #include <kern/ipc_tt.h>
67 #include <kern/ipc_mig.h>
68 #include <kern/kalloc.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71 #include <kern/ipc_kobject.h>
72 #include <kern/misc_protos.h>
73 
74 #include <ipc/port.h>
75 #include <ipc/ipc_kmsg.h>
76 #include <ipc/ipc_entry.h>
77 #include <ipc/ipc_object.h>
78 #include <ipc/ipc_mqueue.h>
79 #include <ipc/ipc_space.h>
80 #include <ipc/ipc_port.h>
81 #include <ipc/ipc_pset.h>
82 #include <ipc/ipc_notify.h>
83 #include <vm/vm_map.h>
84 #include <mach/thread_act.h>
85 
86 #include <libkern/OSAtomic.h>
87 
88 #define KERNEL_DESC_SIZE             sizeof(mach_msg_descriptor_t)
89 
90 void
91 mach_msg_receive_results_complete(ipc_object_t object);
92 
93 /*
94  *	Routine:	mach_msg_send_from_kernel
95  *	Purpose:
96  *		Send a message from the kernel.
97  *
98  *		This is used by the client side of KernelUser interfaces
99  *		to implement SimpleRoutines.  Currently, this includes
100  *		memory_object messages.
101  *	Conditions:
102  *		Nothing locked.
103  *	Returns:
104  *		MACH_MSG_SUCCESS	Sent the message.
105  *		MACH_SEND_INVALID_DEST	Bad destination port.
106  *		MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
107  *		                        or destination is above kernel limit
108  */
109 
110 mach_msg_return_t
mach_msg_send_from_kernel_proper(mach_msg_header_t * msg,mach_msg_size_t send_size)111 mach_msg_send_from_kernel_proper(
112 	mach_msg_header_t       *msg,
113 	mach_msg_size_t         send_size)
114 {
115 	mach_msg_option_t   option = MACH_SEND_KERNEL_DEFAULT;
116 	mach_msg_timeout_t  timeout_val = MACH_MSG_TIMEOUT_NONE;
117 	return kernel_mach_msg_send(msg, send_size, option, timeout_val, NULL);
118 }
119 
120 mach_msg_return_t
mach_msg_send_from_kernel_with_options(mach_msg_header_t * msg,mach_msg_size_t send_size,mach_msg_option_t option,mach_msg_timeout_t timeout_val)121 mach_msg_send_from_kernel_with_options(
122 	mach_msg_header_t       *msg,
123 	mach_msg_size_t         send_size,
124 	mach_msg_option_t       option,
125 	mach_msg_timeout_t      timeout_val)
126 {
127 	return kernel_mach_msg_send(msg, send_size, option, timeout_val, NULL);
128 }
129 
130 static mach_msg_return_t
kernel_mach_msg_send_common(ipc_kmsg_t kmsg,mach_msg_option_t option,mach_msg_timeout_t timeout_val,boolean_t * message_moved)131 kernel_mach_msg_send_common(
132 	ipc_kmsg_t              kmsg,
133 	mach_msg_option_t       option,
134 	mach_msg_timeout_t      timeout_val,
135 	boolean_t               *message_moved)
136 {
137 	mach_msg_return_t mr;
138 
139 	mr = ipc_kmsg_copyin_from_kernel(kmsg);
140 	if (mr != MACH_MSG_SUCCESS) {
141 		ipc_kmsg_free(kmsg);
142 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
143 		return mr;
144 	}
145 
146 	if (message_moved) {
147 		*message_moved = TRUE;
148 	}
149 
150 	/*
151 	 * Until we are sure of its effects, we are disabling
152 	 * importance donation from the kernel-side of user
153 	 * threads in importance-donating tasks - unless the
154 	 * option to force importance donation is passed in,
155 	 * or the thread's SEND_IMPORTANCE option has been set.
156 	 * (11938665 & 23925818)
157 	 */
158 	if (current_thread()->options & TH_OPT_SEND_IMPORTANCE) {
159 		option &= ~MACH_SEND_NOIMPORTANCE;
160 	} else if ((option & MACH_SEND_IMPORTANCE) == 0) {
161 		option |= MACH_SEND_NOIMPORTANCE;
162 	}
163 
164 	mr = ipc_kmsg_send(kmsg, option, timeout_val);
165 
166 	if (mr != MACH_MSG_SUCCESS) {
167 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_ALL);
168 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
169 	}
170 
171 	return mr;
172 }
173 
174 mach_msg_return_t
kernel_mach_msg_send(mach_msg_header_t * msg,mach_msg_size_t send_size,mach_msg_option_t option,mach_msg_timeout_t timeout_val,boolean_t * message_moved)175 kernel_mach_msg_send(
176 	mach_msg_header_t       *msg,
177 	mach_msg_size_t         send_size,
178 	mach_msg_option_t       option,
179 	mach_msg_timeout_t      timeout_val,
180 	boolean_t               *message_moved)
181 {
182 	ipc_kmsg_t kmsg;
183 	mach_msg_return_t mr;
184 
185 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
186 
187 	if (message_moved) {
188 		*message_moved = FALSE;
189 	}
190 
191 	mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
192 	if (mr != MACH_MSG_SUCCESS) {
193 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
194 		return mr;
195 	}
196 
197 	return kernel_mach_msg_send_common(kmsg, option, timeout_val, message_moved);
198 }
199 
200 mach_msg_return_t
201 kernel_mach_msg_send_with_builder_internal(
202 	mach_msg_size_t         desc_count,
203 	mach_msg_size_t         payload_size, /* Not total send size */
204 	mach_msg_option_t       option,
205 	mach_msg_timeout_t      timeout_val,
206 	boolean_t               *message_moved,
207 	void                    (^builder)(mach_msg_header_t *,
208 	mach_msg_descriptor_t *, void *))
209 {
210 	ipc_kmsg_t kmsg;
211 	mach_msg_return_t mr;
212 	mach_msg_header_t *hdr;
213 	void *udata;
214 	bool complex;
215 	mach_msg_size_t send_size;
216 	mach_msg_descriptor_t *desc;
217 
218 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
219 
220 	/*
221 	 * If message has descriptors it must be complex and vice versa. We assume
222 	 * this for messages originated from kernel. The two are not equivalent for
223 	 * user messages for bin-compat reasons.
224 	 */
225 	complex = (desc_count > 0);
226 	send_size = sizeof(mach_msg_header_t) + payload_size;
227 
228 	if (complex) {
229 		send_size += sizeof(mach_msg_body_t) + desc_count * KERNEL_DESC_SIZE;
230 	}
231 	if (message_moved) {
232 		*message_moved = FALSE;
233 	}
234 
235 	kmsg = ipc_kmsg_alloc(send_size, 0, desc_count,
236 	    IPC_KMSG_ALLOC_KERNEL | IPC_KMSG_ALLOC_ZERO);
237 	/* kmsg can be non-linear */
238 
239 	if (kmsg == IKM_NULL) {
240 		mr = MACH_SEND_NO_BUFFER;
241 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
242 		return mr;
243 	}
244 
245 	hdr = ikm_header(kmsg);
246 	udata = (payload_size > 0) ? ikm_udata(kmsg, desc_count, complex) : NULL;
247 	desc = (desc_count > 0) ? (mach_msg_descriptor_t *)((vm_address_t)hdr + sizeof(mach_msg_base_t)) : NULL;
248 
249 	/* Allow the caller to build the message, and sanity check it */
250 	builder(hdr, desc, udata);
251 	assert(hdr->msgh_size == send_size);
252 	if (complex) {
253 		assert(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX);
254 		hdr->msgh_bits |= MACH_MSGH_BITS_COMPLEX;
255 		/* Set the correct descriptor count */
256 		((mach_msg_base_t *)hdr)->body.msgh_descriptor_count = desc_count;
257 	} else {
258 		assert(!(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX));
259 		hdr->msgh_bits &= ~MACH_MSGH_BITS_COMPLEX;
260 	}
261 
262 	return kernel_mach_msg_send_common(kmsg, option, timeout_val, NULL);
263 }
264 
265 mach_msg_return_t
266 kernel_mach_msg_send_with_builder(
267 	mach_msg_size_t         desc_count,
268 	mach_msg_size_t         udata_size,
269 	void                    (^builder)(mach_msg_header_t *,
270 	mach_msg_descriptor_t *, void *))
271 {
272 	return kernel_mach_msg_send_with_builder_internal(desc_count, udata_size,
273 	           MACH_SEND_KERNEL_DEFAULT, MACH_MSG_TIMEOUT_NONE, NULL, builder);
274 }
275 
276 /*
277  *	Routine:	mach_msg_rpc_from_kernel
278  *	Purpose:
279  *		Send a message from the kernel and receive a reply.
280  *		Uses ith_rpc_reply for the reply port.
281  *
282  *		This is used by the client side of KernelUser interfaces
283  *		to implement Routines.
284  *	Conditions:
285  *		Nothing locked.
286  *	Returns:
287  *		MACH_MSG_SUCCESS	Sent the message.
288  *		MACH_RCV_PORT_DIED	The reply port was deallocated.
289  */
290 
291 mach_msg_return_t
mach_msg_rpc_from_kernel_proper(mach_msg_header_t * msg,mach_msg_size_t send_size,mach_msg_size_t rcv_size)292 mach_msg_rpc_from_kernel_proper(
293 	mach_msg_header_t       *msg,
294 	mach_msg_size_t         send_size,
295 	mach_msg_size_t         rcv_size)
296 {
297 	return kernel_mach_msg_rpc(msg, send_size, rcv_size, TRUE, NULL);
298 }
299 
300 mach_msg_return_t
kernel_mach_msg_rpc(mach_msg_header_t * msg,mach_msg_size_t send_size,mach_msg_size_t rcv_size,boolean_t interruptible,boolean_t * message_moved)301 kernel_mach_msg_rpc(
302 	mach_msg_header_t       *msg,
303 	mach_msg_size_t         send_size,
304 	mach_msg_size_t         rcv_size,
305 	boolean_t               interruptible,
306 	boolean_t               *message_moved)
307 {
308 	thread_t self = current_thread();
309 	ipc_port_t dest = IPC_PORT_NULL;
310 	ipc_port_t reply;
311 	ipc_kmsg_t kmsg;
312 	mach_msg_header_t *hdr;
313 	mach_port_seqno_t seqno;
314 	mach_msg_return_t mr;
315 
316 	assert(msg->msgh_local_port == MACH_PORT_NULL);
317 
318 	KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
319 
320 	if (message_moved) {
321 		*message_moved = FALSE;
322 	}
323 
324 	mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
325 	/* kmsg can be non-linear */
326 
327 	if (mr != MACH_MSG_SUCCESS) {
328 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
329 		return mr;
330 	}
331 	hdr = ikm_header(kmsg);
332 
333 	reply = self->ith_kernel_reply_port;
334 	if (reply == IP_NULL) {
335 		thread_get_kernel_special_reply_port();
336 		reply = self->ith_kernel_reply_port;
337 		if (reply == IP_NULL) {
338 			panic("mach_msg_rpc_from_kernel");
339 		}
340 	}
341 
342 	/* Get voucher port for the current thread's voucher */
343 	ipc_voucher_t voucher = IPC_VOUCHER_NULL;
344 	ipc_port_t voucher_port = IP_NULL;
345 	if (thread_get_mach_voucher(self, 0, &voucher) == KERN_SUCCESS) {
346 		/* If thread does not have a voucher, get the default voucher of the process */
347 		if (voucher == IPC_VOUCHER_NULL) {
348 			voucher = ipc_voucher_get_default_voucher();
349 		}
350 		voucher_port = convert_voucher_to_port(voucher);
351 	}
352 
353 	/* insert send-once right for the reply port and send right for the adopted voucher */
354 	hdr->msgh_local_port = reply;
355 	ipc_kmsg_set_voucher_port(kmsg, voucher_port, MACH_MSG_TYPE_MOVE_SEND);
356 	hdr->msgh_bits |=
357 	    MACH_MSGH_BITS_SET_PORTS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE, MACH_MSG_TYPE_MOVE_SEND);
358 
359 	mr = ipc_kmsg_copyin_from_kernel(kmsg);
360 	if (mr != MACH_MSG_SUCCESS) {
361 		/* Remove the voucher from the kmsg */
362 		voucher_port = ipc_kmsg_get_voucher_port(kmsg);
363 		ipc_kmsg_clear_voucher_port(kmsg);
364 		ipc_port_release_send(voucher_port);
365 
366 		ipc_kmsg_free(kmsg);
367 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
368 		return mr;
369 	}
370 
371 	if (message_moved) {
372 		*message_moved = TRUE;
373 	}
374 
375 	/*
376 	 * Sync IPC from kernel should pass adopted voucher and importance.
377 	 */
378 	mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
379 	option &= ~MACH_SEND_NOIMPORTANCE;
380 
381 	/*
382 	 * Destination port would be needed during receive for creating
383 	 * Sync IPC linkage with kernel special reply port, grab a reference
384 	 * of the destination port before it gets donated to mqueue in ipc_kmsg_send.
385 	 */
386 	dest = hdr->msgh_remote_port;
387 	ip_reference(dest);
388 
389 	mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
390 	if (mr != MACH_MSG_SUCCESS) {
391 		ip_release(dest);
392 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_ALL);
393 		KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
394 		return mr;
395 	}
396 
397 	for (;;) {
398 		assert(!ip_in_pset(reply));
399 		require_ip_active(reply);
400 
401 		/* JMM - why this check? */
402 		if (interruptible && !self->active && !self->inspection) {
403 			ip_release(dest);
404 			thread_dealloc_kernel_special_reply_port(current_thread());
405 			return MACH_RCV_INTERRUPTED;
406 		}
407 
408 		/* Setup the sync IPC linkage for the special reply port */
409 		ipc_port_link_special_reply_port(reply,
410 		    dest, FALSE);
411 
412 		ipc_mqueue_receive(&reply->ip_waitq,
413 		    MACH64_MSG_OPTION_NONE,
414 		    MACH_MSG_SIZE_MAX,
415 		    0,
416 		    MACH_MSG_TIMEOUT_NONE,
417 		    interruptible ? THREAD_INTERRUPTIBLE : THREAD_UNINT,
418 		    /* continuation ? */ false);
419 
420 		mr = self->ith_state;
421 		kmsg = self->ith_kmsg;
422 		seqno = self->ith_seqno;
423 
424 		mach_msg_receive_results_complete(ip_to_object(reply));
425 
426 		if (mr == MACH_MSG_SUCCESS) {
427 			break;
428 		}
429 
430 		assert(mr == MACH_RCV_INTERRUPTED);
431 		assert(interruptible);
432 		assert(reply == self->ith_kernel_reply_port);
433 
434 		if (thread_ast_peek(self, AST_APC)) {
435 			ip_release(dest);
436 			thread_dealloc_kernel_special_reply_port(current_thread());
437 			return mr;
438 		}
439 	}
440 
441 	/* release the destination port ref acquired above */
442 	ip_release(dest);
443 	dest = IPC_PORT_NULL;
444 
445 	/* reload hdr from reply kmsg got above */
446 	hdr = ikm_header(kmsg);
447 
448 	mach_msg_size_t kmsg_size = hdr->msgh_size;
449 	mach_msg_size_t kmsg_and_max_trailer_size;
450 
451 	/*
452 	 * The amount of trailer to receive is flexible (see below),
453 	 * but the kmsg header must have a size that allows for a maximum
454 	 * trailer to follow as that's how IPC works (otherwise it might be corrupt).
455 	 */
456 	if (os_add_overflow(kmsg_size, MAX_TRAILER_SIZE, &kmsg_and_max_trailer_size)) {
457 		panic("kernel_mach_msg_rpc");
458 	}
459 
460 	/* The message header and body itself must be receivable */
461 	if (rcv_size < kmsg_size) {
462 		ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_ALL);
463 		return MACH_RCV_TOO_LARGE;
464 	}
465 
466 	/*
467 	 *	We want to preserve rights and memory in reply!
468 	 *	We don't have to put them anywhere; just leave them
469 	 *	as they are.
470 	 */
471 	ipc_kmsg_copyout_dest_to_kernel(kmsg, ipc_space_reply);
472 
473 	mach_msg_format_0_trailer_t *trailer =  (mach_msg_format_0_trailer_t *)
474 	    ipc_kmsg_get_trailer(kmsg, false);
475 
476 	/* Determine what trailer bits we can receive (as no option specified) */
477 	if (rcv_size < kmsg_size + MACH_MSG_TRAILER_MINIMUM_SIZE) {
478 		rcv_size = kmsg_size;
479 	} else {
480 		if (rcv_size >= kmsg_and_max_trailer_size) {
481 			/*
482 			 * Enough room for a maximum trailer.
483 			 * JMM - we really should set the expected receiver-set fields:
484 			 *       (seqno, context, filterid, etc...) but nothing currently
485 			 *       expects them anyway.
486 			 */
487 			trailer->msgh_trailer_size = MAX_TRAILER_SIZE;
488 			rcv_size = kmsg_and_max_trailer_size;
489 		} else {
490 			assert(trailer->msgh_trailer_size == MACH_MSG_TRAILER_MINIMUM_SIZE);
491 			rcv_size = kmsg_size + MACH_MSG_TRAILER_MINIMUM_SIZE;
492 		}
493 	}
494 	assert(trailer->msgh_trailer_type == MACH_MSG_TRAILER_FORMAT_0);
495 	mr = MACH_MSG_SUCCESS;
496 
497 	ipc_kmsg_put_to_kernel(msg, kmsg, rcv_size);
498 	return mr;
499 }
500 
501 /*
502  *	Routine:	mach_msg_destroy_from_kernel_proper
503  *	Purpose:
504  *		mach_msg_destroy_from_kernel_proper is used to destroy
505  *		an unwanted/unexpected reply message from a MIG
506  *		kernel-specific user-side stub.	It is like ipc_kmsg_destroy(),
507  *		except we no longer have the kmsg - just the contents.
508  */
509 void
mach_msg_destroy_from_kernel_proper(mach_msg_header_t * msg)510 mach_msg_destroy_from_kernel_proper(mach_msg_header_t *msg)
511 {
512 	mach_msg_bits_t mbits = msg->msgh_bits;
513 	ipc_object_t object;
514 
515 	object = (ipc_object_t) msg->msgh_remote_port;
516 	if (IO_VALID(object)) {
517 		ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
518 	}
519 
520 	/*
521 	 * The destination (now in msg->msgh_local_port via
522 	 * ipc_kmsg_copyout_dest_to_kernel) has been consumed with
523 	 * ipc_object_copyout_dest.
524 	 */
525 
526 	/* MIG kernel users don't receive vouchers */
527 	assert(!MACH_MSGH_BITS_VOUCHER(mbits));
528 
529 	/* For simple messages, we're done */
530 	if ((mbits & MACH_MSGH_BITS_COMPLEX) == 0) {
531 		return;
532 	}
533 
534 	/* Discard descriptor contents */
535 	mach_msg_body_t *body = (mach_msg_body_t *)(msg + 1);
536 	mach_msg_descriptor_t *daddr = (mach_msg_descriptor_t *)(body + 1);
537 	mach_msg_size_t i;
538 
539 	for (i = 0; i < body->msgh_descriptor_count; i++, daddr++) {
540 		switch (daddr->type.type) {
541 		case MACH_MSG_PORT_DESCRIPTOR: {
542 			mach_msg_port_descriptor_t *dsc = &daddr->port;
543 			if (IO_VALID((ipc_object_t) dsc->name)) {
544 				ipc_object_destroy((ipc_object_t) dsc->name, dsc->disposition);
545 			}
546 			break;
547 		}
548 		case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
549 		case MACH_MSG_OOL_DESCRIPTOR: {
550 			mach_msg_ool_descriptor_t *dsc =
551 			    (mach_msg_ool_descriptor_t *)&daddr->out_of_line;
552 
553 			if (dsc->size > 0) {
554 				vm_map_copy_discard((vm_map_copy_t) dsc->address);
555 			} else {
556 				assert(dsc->address == (void *) 0);
557 			}
558 			break;
559 		}
560 		case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
561 			ipc_object_t                    *objects;
562 			mach_msg_type_number_t          j;
563 			mach_msg_ool_ports_descriptor_t *dsc;
564 
565 			dsc = (mach_msg_ool_ports_descriptor_t  *)&daddr->ool_ports;
566 			objects = (ipc_object_t *) dsc->address;
567 
568 			if (dsc->count == 0) {
569 				break;
570 			}
571 			assert(objects != 0);
572 			for (j = 0; j < dsc->count; j++) {
573 				object = objects[j];
574 				if (IO_VALID(object)) {
575 					ipc_object_destroy(object, dsc->disposition);
576 				}
577 			}
578 			kfree_type(mach_port_t, dsc->count, dsc->address);
579 			break;
580 		}
581 		case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
582 			mach_msg_guarded_port_descriptor_t *dsc = (mach_msg_guarded_port_descriptor_t *)&daddr->guarded_port;
583 			if (IO_VALID((ipc_object_t) dsc->name)) {
584 				ipc_object_destroy((ipc_object_t) dsc->name, dsc->disposition);
585 			}
586 			break;
587 		}
588 		default:
589 			break;
590 		}
591 	}
592 }
593 
594 /************** These Calls are set up for kernel-loaded tasks/threads **************/
595 
596 /*
597  *	Routine:	mig_get_reply_port
598  *	Purpose:
599  *		Called by client side interfaces living in the kernel
600  *		to get a reply port.
601  */
602 mach_port_t
mig_get_reply_port(void)603 mig_get_reply_port(void)
604 {
605 	return MACH_PORT_NULL;
606 }
607 
608 /*
609  *	Routine:	mig_dealloc_reply_port
610  *	Purpose:
611  *		Called by client side interfaces to get rid of a reply port.
612  */
613 
614 void
mig_dealloc_reply_port(__unused mach_port_t reply_port)615 mig_dealloc_reply_port(
616 	__unused mach_port_t reply_port)
617 {
618 }
619 
620 /*
621  *	Routine:	mig_put_reply_port
622  *	Purpose:
623  *		Called by client side interfaces after each RPC to
624  *		let the client recycle the reply port if it wishes.
625  */
626 void
mig_put_reply_port(__unused mach_port_t reply_port)627 mig_put_reply_port(
628 	__unused mach_port_t reply_port)
629 {
630 }
631 
632 /*
633  * mig_strncpy.c - by Joshua Block
634  *
635  * mig_strncp -- Bounded string copy.  Does what the library routine strncpy
636  * OUGHT to do:  Copies the (null terminated) string in src into dest, a
637  * buffer of length len.  Assures that the copy is still null terminated
638  * and doesn't overflow the buffer, truncating the copy if necessary.
639  *
640  * Parameters:
641  *
642  *     dest - Pointer to destination buffer.
643  *
644  *     src - Pointer to source string.
645  *
646  *     len - Length of destination buffer.
647  */
648 int
mig_strncpy(char * dest,const char * src,int len)649 mig_strncpy(
650 	char            *dest,
651 	const char      *src,
652 	int             len)
653 {
654 	int i = 0;
655 
656 	if (len > 0) {
657 		if (dest != NULL) {
658 			if (src != NULL) {
659 				for (i = 1; i < len; i++) {
660 					if (!(*dest++ = *src++)) {
661 						return i;
662 					}
663 				}
664 			}
665 			*dest = '\0';
666 		}
667 	}
668 	return i;
669 }
670 
671 /*
672  * mig_strncpy_zerofill -- Bounded string copy.  Does what the
673  * library routine strncpy OUGHT to do:  Copies the (null terminated)
674  * string in src into dest, a buffer of length len.  Assures that
675  * the copy is still null terminated and doesn't overflow the buffer,
676  * truncating the copy if necessary. If the string in src is smaller
677  * than given length len, it will zero fill the remaining bytes in dest.
678  *
679  * Parameters:
680  *
681  *     dest - Pointer to destination buffer.
682  *
683  *     src - Pointer to source string.
684  *
685  *     len - Length of destination buffer.
686  */
687 int
mig_strncpy_zerofill(char * dest,const char * src,int len)688 mig_strncpy_zerofill(
689 	char            *dest,
690 	const char      *src,
691 	int             len)
692 {
693 	int i = 0;
694 	boolean_t terminated = FALSE;
695 	int retval = 0;
696 
697 	if (len <= 0 || dest == NULL) {
698 		return 0;
699 	}
700 
701 	if (src == NULL) {
702 		terminated = TRUE;
703 	}
704 
705 	for (i = 1; i < len; i++) {
706 		if (!terminated) {
707 			if (!(*dest++ = *src++)) {
708 				retval = i;
709 				terminated = TRUE;
710 			}
711 		} else {
712 			*dest++ = '\0';
713 		}
714 	}
715 
716 	*dest = '\0';
717 	if (!terminated) {
718 		retval = i;
719 	}
720 
721 	return retval;
722 }
723 
724 void *
mig_user_allocate(vm_size_t size)725 mig_user_allocate(
726 	vm_size_t       size)
727 {
728 	return kalloc_data(size, Z_WAITOK);
729 }
730 
731 void
mig_user_deallocate(char * data,vm_size_t size)732 mig_user_deallocate(
733 	char            *data,
734 	vm_size_t       size)
735 {
736 	kfree_data(data, size);
737 }
738