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