]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/mach_msg.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / ipc / mach_msg.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
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.
0a7de745 41 *
1c79356b
A
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.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
2d21ac55
A
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 */
1c79356b
A
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
91447636 73#include <mach/mach_types.h>
1c79356b
A
74#include <mach/kern_return.h>
75#include <mach/port.h>
76#include <mach/message.h>
77#include <mach/mig_errors.h>
91447636
A
78#include <mach/mach_traps.h>
79
80#include <kern/kern_types.h>
1c79356b 81#include <kern/assert.h>
1c79356b 82#include <kern/cpu_number.h>
91447636
A
83#include <kern/ipc_kobject.h>
84#include <kern/ipc_mig.h>
1c79356b
A
85#include <kern/task.h>
86#include <kern/thread.h>
1c79356b
A
87#include <kern/sched_prim.h>
88#include <kern/exception.h>
89#include <kern/misc_protos.h>
91447636
A
90#include <kern/processor.h>
91#include <kern/syscall_subr.h>
5ba3f43e 92#include <kern/policy_internal.h>
f427ee49 93#include <kern/mach_filter.h>
91447636 94
1c79356b 95#include <vm/vm_map.h>
91447636 96
d9a64523 97#include <ipc/port.h>
91447636 98#include <ipc/ipc_types.h>
1c79356b
A
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>
fe8ab488 107#include <ipc/ipc_importance.h>
490019cf 108#include <ipc/ipc_voucher.h>
0b4e3aa0 109
1c79356b 110#include <machine/machine_routines.h>
2d21ac55 111#include <security/mac_mach_internal.h>
91447636 112
1c79356b
A
113#include <sys/kdebug.h>
114
91447636
A
115#ifndef offsetof
116#define offsetof(type, member) ((size_t)(&((type *)0)->member))
117#endif /* offsetof */
118
1c79356b 119/*
91447636 120 * Forward declarations - kernel internal routines
1c79356b
A
121 */
122
123mach_msg_return_t mach_msg_send(
0a7de745
A
124 mach_msg_header_t *msg,
125 mach_msg_option_t option,
126 mach_msg_size_t send_size,
127 mach_msg_timeout_t send_timeout,
128 mach_port_name_t notify);
1c79356b
A
129
130mach_msg_return_t mach_msg_receive(
0a7de745
A
131 mach_msg_header_t *msg,
132 mach_msg_option_t option,
133 mach_msg_size_t rcv_size,
134 mach_port_name_t rcv_name,
135 mach_msg_timeout_t rcv_timeout,
136 void (*continuation)(mach_msg_return_t),
137 mach_msg_size_t slist_size);
1c79356b
A
138
139
140mach_msg_return_t msg_receive_error(
0a7de745
A
141 ipc_kmsg_t kmsg,
142 mach_msg_option_t option,
143 mach_vm_address_t rcv_addr,
144 mach_msg_size_t rcv_size,
145 mach_port_seqno_t seqno,
146 ipc_space_t space,
147 mach_msg_size_t *out_size);
1c79356b 148
5ba3f43e
A
149static mach_msg_return_t
150mach_msg_rcv_link_special_reply_port(
151 ipc_port_t special_reply_port,
152 mach_port_name_t dest_name_port);
153
d9a64523
A
154void
155mach_msg_receive_results_complete(ipc_object_t object);
5ba3f43e 156
cb323159
A
157const security_token_t KERNEL_SECURITY_TOKEN = KERNEL_SECURITY_TOKEN_VALUE;
158const audit_token_t KERNEL_AUDIT_TOKEN = KERNEL_AUDIT_TOKEN_VALUE;
1c79356b 159
f427ee49
A
160const mach_msg_max_trailer_t trailer_template = {
161 .msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0,
162 .msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE,
163 .msgh_sender = KERNEL_SECURITY_TOKEN_VALUE,
164 .msgh_audit = KERNEL_AUDIT_TOKEN_VALUE
1c79356b
A
165};
166
167/*
39236c6e 168 * Routine: mach_msg_send [Kernel Internal]
1c79356b 169 * Purpose:
39236c6e
A
170 * Routine for kernel-task threads to send a message.
171 *
172 * Unlike mach_msg_send_from_kernel(), this routine
173 * looks port names up in the kernel's port namespace
174 * and copies in the kernel virtual memory (instead
175 * of taking a vm_map_copy_t pointer for OOL descriptors).
1c79356b
A
176 * Conditions:
177 * Nothing locked.
178 * Returns:
179 * MACH_MSG_SUCCESS Sent the message.
180 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
181 * MACH_SEND_NO_BUFFER Couldn't allocate buffer.
182 * MACH_SEND_INVALID_DATA Couldn't copy message data.
183 * MACH_SEND_INVALID_HEADER
184 * Illegal value in the message header bits.
185 * MACH_SEND_INVALID_DEST The space is dead.
186 * MACH_SEND_INVALID_NOTIFY Bad notify port.
187 * MACH_SEND_INVALID_DEST Can't copyin destination port.
188 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
189 * MACH_SEND_TIMED_OUT Timeout expired without delivery.
190 * MACH_SEND_INTERRUPTED Delivery interrupted.
1c79356b
A
191 */
192
193mach_msg_return_t
194mach_msg_send(
0a7de745
A
195 mach_msg_header_t *msg,
196 mach_msg_option_t option,
197 mach_msg_size_t send_size,
198 mach_msg_timeout_t send_timeout,
f427ee49 199 mach_msg_priority_t priority)
1c79356b
A
200{
201 ipc_space_t space = current_space();
202 vm_map_t map = current_map();
203 ipc_kmsg_t kmsg;
204 mach_msg_return_t mr;
0a7de745
A
205 mach_msg_size_t msg_and_trailer_size;
206 mach_msg_max_trailer_t *trailer;
1c79356b 207
d9a64523
A
208 option |= MACH_SEND_KERNEL;
209
5ba3f43e
A
210 if ((send_size & 3) ||
211 send_size < sizeof(mach_msg_header_t) ||
0a7de745 212 (send_size < sizeof(mach_msg_base_t) && (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX))) {
91447636 213 return MACH_SEND_MSG_TOO_SMALL;
0a7de745 214 }
1c79356b 215
0a7de745 216 if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE) {
8ad349bb 217 return MACH_SEND_TOO_LARGE;
0a7de745
A
218 }
219
220 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
39037602 221
91447636
A
222 msg_and_trailer_size = send_size + MAX_TRAILER_SIZE;
223
224 kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
225
39037602 226 if (kmsg == IKM_NULL) {
0a7de745 227 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_NO_BUFFER);
91447636 228 return MACH_SEND_NO_BUFFER;
39037602 229 }
91447636 230
0a7de745
A
231 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
232 (uintptr_t)0, /* this should only be called from the kernel! */
233 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
234 0, 0,
235 0);
91447636 236 (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size);
1c79356b 237
91447636
A
238 kmsg->ikm_header->msgh_size = send_size;
239
0a7de745 240 /*
91447636
A
241 * reserve for the trailer the largest space (MAX_TRAILER_SIZE)
242 * However, the internal size field of the trailer (msgh_trailer_size)
243 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
244 * the cases where no implicit data is requested.
245 */
246 trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size);
f427ee49 247 bzero(trailer, sizeof(*trailer));
91447636
A
248 trailer->msgh_sender = current_thread()->task->sec_token;
249 trailer->msgh_audit = current_thread()->task->audit_token;
250 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
251 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
39236c6e 252
f427ee49 253 mr = ipc_kmsg_copyin(kmsg, space, map, priority, &option);
39236c6e 254
1c79356b
A
255 if (mr != MACH_MSG_SUCCESS) {
256 ipc_kmsg_free(kmsg);
0a7de745 257 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
1c79356b
A
258 return mr;
259 }
260
39236c6e
A
261 mr = ipc_kmsg_send(kmsg, option, send_timeout);
262
1c79356b 263 if (mr != MACH_MSG_SUCCESS) {
0a7de745
A
264 mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL);
265 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header,
266 kmsg->ikm_header->msgh_size);
267 ipc_kmsg_free(kmsg);
268 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
1c79356b
A
269 }
270
271 return mr;
272}
273
0a7de745 274/*
39236c6e
A
275 * message header as seen at user-space
276 * (for MACH_RCV_LARGE/IDENTITY updating)
277 */
0a7de745
A
278typedef struct{
279 mach_msg_bits_t msgh_bits;
280 mach_msg_size_t msgh_size;
281 mach_port_name_t msgh_remote_port;
282 mach_port_name_t msgh_local_port;
283 mach_msg_size_t msgh_reserved;
284 mach_msg_id_t msgh_id;
39236c6e
A
285} mach_msg_user_header_t;
286
1c79356b 287/*
6d2010ae 288 * Routine: mach_msg_receive_results
1c79356b
A
289 * Purpose:
290 * Receive a message.
291 * Conditions:
292 * Nothing locked.
293 * Returns:
294 * MACH_MSG_SUCCESS Received a message.
295 * MACH_RCV_INVALID_NAME The name doesn't denote a right,
296 * or the denoted right is not receive or port set.
297 * MACH_RCV_IN_SET Receive right is a member of a set.
298 * MACH_RCV_TOO_LARGE Message wouldn't fit into buffer.
299 * MACH_RCV_TIMED_OUT Timeout expired without a message.
300 * MACH_RCV_INTERRUPTED Reception interrupted.
301 * MACH_RCV_PORT_DIED Port/set died while receiving.
302 * MACH_RCV_PORT_CHANGED Port moved into set while receiving.
303 * MACH_RCV_INVALID_DATA Couldn't copy to user buffer.
304 * MACH_RCV_INVALID_NOTIFY Bad notify port.
305 * MACH_RCV_HEADER_ERROR
306 */
307
308mach_msg_return_t
39037602
A
309mach_msg_receive_results(
310 mach_msg_size_t *sizep)
1c79356b
A
311{
312 thread_t self = current_thread();
313 ipc_space_t space = current_space();
316670eb 314 vm_map_t map = current_map();
1c79356b
A
315
316 ipc_object_t object = self->ith_object;
317 mach_msg_return_t mr = self->ith_state;
39037602
A
318 mach_vm_address_t rcv_addr = self->ith_msg_addr;
319 mach_msg_size_t rcv_size = self->ith_rsize;
1c79356b
A
320 mach_msg_option_t option = self->ith_option;
321 ipc_kmsg_t kmsg = self->ith_kmsg;
322 mach_port_seqno_t seqno = self->ith_seqno;
39037602 323
316670eb 324 mach_msg_trailer_size_t trailer_size;
f427ee49 325 mach_vm_address_t context;
39037602 326 mach_msg_size_t size = 0;
1c79356b 327
d9a64523
A
328 /*
329 * unlink the special_reply_port before releasing reference to object.
330 * get the thread's turnstile, if the thread donated it's turnstile to the port
331 */
332 mach_msg_receive_results_complete(object);
316670eb 333 io_release(object);
1c79356b
A
334
335 if (mr != MACH_MSG_SUCCESS) {
39037602 336 if (mr == MACH_RCV_TOO_LARGE) {
39037602
A
337 /*
338 * If the receive operation occurs with MACH_RCV_LARGE set
339 * then no message was extracted from the queue, and the size
340 * and (optionally) receiver names were the only thing captured.
341 * Just copyout the size (and optional port name) in a fake
342 * header.
343 */
344 if (option & MACH_RCV_LARGE) {
39037602
A
345 if ((option & MACH_RCV_STACK) == 0 &&
346 rcv_size >= offsetof(mach_msg_user_header_t, msgh_reserved)) {
39037602
A
347 /*
348 * We need to inform the user-level code that it needs more
349 * space. The value for how much space was returned in the
350 * msize save area instead of the message (which was left on
351 * the queue).
352 */
353 if (option & MACH_RCV_LARGE_IDENTITY) {
354 if (copyout((char *) &self->ith_receiver_name,
0a7de745
A
355 rcv_addr + offsetof(mach_msg_user_header_t, msgh_local_port),
356 sizeof(mach_port_name_t))) {
39037602 357 mr = MACH_RCV_INVALID_DATA;
0a7de745 358 }
39037602
A
359 }
360 if (copyout((char *) &self->ith_msize,
0a7de745
A
361 rcv_addr + offsetof(mach_msg_user_header_t, msgh_size),
362 sizeof(mach_msg_size_t))) {
39037602 363 mr = MACH_RCV_INVALID_DATA;
0a7de745 364 }
39037602
A
365 }
366 } else {
39037602
A
367 /* discard importance in message */
368 ipc_importance_clean(kmsg);
369
370 if (msg_receive_error(kmsg, option, rcv_addr, rcv_size, seqno, space, &size)
0a7de745 371 == MACH_RCV_INVALID_DATA) {
39037602 372 mr = MACH_RCV_INVALID_DATA;
0a7de745 373 }
39037602
A
374 }
375 }
376
0a7de745 377 if (sizep) {
39037602 378 *sizep = size;
0a7de745 379 }
39037602 380 return mr;
1c79356b
A
381 }
382
39037602
A
383 /* MACH_MSG_SUCCESS */
384
39236c6e 385#if IMPORTANCE_INHERITANCE
39236c6e 386
fe8ab488
A
387 /* adopt/transform any importance attributes carried in the message */
388 ipc_importance_receive(kmsg, option);
389
39236c6e
A
390#endif /* IMPORTANCE_INHERITANCE */
391
490019cf
A
392 /* auto redeem the voucher in the message */
393 ipc_voucher_receive_postprocessing(kmsg, option);
394
f427ee49
A
395 /* Save destination port context for the trailer before copyout */
396 context = kmsg->ikm_header->msgh_remote_port->ip_context;
0a7de745 397
3e170ce0 398 mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL, option);
1c79356b 399
f427ee49
A
400 trailer_size = ipc_kmsg_trailer_size(option, self);
401
1c79356b 402 if (mr != MACH_MSG_SUCCESS) {
fe8ab488
A
403 /* already received importance, so have to undo that here */
404 ipc_importance_unreceive(kmsg, option);
405
39037602 406 /* if we had a body error copyout what we have, otherwise a simple header/trailer */
0a7de745 407 if ((mr & ~MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
f427ee49 408 ipc_kmsg_add_trailer(kmsg, space, option, self, seqno, FALSE, context);
0a7de745 409 if (ipc_kmsg_put(kmsg, option, rcv_addr, rcv_size, trailer_size, &size) == MACH_RCV_INVALID_DATA) {
1c79356b 410 mr = MACH_RCV_INVALID_DATA;
0a7de745 411 }
39037602 412 } else {
0a7de745
A
413 if (msg_receive_error(kmsg, option, rcv_addr, rcv_size, seqno, space, &size)
414 == MACH_RCV_INVALID_DATA) {
1c79356b 415 mr = MACH_RCV_INVALID_DATA;
0a7de745 416 }
1c79356b 417 }
fe8ab488 418 } else {
39037602 419 /* capture ksmg QoS values to the thread continuation state */
f427ee49 420 self->ith_ppriority = kmsg->ikm_ppriority;
39037602 421 self->ith_qos_override = kmsg->ikm_qos_override;
f427ee49 422 ipc_kmsg_add_trailer(kmsg, space, option, self, seqno, FALSE, context);
39037602 423 mr = ipc_kmsg_put(kmsg, option, rcv_addr, rcv_size, trailer_size, &size);
1c79356b 424 }
fe8ab488 425
0a7de745 426 if (sizep) {
39037602 427 *sizep = size;
0a7de745 428 }
1c79356b
A
429 return mr;
430}
431
39236c6e
A
432/*
433 * Routine: mach_msg_receive [Kernel Internal]
434 * Purpose:
435 * Routine for kernel-task threads to actively receive a message.
436 *
437 * Unlike being dispatched to by ipc_kobject_server() or the
438 * reply part of mach_msg_rpc_from_kernel(), this routine
439 * looks up the receive port name in the kernel's port
d9a64523 440 * namespace and copies out received port rights to that namespace
39236c6e
A
441 * as well. Out-of-line memory is copied out the kernel's
442 * address space (rather than just providing the vm_map_copy_t).
443 * Conditions:
444 * Nothing locked.
445 * Returns:
446 * MACH_MSG_SUCCESS Received a message.
447 * See <mach/message.h> for list of MACH_RCV_XXX errors.
448 */
1c79356b
A
449mach_msg_return_t
450mach_msg_receive(
0a7de745
A
451 mach_msg_header_t *msg,
452 mach_msg_option_t option,
453 mach_msg_size_t rcv_size,
454 mach_port_name_t rcv_name,
455 mach_msg_timeout_t rcv_timeout,
456 void (*continuation)(mach_msg_return_t),
3e170ce0 457 __unused mach_msg_size_t slist_size)
1c79356b
A
458{
459 thread_t self = current_thread();
460 ipc_space_t space = current_space();
1c79356b
A
461 ipc_object_t object;
462 ipc_mqueue_t mqueue;
1c79356b 463 mach_msg_return_t mr;
1c79356b
A
464
465 mr = ipc_mqueue_copyin(space, rcv_name, &mqueue, &object);
0a7de745 466 if (mr != MACH_MSG_SUCCESS) {
1c79356b
A
467 return mr;
468 }
469 /* hold ref for object */
470
91447636 471 self->ith_msg_addr = CAST_DOWN(mach_vm_address_t, msg);
1c79356b 472 self->ith_object = object;
39037602
A
473 self->ith_rsize = rcv_size;
474 self->ith_msize = 0;
1c79356b 475 self->ith_option = option;
1c79356b 476 self->ith_continuation = continuation;
5ba3f43e 477 self->ith_knote = ITH_KNOTE_NULL;
1c79356b 478
91447636 479 ipc_mqueue_receive(mqueue, option, rcv_size, rcv_timeout, THREAD_ABORTSAFE);
0a7de745 480 if ((option & MACH_RCV_TIMEOUT) && rcv_timeout == 0) {
91447636 481 thread_poll_yield(self);
0a7de745 482 }
39037602 483 return mach_msg_receive_results(NULL);
1c79356b
A
484}
485
486void
487mach_msg_receive_continue(void)
488{
39037602 489 mach_msg_return_t mr;
1c79356b
A
490 thread_t self = current_thread();
491
f427ee49 492 ipc_port_thread_group_unblocked();
0a7de745 493 if (self->ith_state == MACH_PEEK_READY) {
39037602 494 mr = MACH_PEEK_READY;
0a7de745 495 } else {
39037602 496 mr = mach_msg_receive_results(NULL);
0a7de745 497 }
39037602 498 (*self->ith_continuation)(mr);
1c79356b
A
499}
500
1c79356b
A
501
502/*
503 * Routine: mach_msg_overwrite_trap [mach trap]
504 * Purpose:
505 * Possibly send a message; possibly receive a message.
506 * Conditions:
507 * Nothing locked.
508 * Returns:
509 * All of mach_msg_send and mach_msg_receive error codes.
510 */
511
512mach_msg_return_t
513mach_msg_overwrite_trap(
91447636 514 struct mach_msg_overwrite_trap_args *args)
1c79356b 515{
0a7de745
A
516 mach_vm_address_t msg_addr = args->msg;
517 mach_msg_option_t option = args->option;
518 mach_msg_size_t send_size = args->send_size;
519 mach_msg_size_t rcv_size = args->rcv_size;
520 mach_port_name_t rcv_name = args->rcv_name;
521 mach_msg_timeout_t msg_timeout = args->timeout;
f427ee49 522 mach_msg_priority_t priority = args->priority;
0a7de745 523 mach_vm_address_t rcv_msg_addr = args->rcv_msg;
2d21ac55 524 __unused mach_port_seqno_t temp_seqno = 0;
91447636 525
1c79356b 526 mach_msg_return_t mr = MACH_MSG_SUCCESS;
b0d623f7 527 vm_map_t map = current_map();
39236c6e
A
528
529 /* Only accept options allowed by the user */
530 option &= MACH_MSG_OPTION_USER;
531
1c79356b 532 if (option & MACH_SEND_MSG) {
91447636 533 ipc_space_t space = current_space();
91447636
A
534 ipc_kmsg_t kmsg;
535
0a7de745 536 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
39037602 537
91447636
A
538 mr = ipc_kmsg_get(msg_addr, send_size, &kmsg);
539
39037602 540 if (mr != MACH_MSG_SUCCESS) {
0a7de745 541 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
91447636 542 return mr;
39037602 543 }
91447636 544
0a7de745
A
545 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
546 (uintptr_t)msg_addr,
547 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
548 0, 0,
549 0);
39037602 550
f427ee49 551 mr = ipc_kmsg_copyin(kmsg, space, map, priority, &option);
39236c6e 552
1c79356b 553 if (mr != MACH_MSG_SUCCESS) {
91447636 554 ipc_kmsg_free(kmsg);
0a7de745 555 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
f427ee49 556 goto end;
1c79356b 557 }
91447636 558
39236c6e 559 mr = ipc_kmsg_send(kmsg, option, msg_timeout);
91447636
A
560
561 if (mr != MACH_MSG_SUCCESS) {
562 mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL);
39037602 563 (void) ipc_kmsg_put(kmsg, option, msg_addr, send_size, 0, NULL);
0a7de745 564 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
f427ee49 565 goto end;
91447636 566 }
1c79356b
A
567 }
568
569 if (option & MACH_RCV_MSG) {
91447636
A
570 thread_t self = current_thread();
571 ipc_space_t space = current_space();
572 ipc_object_t object;
573 ipc_mqueue_t mqueue;
574
575 mr = ipc_mqueue_copyin(space, rcv_name, &mqueue, &object);
576 if (mr != MACH_MSG_SUCCESS) {
f427ee49 577 goto end;
91447636
A
578 }
579 /* hold ref for object */
1c79356b 580
5ba3f43e
A
581 if ((option & MACH_RCV_SYNC_WAIT) && !(option & MACH_SEND_SYNC_OVERRIDE)) {
582 ipc_port_t special_reply_port;
cb323159 583 special_reply_port = ip_object_to_port(object);
5ba3f43e
A
584 /* link the special reply port to the destination */
585 mr = mach_msg_rcv_link_special_reply_port(special_reply_port,
f427ee49 586 (mach_port_name_t)priority);
5ba3f43e
A
587 if (mr != MACH_MSG_SUCCESS) {
588 io_release(object);
f427ee49 589 goto end;
5ba3f43e
A
590 }
591 }
592
0a7de745 593 if (rcv_msg_addr != (mach_vm_address_t)0) {
91447636 594 self->ith_msg_addr = rcv_msg_addr;
0a7de745 595 } else {
91447636 596 self->ith_msg_addr = msg_addr;
0a7de745 597 }
91447636 598 self->ith_object = object;
39037602
A
599 self->ith_rsize = rcv_size;
600 self->ith_msize = 0;
91447636 601 self->ith_option = option;
b0d623f7 602 self->ith_receiver_name = MACH_PORT_NULL;
91447636 603 self->ith_continuation = thread_syscall_return;
5ba3f43e 604 self->ith_knote = ITH_KNOTE_NULL;
91447636
A
605
606 ipc_mqueue_receive(mqueue, option, rcv_size, msg_timeout, THREAD_ABORTSAFE);
0a7de745 607 if ((option & MACH_RCV_TIMEOUT) && msg_timeout == 0) {
91447636 608 thread_poll_yield(self);
0a7de745 609 }
f427ee49
A
610 mr = mach_msg_receive_results(NULL);
611 goto end;
1c79356b
A
612 }
613
f427ee49
A
614end:
615 ipc_port_thread_group_unblocked();
616 return mr;
1c79356b
A
617}
618
5ba3f43e
A
619/*
620 * Routine: mach_msg_rcv_link_special_reply_port
621 * Purpose:
622 * Link the special reply port(rcv right) to the
623 * other end of the sync ipc channel.
624 * Conditions:
625 * Nothing locked.
626 * Returns:
627 * None.
628 */
629static mach_msg_return_t
630mach_msg_rcv_link_special_reply_port(
631 ipc_port_t special_reply_port,
632 mach_port_name_t dest_name_port)
633{
634 ipc_port_t dest_port = IP_NULL;
635 kern_return_t kr;
5ba3f43e
A
636
637 if (current_thread()->ith_special_reply_port != special_reply_port) {
638 return MACH_RCV_INVALID_NOTIFY;
639 }
640
641 /* Copyin the destination port */
642 if (!MACH_PORT_VALID(dest_name_port)) {
643 return MACH_RCV_INVALID_NOTIFY;
644 }
645
cb323159
A
646 kr = ipc_port_translate_send(current_space(), dest_name_port, &dest_port);
647 if (kr == KERN_SUCCESS) {
648 ip_reference(dest_port);
649 ip_unlock(dest_port);
5ba3f43e 650
cb323159
A
651 /*
652 * The receive right of dest port might have gone away,
653 * do not fail the receive in that case.
654 */
d9a64523 655 ipc_port_link_special_reply_port(special_reply_port,
cb323159 656 dest_port, FALSE);
5ba3f43e 657
cb323159 658 ip_release(dest_port);
5ba3f43e
A
659 }
660 return MACH_MSG_SUCCESS;
661}
662
663/*
d9a64523 664 * Routine: mach_msg_receive_results_complete
5ba3f43e 665 * Purpose:
d9a64523
A
666 * Get thread's turnstile back from the object and
667 * if object is a special reply port then reset its
668 * linkage.
5ba3f43e
A
669 * Condition:
670 * Nothing locked.
671 * Returns:
672 * None.
673 */
d9a64523
A
674void
675mach_msg_receive_results_complete(ipc_object_t object)
5ba3f43e
A
676{
677 thread_t self = current_thread();
d9a64523 678 ipc_port_t port = IPC_PORT_NULL;
94ff46dc 679 boolean_t get_turnstile = (self->turnstile == TURNSTILE_NULL);
5ba3f43e 680
d9a64523 681 if (io_otype(object) == IOT_PORT) {
cb323159 682 port = ip_object_to_port(object);
d9a64523
A
683 } else {
684 assert(self->turnstile != TURNSTILE_NULL);
5ba3f43e
A
685 return;
686 }
687
d9a64523
A
688 uint8_t flags = IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE;
689
690 /*
691 * Don't clear the ip_srp_msg_sent bit if...
692 */
693 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
0a7de745
A
694 self->ith_state == MACH_RCV_INTERRUPTED ||
695 self->ith_state == MACH_RCV_TIMED_OUT ||
696 self->ith_state == MACH_RCV_PORT_CHANGED ||
697 self->ith_state == MACH_PEEK_READY)) {
d9a64523
A
698 flags |= IPC_PORT_ADJUST_SR_RECEIVED_MSG;
699 }
700
94ff46dc
A
701 if (port->ip_specialreply || get_turnstile) {
702 ip_lock(port);
703 ipc_port_adjust_special_reply_port_locked(port, NULL,
704 flags, get_turnstile);
705 }
706 assert(self->turnstile != TURNSTILE_NULL);
d9a64523 707 /* thread now has a turnstile */
5ba3f43e
A
708}
709
9bccf70c
A
710/*
711 * Routine: mach_msg_trap [mach trap]
712 * Purpose:
713 * Possibly send a message; possibly receive a message.
714 * Conditions:
715 * Nothing locked.
716 * Returns:
717 * All of mach_msg_send and mach_msg_receive error codes.
718 */
719
720mach_msg_return_t
721mach_msg_trap(
91447636 722 struct mach_msg_overwrite_trap_args *args)
9bccf70c 723{
91447636
A
724 kern_return_t kr;
725 args->rcv_msg = (mach_vm_address_t)0;
726
0a7de745 727 kr = mach_msg_overwrite_trap(args);
91447636 728 return kr;
9bccf70c 729}
0a7de745 730
9bccf70c 731
1c79356b
A
732/*
733 * Routine: msg_receive_error [internal]
734 * Purpose:
735 * Builds a minimal header/trailer and copies it to
736 * the user message buffer. Invoked when in the case of a
737 * MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error.
738 * Conditions:
739 * Nothing locked.
39037602
A
740 * size - maximum buffer size on input,
741 * actual copied-out size on output
1c79356b
A
742 * Returns:
743 * MACH_MSG_SUCCESS minimal header/trailer copied
744 * MACH_RCV_INVALID_DATA copyout to user buffer failed
745 */
0a7de745 746
1c79356b
A
747mach_msg_return_t
748msg_receive_error(
0a7de745
A
749 ipc_kmsg_t kmsg,
750 mach_msg_option_t option,
751 mach_vm_address_t rcv_addr,
752 mach_msg_size_t rcv_size,
753 mach_port_seqno_t seqno,
754 ipc_space_t space,
755 mach_msg_size_t *sizep)
1c79356b 756{
0a7de745 757 mach_vm_address_t context;
316670eb 758 mach_msg_trailer_size_t trailer_size;
0a7de745 759 mach_msg_max_trailer_t *trailer;
f427ee49 760 thread_t self = current_thread();
b0d623f7
A
761
762 context = kmsg->ikm_header->msgh_remote_port->ip_context;
1c79356b
A
763
764 /*
765 * Copy out the destination port in the message.
0a7de745 766 * Destroy all other rights and memory in the message.
1c79356b
A
767 */
768 ipc_kmsg_copyout_dest(kmsg, space);
769
770 /*
771 * Build a minimal message with the requested trailer.
772 */
0a7de745
A
773 trailer = (mach_msg_max_trailer_t *)
774 ((vm_offset_t)kmsg->ikm_header +
eb6b6ca3 775 mach_round_msg(sizeof(mach_msg_header_t)));
91447636 776 kmsg->ikm_header->msgh_size = sizeof(mach_msg_header_t);
f427ee49 777 bcopy((const char *)&trailer_template,
0a7de745
A
778 (char *)trailer,
779 sizeof(trailer_template));
316670eb 780
f427ee49
A
781 trailer_size = ipc_kmsg_trailer_size(option, self);
782 ipc_kmsg_add_trailer(kmsg, space, option, self,
783 seqno, TRUE, context);
1c79356b
A
784
785 /*
39037602
A
786 * Copy the message to user space and return the size
787 * (note that ipc_kmsg_put may also adjust the actual
788 * size copied out to user-space).
1c79356b 789 */
0a7de745
A
790 if (ipc_kmsg_put(kmsg, option, rcv_addr, rcv_size, trailer_size, sizep) == MACH_RCV_INVALID_DATA) {
791 return MACH_RCV_INVALID_DATA;
792 } else {
793 return MACH_MSG_SUCCESS;
794 }
1c79356b 795}
f427ee49 796
c3c9b80d 797static SECURITY_READ_ONLY_LATE(mach_msg_fetch_filter_policy_cbfunc_t) mach_msg_fetch_filter_policy_callback = NULL;
f427ee49
A
798
799kern_return_t
800mach_msg_filter_register_callback(
801 const struct mach_msg_filter_callbacks *callbacks)
802{
803 if (callbacks == NULL) {
804 return KERN_INVALID_ARGUMENT;
805 }
806
807 if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_0) {
808 if (mach_msg_fetch_filter_policy_callback != NULL) {
809 return KERN_FAILURE;
810 }
811 mach_msg_fetch_filter_policy_callback = callbacks->fetch_filter_policy;
812 }
813
814 return KERN_SUCCESS;
815}
816
817/* This function should only be called if the task and port allow message filtering */
818boolean_t
819mach_msg_fetch_filter_policy(
820 void *port_label,
821 mach_msg_id_t msgh_id,
822 mach_msg_filter_id *fid)
823{
824 boolean_t ret = TRUE;
825
826 if (mach_msg_fetch_filter_policy_callback == NULL) {
827 *fid = MACH_MSG_FILTER_POLICY_ALLOW;
828 return true;
829 }
830 ret = mach_msg_fetch_filter_policy_callback(current_task(), port_label, msgh_id, fid);
831
832 return ret;
833}