]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39037602 | 2 | * Copyright (c) 2000-2016 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: kern/ipc_kobject.c | |
67 | * Author: Rich Draves | |
68 | * Date: 1989 | |
69 | * | |
70 | * Functions for letting a port represent a kernel object. | |
71 | */ | |
72 | ||
73 | #include <mach_debug.h> | |
74 | #include <mach_ipc_test.h> | |
9bccf70c | 75 | #include <mach/mig.h> |
1c79356b A |
76 | #include <mach/port.h> |
77 | #include <mach/kern_return.h> | |
78 | #include <mach/message.h> | |
79 | #include <mach/mig_errors.h> | |
80 | #include <mach/notify.h> | |
91447636 | 81 | #include <mach/ndr.h> |
b0d623f7 | 82 | #include <mach/vm_param.h> |
91447636 A |
83 | |
84 | #include <mach/mach_vm_server.h> | |
85 | #include <mach/mach_port_server.h> | |
86 | #include <mach/mach_host_server.h> | |
87 | #include <mach/host_priv_server.h> | |
88 | #include <mach/host_security_server.h> | |
89 | #include <mach/clock_server.h> | |
90 | #include <mach/clock_priv_server.h> | |
91447636 | 91 | #include <mach/lock_set_server.h> |
d9a64523 | 92 | #include <mach/memory_entry_server.h> |
91447636 A |
93 | #include <mach/memory_object_control_server.h> |
94 | #include <mach/memory_object_default_server.h> | |
91447636 A |
95 | #include <mach/processor_server.h> |
96 | #include <mach/processor_set_server.h> | |
91447636 | 97 | #include <mach/task_server.h> |
fe8ab488 A |
98 | #include <mach/mach_voucher_server.h> |
99 | #include <mach/mach_voucher_attr_control_server.h> | |
39037602 | 100 | #ifdef VM32_SUPPORT |
b0d623f7 A |
101 | #include <mach/vm32_map_server.h> |
102 | #endif | |
91447636 | 103 | #include <mach/thread_act_server.h> |
cb323159 | 104 | #include <mach/restartable_server.h> |
91447636 | 105 | |
d9a64523 A |
106 | #include <mach/exc_server.h> |
107 | #include <mach/mach_exc_server.h> | |
108 | ||
91447636 A |
109 | #include <device/device_types.h> |
110 | #include <device/device_server.h> | |
111 | ||
112 | #include <UserNotification/UNDReplyServer.h> | |
113 | ||
cb323159 A |
114 | #if CONFIG_ARCADE |
115 | #include <mach/arcade_register_server.h> | |
116 | #endif | |
117 | ||
0a7de745 | 118 | #if CONFIG_AUDIT |
b0d623f7 A |
119 | #include <kern/audit_sessionport.h> |
120 | #endif | |
121 | ||
91447636 A |
122 | #if MACH_MACHINE_ROUTINES |
123 | #include <machine/machine_routines.h> | |
0a7de745 A |
124 | #endif /* MACH_MACHINE_ROUTINES */ |
125 | #if XK_PROXY | |
91447636 | 126 | #include <uk_xkern/xk_uproxy_server.h> |
0a7de745 | 127 | #endif /* XK_PROXY */ |
9bccf70c | 128 | |
91447636 | 129 | #include <kern/ipc_tt.h> |
1c79356b | 130 | #include <kern/ipc_mig.h> |
d1ecb069 | 131 | #include <kern/ipc_misc.h> |
1c79356b | 132 | #include <kern/ipc_kobject.h> |
55e303ae | 133 | #include <kern/host_notify.h> |
1c79356b | 134 | #include <kern/mk_timer.h> |
55e303ae | 135 | #include <kern/misc_protos.h> |
cb323159 A |
136 | |
137 | #if CONFIG_ARCADE | |
138 | #include <kern/arcade.h> | |
139 | #endif /* CONFIG_ARCADE */ | |
140 | ||
1c79356b A |
141 | #include <ipc/ipc_kmsg.h> |
142 | #include <ipc/ipc_port.h> | |
fe8ab488 | 143 | #include <ipc/ipc_voucher.h> |
3e170ce0 | 144 | #include <kern/sync_sema.h> |
1c79356b | 145 | #include <kern/counters.h> |
5ba3f43e | 146 | #include <kern/work_interval.h> |
1c79356b | 147 | |
91447636 | 148 | #include <vm/vm_protos.h> |
1c79356b | 149 | |
2d21ac55 A |
150 | #include <security/mac_mach_internal.h> |
151 | ||
39236c6e A |
152 | extern char *proc_name_address(void *p); |
153 | extern int proc_pid(void *p); | |
154 | ||
1c79356b A |
155 | /* |
156 | * Routine: ipc_kobject_notify | |
157 | * Purpose: | |
158 | * Deliver notifications to kobjects that care about them. | |
159 | */ | |
160 | boolean_t | |
161 | ipc_kobject_notify( | |
0a7de745 A |
162 | mach_msg_header_t *request_header, |
163 | mach_msg_header_t *reply_header); | |
1c79356b | 164 | |
1c79356b | 165 | typedef struct { |
0a7de745 A |
166 | mach_msg_id_t num; |
167 | mig_routine_t routine; | |
1c79356b | 168 | int size; |
0a7de745 | 169 | #if MACH_COUNTERS |
1c79356b A |
170 | mach_counter_t callcount; |
171 | #endif | |
172 | } mig_hash_t; | |
173 | ||
fe8ab488 | 174 | #define MAX_MIG_ENTRIES 1031 |
1c79356b A |
175 | #define MIG_HASH(x) (x) |
176 | ||
177 | #ifndef max | |
0a7de745 | 178 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
1c79356b A |
179 | #endif /* max */ |
180 | ||
fe8ab488 A |
181 | static mig_hash_t mig_buckets[MAX_MIG_ENTRIES]; |
182 | static int mig_table_max_displ; | |
183 | static mach_msg_size_t mig_reply_size = sizeof(mig_reply_error_t); | |
1c79356b | 184 | |
1c79356b A |
185 | |
186 | ||
91447636 | 187 | const struct mig_subsystem *mig_e[] = { |
0a7de745 A |
188 | (const struct mig_subsystem *)&mach_vm_subsystem, |
189 | (const struct mig_subsystem *)&mach_port_subsystem, | |
190 | (const struct mig_subsystem *)&mach_host_subsystem, | |
191 | (const struct mig_subsystem *)&host_priv_subsystem, | |
192 | (const struct mig_subsystem *)&host_security_subsystem, | |
193 | (const struct mig_subsystem *)&clock_subsystem, | |
194 | (const struct mig_subsystem *)&clock_priv_subsystem, | |
195 | (const struct mig_subsystem *)&processor_subsystem, | |
196 | (const struct mig_subsystem *)&processor_set_subsystem, | |
197 | (const struct mig_subsystem *)&is_iokit_subsystem, | |
91447636 | 198 | (const struct mig_subsystem *)&lock_set_subsystem, |
91447636 A |
199 | (const struct mig_subsystem *)&task_subsystem, |
200 | (const struct mig_subsystem *)&thread_act_subsystem, | |
39037602 | 201 | #ifdef VM32_SUPPORT |
b0d623f7 A |
202 | (const struct mig_subsystem *)&vm32_map_subsystem, |
203 | #endif | |
91447636 | 204 | (const struct mig_subsystem *)&UNDReply_subsystem, |
fe8ab488 A |
205 | (const struct mig_subsystem *)&mach_voucher_subsystem, |
206 | (const struct mig_subsystem *)&mach_voucher_attr_control_subsystem, | |
d9a64523 | 207 | (const struct mig_subsystem *)&memory_entry_subsystem, |
cb323159 | 208 | (const struct mig_subsystem *)&task_restartable_subsystem, |
1c79356b A |
209 | |
210 | #if XK_PROXY | |
0a7de745 | 211 | (const struct mig_subsystem *)&do_uproxy_xk_uproxy_subsystem, |
1c79356b A |
212 | #endif /* XK_PROXY */ |
213 | #if MACH_MACHINE_ROUTINES | |
0a7de745 | 214 | (const struct mig_subsystem *)&MACHINE_SUBSYSTEM, |
1c79356b A |
215 | #endif /* MACH_MACHINE_ROUTINES */ |
216 | #if MCMSG && iPSC860 | |
91447636 | 217 | (const struct mig_subsystem *)&mcmsg_info_subsystem, |
1c79356b | 218 | #endif /* MCMSG && iPSC860 */ |
0a7de745 A |
219 | (const struct mig_subsystem *)&catch_exc_subsystem, |
220 | (const struct mig_subsystem *)&catch_mach_exc_subsystem, | |
cb323159 A |
221 | #if CONFIG_ARCADE |
222 | (const struct mig_subsystem *)&arcade_register_subsystem, | |
223 | #endif | |
1c79356b A |
224 | }; |
225 | ||
226 | void | |
227 | mig_init(void) | |
228 | { | |
0a7de745 A |
229 | unsigned int i, n = sizeof(mig_e) / sizeof(const struct mig_subsystem *); |
230 | int howmany; | |
231 | mach_msg_id_t j, pos, nentry, range; | |
232 | ||
233 | for (i = 0; i < n; i++) { | |
234 | range = mig_e[i]->end - mig_e[i]->start; | |
235 | if (!mig_e[i]->start || range < 0) { | |
236 | panic("the msgh_ids in mig_e[] aren't valid!"); | |
237 | } | |
238 | ||
239 | for (j = 0; j < range; j++) { | |
240 | if (mig_e[i]->routine[j].stub_routine) { | |
241 | /* Only put real entries in the table */ | |
242 | nentry = j + mig_e[i]->start; | |
243 | for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1; | |
244 | mig_buckets[pos].num; | |
245 | pos++, pos = pos % MAX_MIG_ENTRIES, howmany++) { | |
246 | if (mig_buckets[pos].num == nentry) { | |
247 | printf("message id = %d\n", nentry); | |
248 | panic("multiple entries with the same msgh_id"); | |
249 | } | |
250 | if (howmany == MAX_MIG_ENTRIES) { | |
251 | panic("the mig dispatch table is too small"); | |
252 | } | |
253 | } | |
254 | ||
255 | mig_buckets[pos].num = nentry; | |
256 | mig_buckets[pos].routine = mig_e[i]->routine[j].stub_routine; | |
257 | if (mig_e[i]->routine[j].max_reply_msg) { | |
258 | mig_buckets[pos].size = mig_e[i]->routine[j].max_reply_msg; | |
259 | } else { | |
260 | mig_buckets[pos].size = mig_e[i]->maxsize; | |
261 | } | |
262 | ||
263 | mig_table_max_displ = max(howmany, mig_table_max_displ); | |
264 | } | |
265 | } | |
1c79356b | 266 | } |
0a7de745 | 267 | printf("mig_table_max_displ = %d\n", mig_table_max_displ); |
1c79356b A |
268 | } |
269 | ||
270 | ||
271 | /* | |
272 | * Routine: ipc_kobject_server | |
273 | * Purpose: | |
274 | * Handle a message sent to the kernel. | |
275 | * Generates a reply message. | |
276 | * Version for Untyped IPC. | |
277 | * Conditions: | |
278 | * Nothing locked. | |
279 | */ | |
280 | ||
281 | ipc_kmsg_t | |
282 | ipc_kobject_server( | |
0a7de745 | 283 | ipc_kmsg_t request, |
39037602 | 284 | mach_msg_option_t __unused option) |
1c79356b A |
285 | { |
286 | mach_msg_size_t reply_size; | |
287 | ipc_kmsg_t reply; | |
288 | kern_return_t kr; | |
4bd07ac2 | 289 | ipc_port_t replyp = IPC_PORT_NULL; |
1c79356b | 290 | mach_msg_format_0_trailer_t *trailer; |
39037602 A |
291 | mig_hash_t *ptr; |
292 | task_t task = TASK_NULL; | |
293 | uint32_t exec_token; | |
294 | boolean_t exec_token_changed = FALSE; | |
d9a64523 | 295 | int request_msgh_id = request->ikm_header->msgh_id; |
cb323159 A |
296 | natural_t ikot; |
297 | ipc_port_t port; | |
91447636 | 298 | |
cb323159 A |
299 | reply = NULL; |
300 | port = request->ikm_header->msgh_remote_port; | |
301 | if (IP_VALID(port)) { | |
302 | ikot = ip_kotype(port); | |
303 | } else { | |
304 | ikot = IKOT_UNKNOWN; | |
305 | } | |
306 | if (ikot == IKOT_UEXT_OBJECT) { | |
307 | kr = uext_server(request, &reply); | |
308 | if ((MIG_NO_REPLY == kr) || (KERN_SUCCESS == kr)) { | |
309 | ipc_kmsg_trace_send(request, option); | |
310 | goto msgdone; | |
311 | } | |
312 | } | |
1c79356b | 313 | /* |
cb323159 | 314 | * Find corresponding mig_hash entry if any |
91447636 | 315 | */ |
1c79356b | 316 | { |
0a7de745 A |
317 | unsigned int i = (unsigned int)MIG_HASH(request_msgh_id); |
318 | int max_iter = mig_table_max_displ; | |
319 | ||
320 | do { | |
321 | ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES]; | |
322 | } while (request_msgh_id != ptr->num && ptr->num && --max_iter); | |
323 | ||
324 | if (!ptr->routine || request_msgh_id != ptr->num) { | |
325 | ptr = (mig_hash_t *)0; | |
326 | reply_size = mig_reply_size; | |
327 | } else { | |
328 | reply_size = ptr->size; | |
cb323159 | 329 | #if MACH_COUNTERS |
0a7de745 | 330 | ptr->callcount++; |
1c79356b | 331 | #endif |
0a7de745 | 332 | } |
1c79356b A |
333 | } |
334 | ||
335 | /* round up for trailer size */ | |
0a7de745 | 336 | reply_size += MAX_TRAILER_SIZE; |
1c79356b A |
337 | reply = ipc_kmsg_alloc(reply_size); |
338 | ||
339 | if (reply == IKM_NULL) { | |
340 | printf("ipc_kobject_server: dropping request\n"); | |
39037602 | 341 | ipc_kmsg_trace_send(request, option); |
1c79356b A |
342 | ipc_kmsg_destroy(request); |
343 | return IKM_NULL; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Initialize reply message. | |
348 | */ | |
349 | { | |
0a7de745 A |
350 | #define InP ((mach_msg_header_t *) request->ikm_header) |
351 | #define OutP ((mig_reply_error_t *) reply->ikm_header) | |
352 | ||
353 | /* | |
354 | * MIG should really assure no data leakage - | |
355 | * but until it does, pessimistically zero the | |
356 | * whole reply buffer. | |
357 | */ | |
358 | bzero((void *)OutP, reply_size); | |
359 | ||
360 | OutP->NDR = NDR_record; | |
361 | OutP->Head.msgh_size = sizeof(mig_reply_error_t); | |
362 | ||
363 | OutP->Head.msgh_bits = | |
364 | MACH_MSGH_BITS_SET(MACH_MSGH_BITS_LOCAL(InP->msgh_bits), 0, 0, 0); | |
365 | OutP->Head.msgh_remote_port = InP->msgh_local_port; | |
366 | OutP->Head.msgh_local_port = MACH_PORT_NULL; | |
367 | OutP->Head.msgh_voucher_port = MACH_PORT_NULL; | |
368 | OutP->Head.msgh_id = InP->msgh_id + 100; | |
369 | ||
370 | #undef InP | |
371 | #undef OutP | |
1c79356b A |
372 | } |
373 | ||
374 | /* | |
375 | * Find the routine to call, and call it | |
376 | * to perform the kernel function | |
377 | */ | |
39037602 | 378 | ipc_kmsg_trace_send(request, option); |
1c79356b | 379 | { |
0a7de745 A |
380 | if (ptr) { |
381 | /* | |
382 | * Check if the port is a task port, if its a task port then | |
383 | * snapshot the task exec token before the mig routine call. | |
384 | */ | |
cb323159 | 385 | if (ikot == IKOT_TASK) { |
0a7de745 A |
386 | task = convert_port_to_task_with_exec_token(port, &exec_token); |
387 | } | |
39037602 | 388 | |
0a7de745 | 389 | (*ptr->routine)(request->ikm_header, reply->ikm_header); |
39037602 | 390 | |
0a7de745 A |
391 | /* Check if the exec token changed during the mig routine */ |
392 | if (task != TASK_NULL) { | |
393 | if (exec_token != task->exec_token) { | |
394 | exec_token_changed = TRUE; | |
395 | } | |
396 | task_deallocate(task); | |
39037602 | 397 | } |
39037602 | 398 | |
0a7de745 A |
399 | kernel_task->messages_received++; |
400 | } else { | |
401 | if (!ipc_kobject_notify(request->ikm_header, reply->ikm_header)) { | |
4bd07ac2 | 402 | #if DEVELOPMENT || DEBUG |
0a7de745 A |
403 | printf("ipc_kobject_server: bogus kernel message, id=%d\n", |
404 | request->ikm_header->msgh_id); | |
405 | #endif /* DEVELOPMENT || DEBUG */ | |
406 | _MIG_MSGID_INVALID(request->ikm_header->msgh_id); | |
407 | ||
408 | ((mig_reply_error_t *) reply->ikm_header)->RetCode | |
409 | = MIG_BAD_ID; | |
410 | } else { | |
411 | kernel_task->messages_received++; | |
412 | } | |
1c79356b | 413 | } |
0a7de745 | 414 | kernel_task->messages_sent++; |
1c79356b A |
415 | } |
416 | ||
cb323159 A |
417 | if (!(reply->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) && |
418 | ((mig_reply_error_t *) reply->ikm_header)->RetCode != KERN_SUCCESS) { | |
419 | kr = ((mig_reply_error_t *) reply->ikm_header)->RetCode; | |
420 | } else { | |
421 | kr = KERN_SUCCESS; | |
422 | } | |
423 | ||
424 | msgdone: | |
1c79356b A |
425 | /* |
426 | * Destroy destination. The following code differs from | |
427 | * ipc_object_destroy in that we release the send-once | |
428 | * right instead of generating a send-once notification | |
cb323159 | 429 | * (which would bring us here again, creating a loop). |
1c79356b A |
430 | * It also differs in that we only expect send or |
431 | * send-once rights, never receive rights. | |
432 | * | |
433 | * We set msgh_remote_port to IP_NULL so that the kmsg | |
434 | * destroy routines don't try to destroy the port twice. | |
435 | */ | |
91447636 | 436 | switch (MACH_MSGH_BITS_REMOTE(request->ikm_header->msgh_bits)) { |
0a7de745 | 437 | case MACH_MSG_TYPE_PORT_SEND: |
cb323159 A |
438 | ipc_port_release_send(request->ikm_header->msgh_remote_port); |
439 | request->ikm_header->msgh_remote_port = IP_NULL; | |
0a7de745 A |
440 | break; |
441 | ||
442 | case MACH_MSG_TYPE_PORT_SEND_ONCE: | |
cb323159 A |
443 | ipc_port_release_sonce(request->ikm_header->msgh_remote_port); |
444 | request->ikm_header->msgh_remote_port = IP_NULL; | |
0a7de745 A |
445 | break; |
446 | ||
447 | default: | |
448 | panic("ipc_kobject_server: strange destination rights"); | |
1c79356b | 449 | } |
1c79356b | 450 | |
fe8ab488 A |
451 | /* |
452 | * Destroy voucher. The kernel MIG servers never take ownership | |
453 | * of vouchers sent in messages. Swallow any such rights here. | |
454 | */ | |
455 | if (IP_VALID(request->ikm_voucher)) { | |
456 | assert(MACH_MSG_TYPE_PORT_SEND == | |
0a7de745 | 457 | MACH_MSGH_BITS_VOUCHER(request->ikm_header->msgh_bits)); |
fe8ab488 A |
458 | ipc_port_release_send(request->ikm_voucher); |
459 | request->ikm_voucher = IP_NULL; | |
460 | } | |
461 | ||
1c79356b A |
462 | if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) { |
463 | /* | |
464 | * The server function is responsible for the contents | |
465 | * of the message. The reply port right is moved | |
466 | * to the reply message, and we have deallocated | |
467 | * the destination port right, so we just need | |
468 | * to free the kmsg. | |
469 | */ | |
470 | ipc_kmsg_free(request); | |
1c79356b A |
471 | } else { |
472 | /* | |
473 | * The message contents of the request are intact. | |
474 | * Destroy everthing except the reply port right, | |
475 | * which is needed in the reply message. | |
476 | */ | |
91447636 | 477 | request->ikm_header->msgh_local_port = MACH_PORT_NULL; |
1c79356b A |
478 | ipc_kmsg_destroy(request); |
479 | } | |
480 | ||
481 | if (kr == MIG_NO_REPLY) { | |
482 | /* | |
483 | * The server function will send a reply message | |
484 | * using the reply port right, which it has saved. | |
485 | */ | |
486 | ||
cb323159 A |
487 | if (reply) { |
488 | ipc_kmsg_free(reply); | |
489 | } | |
1c79356b | 490 | return IKM_NULL; |
cb323159 A |
491 | } |
492 | ||
493 | if (reply) { | |
494 | replyp = reply->ikm_header->msgh_remote_port; | |
495 | } | |
496 | ||
497 | if (!IP_VALID(replyp)) { | |
1c79356b A |
498 | /* |
499 | * Can't queue the reply message if the destination | |
500 | * (the reply port) isn't valid. | |
501 | */ | |
502 | ||
503 | ipc_kmsg_destroy(reply); | |
504 | ||
4bd07ac2 A |
505 | return IKM_NULL; |
506 | } else if (replyp->ip_receiver == ipc_space_kernel) { | |
507 | /* | |
508 | * Don't send replies to kobject kernel ports | |
509 | */ | |
510 | #if DEVELOPMENT || DEBUG | |
511 | printf("%s: refusing to send reply to kobject %d port (id:%d)\n", | |
0a7de745 A |
512 | __func__, ip_kotype(replyp), request_msgh_id); |
513 | #endif /* DEVELOPMENT || DEBUG */ | |
4bd07ac2 | 514 | ipc_kmsg_destroy(reply); |
1c79356b A |
515 | return IKM_NULL; |
516 | } | |
517 | ||
39037602 A |
518 | /* Fail the MIG call if the task exec token changed during the call */ |
519 | if (kr == KERN_SUCCESS && exec_token_changed) { | |
520 | /* | |
521 | * Create a new reply msg with error and destroy the old reply msg. | |
522 | */ | |
523 | ipc_kmsg_t new_reply = ipc_kmsg_alloc(reply_size); | |
524 | ||
525 | if (new_reply == IKM_NULL) { | |
526 | printf("ipc_kobject_server: dropping request\n"); | |
527 | ipc_kmsg_destroy(reply); | |
528 | return IKM_NULL; | |
529 | } | |
530 | /* | |
531 | * Initialize the new reply message. | |
532 | */ | |
533 | { | |
0a7de745 A |
534 | #define OutP_new ((mig_reply_error_t *) new_reply->ikm_header) |
535 | #define OutP_old ((mig_reply_error_t *) reply->ikm_header) | |
39037602 | 536 | |
0a7de745 | 537 | bzero((void *)OutP_new, reply_size); |
39037602 | 538 | |
0a7de745 A |
539 | OutP_new->NDR = OutP_old->NDR; |
540 | OutP_new->Head.msgh_size = sizeof(mig_reply_error_t); | |
541 | OutP_new->Head.msgh_bits = OutP_old->Head.msgh_bits & ~MACH_MSGH_BITS_COMPLEX; | |
542 | OutP_new->Head.msgh_remote_port = OutP_old->Head.msgh_remote_port; | |
543 | OutP_new->Head.msgh_local_port = MACH_PORT_NULL; | |
544 | OutP_new->Head.msgh_voucher_port = MACH_PORT_NULL; | |
545 | OutP_new->Head.msgh_id = OutP_old->Head.msgh_id; | |
39037602 | 546 | |
0a7de745 A |
547 | /* Set the error as KERN_INVALID_TASK */ |
548 | OutP_new->RetCode = KERN_INVALID_TASK; | |
39037602 | 549 | |
0a7de745 | 550 | #undef OutP_new |
39037602 A |
551 | #undef OutP_old |
552 | } | |
553 | ||
554 | /* | |
555 | * Destroy everything in reply except the reply port right, | |
556 | * which is needed in the new reply message. | |
557 | */ | |
558 | reply->ikm_header->msgh_remote_port = MACH_PORT_NULL; | |
559 | ipc_kmsg_destroy(reply); | |
560 | ||
561 | reply = new_reply; | |
562 | } | |
563 | ||
0a7de745 A |
564 | trailer = (mach_msg_format_0_trailer_t *) |
565 | ((vm_offset_t)reply->ikm_header + (int)reply->ikm_header->msgh_size); | |
91447636 | 566 | |
0a7de745 A |
567 | trailer->msgh_sender = KERNEL_SECURITY_TOKEN; |
568 | trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0; | |
569 | trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE; | |
1c79356b | 570 | |
1c79356b A |
571 | return reply; |
572 | } | |
573 | ||
574 | /* | |
575 | * Routine: ipc_kobject_set | |
576 | * Purpose: | |
577 | * Make a port represent a kernel object of the given type. | |
578 | * The caller is responsible for handling refs for the | |
579 | * kernel object, if necessary. | |
580 | * Conditions: | |
581 | * Nothing locked. The port must be active if setting | |
582 | * a kobject linkage. Clearing a linkage is OK on an | |
583 | * inactive port. | |
584 | */ | |
585 | void | |
586 | ipc_kobject_set( | |
0a7de745 A |
587 | ipc_port_t port, |
588 | ipc_kobject_t kobject, | |
589 | ipc_kobject_type_t type) | |
1c79356b A |
590 | { |
591 | ip_lock(port); | |
592 | ipc_kobject_set_atomically(port, kobject, type); | |
593 | ip_unlock(port); | |
594 | } | |
595 | ||
596 | void | |
597 | ipc_kobject_set_atomically( | |
0a7de745 A |
598 | ipc_port_t port, |
599 | ipc_kobject_t kobject, | |
600 | ipc_kobject_type_t type) | |
1c79356b A |
601 | { |
602 | assert(type == IKOT_NONE || ip_active(port)); | |
0a7de745 | 603 | #if MACH_ASSERT |
cb323159 | 604 | port->ip_spares[2] = (port->ip_object.io_bits & IO_BITS_KOTYPE); |
0a7de745 | 605 | #endif /* MACH_ASSERT */ |
cb323159 | 606 | port->ip_object.io_bits = (port->ip_object.io_bits & ~IO_BITS_KOTYPE) | type; |
1c79356b | 607 | port->ip_kobject = kobject; |
cb323159 A |
608 | if (type != IKOT_NONE) { |
609 | /* Once set, this bit can never be unset */ | |
610 | port->ip_object.io_bits |= IO_BITS_KOBJECT; | |
611 | } | |
612 | } | |
613 | ||
614 | /* | |
615 | * Routine: ipc_kobject_alloc_port | |
616 | * Purpose: | |
617 | * Allocate a kobject port in the kernel space of the specified type. | |
618 | * | |
619 | * This function never fails. | |
620 | * | |
621 | * Conditions: | |
622 | * No locks held (memory is allocated) | |
623 | */ | |
624 | ipc_port_t | |
625 | ipc_kobject_alloc_port( | |
626 | ipc_kobject_t kobject, | |
627 | ipc_kobject_type_t type, | |
628 | ipc_kobject_alloc_options_t options) | |
629 | { | |
630 | ipc_port_t port = ipc_port_alloc_kernel(); | |
631 | ||
632 | if (port == IP_NULL) { | |
633 | panic("ipc_kobject_alloc_port(): failed to allocate port"); | |
634 | } | |
635 | ||
636 | ipc_kobject_set_atomically(port, kobject, type); | |
637 | ||
638 | if (options & IPC_KOBJECT_ALLOC_MAKE_SEND) { | |
639 | ipc_port_make_send_locked(port); | |
640 | } | |
641 | if (options & IPC_KOBJECT_ALLOC_NSREQUEST) { | |
642 | ipc_port_make_sonce_locked(port); | |
643 | port->ip_nsrequest = port; | |
644 | } | |
645 | if (options & IPC_KOBJECT_ALLOC_NO_GRANT) { | |
646 | port->ip_no_grant = 1; | |
647 | } | |
648 | if (options & IPC_KOBJECT_ALLOC_IMMOVABLE_SEND) { | |
649 | port->ip_immovable_send = 1; | |
650 | } | |
651 | ||
652 | return port; | |
653 | } | |
654 | ||
655 | /* | |
656 | * Routine: ipc_kobject_make_send_lazy_alloc_port | |
657 | * Purpose: | |
658 | * Make a send once for a kobject port. | |
659 | * | |
660 | * A location owning this port is passed in port_store. | |
661 | * If no port exists, a port is made lazily. | |
662 | * | |
663 | * A send right is made for the port, and if this is the first one | |
664 | * (possibly not for the first time), then the no-more-senders | |
665 | * notification is rearmed. | |
666 | * | |
667 | * When a notification is armed, the kobject must donate | |
668 | * one of its references to the port. It is expected | |
669 | * the no-more-senders notification will consume this reference. | |
670 | * | |
671 | * Returns: | |
672 | * TRUE if a notification was armed | |
673 | * FALSE else | |
674 | * | |
675 | * Conditions: | |
676 | * Nothing is locked, memory can be allocated. | |
677 | * The caller must be able to donate a kobject reference to the port. | |
678 | */ | |
679 | boolean_t | |
680 | ipc_kobject_make_send_lazy_alloc_port( | |
681 | ipc_port_t *port_store, | |
682 | ipc_kobject_t kobject, | |
683 | ipc_kobject_type_t type) | |
684 | { | |
685 | ipc_port_t port, previous; | |
686 | boolean_t rc = FALSE; | |
687 | ||
688 | port = os_atomic_load(port_store, dependency); | |
689 | ||
690 | if (!IP_VALID(port)) { | |
691 | port = ipc_kobject_alloc_port(kobject, type, | |
692 | IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST); | |
693 | if (os_atomic_cmpxchgv(port_store, IP_NULL, port, &previous, release)) { | |
694 | return TRUE; | |
695 | } | |
696 | ||
697 | // undo what ipc_kobject_alloc_port() did above | |
698 | port->ip_nsrequest = IP_NULL; | |
699 | port->ip_mscount = 0; | |
700 | port->ip_sorights = 0; | |
701 | port->ip_srights = 0; | |
702 | ip_release(port); | |
703 | ip_release(port); | |
704 | ipc_port_dealloc_kernel(port); | |
705 | ||
706 | port = previous; | |
707 | } | |
708 | ||
709 | ip_lock(port); | |
710 | ipc_port_make_send_locked(port); | |
711 | if (port->ip_srights == 1) { | |
712 | ipc_port_make_sonce_locked(port); | |
713 | assert(port->ip_nsrequest == IP_NULL); | |
714 | port->ip_nsrequest = port; | |
715 | rc = TRUE; | |
716 | } | |
717 | ip_unlock(port); | |
718 | ||
719 | return rc; | |
1c79356b A |
720 | } |
721 | ||
722 | /* | |
723 | * Routine: ipc_kobject_destroy | |
724 | * Purpose: | |
725 | * Release any kernel object resources associated | |
726 | * with the port, which is being destroyed. | |
727 | * | |
728 | * This should only be needed when resources are | |
729 | * associated with a user's port. In the normal case, | |
730 | * when the kernel is the receiver, the code calling | |
731 | * ipc_port_dealloc_kernel should clean up the resources. | |
732 | * Conditions: | |
733 | * The port is not locked, but it is dead. | |
734 | */ | |
735 | ||
736 | void | |
737 | ipc_kobject_destroy( | |
0a7de745 | 738 | ipc_port_t port) |
1c79356b A |
739 | { |
740 | switch (ip_kotype(port)) { | |
1c79356b A |
741 | case IKOT_TIMER: |
742 | mk_timer_port_destroy(port); | |
743 | break; | |
744 | ||
745 | case IKOT_NAMED_ENTRY: | |
746 | mach_destroy_memory_entry(port); | |
747 | break; | |
748 | ||
55e303ae A |
749 | case IKOT_HOST_NOTIFY: |
750 | host_notify_port_destroy(port); | |
751 | break; | |
752 | ||
753 | default: | |
1c79356b A |
754 | break; |
755 | } | |
756 | } | |
757 | ||
758 | ||
1c79356b A |
759 | boolean_t |
760 | ipc_kobject_notify( | |
761 | mach_msg_header_t *request_header, | |
762 | mach_msg_header_t *reply_header) | |
763 | { | |
4bd07ac2 | 764 | mach_msg_max_trailer_t * trailer; |
cb323159 | 765 | ipc_port_t port = request_header->msgh_remote_port; |
1c79356b A |
766 | |
767 | ((mig_reply_error_t *) reply_header)->RetCode = MIG_NO_REPLY; | |
4bd07ac2 A |
768 | |
769 | trailer = (mach_msg_max_trailer_t *) | |
0a7de745 | 770 | ((vm_offset_t)request_header + request_header->msgh_size); |
5ba3f43e A |
771 | |
772 | /* | |
773 | * The kobject notification is privileged and can change the | |
774 | * refcount on kernel-internal objects - make sure | |
775 | * that the message wasn't faked! | |
776 | */ | |
4bd07ac2 | 777 | if (0 != bcmp(&trailer->msgh_audit, &KERNEL_AUDIT_TOKEN, |
0a7de745 | 778 | sizeof(trailer->msgh_audit))) { |
4bd07ac2 A |
779 | return FALSE; |
780 | } | |
781 | if (0 != bcmp(&trailer->msgh_sender, &KERNEL_SECURITY_TOKEN, | |
0a7de745 | 782 | sizeof(trailer->msgh_sender))) { |
4bd07ac2 A |
783 | return FALSE; |
784 | } | |
785 | ||
1c79356b | 786 | switch (request_header->msgh_id) { |
0a7de745 A |
787 | case MACH_NOTIFY_NO_SENDERS: |
788 | switch (ip_kotype(port)) { | |
789 | case IKOT_VOUCHER: | |
790 | ipc_voucher_notify(request_header); | |
791 | return TRUE; | |
792 | ||
793 | case IKOT_VOUCHER_ATTR_CONTROL: | |
794 | ipc_voucher_attr_control_notify(request_header); | |
795 | return TRUE; | |
796 | ||
797 | case IKOT_SEMAPHORE: | |
798 | semaphore_notify(request_header); | |
799 | return TRUE; | |
800 | ||
801 | case IKOT_TASK: | |
802 | task_port_notify(request_header); | |
803 | return TRUE; | |
804 | ||
805 | case IKOT_NAMED_ENTRY: | |
806 | ip_lock(port); | |
807 | ||
808 | /* | |
809 | * Bring the sequence number and mscount in | |
810 | * line with ipc_port_destroy assertion. | |
811 | */ | |
812 | port->ip_mscount = 0; | |
813 | port->ip_messages.imq_seqno = 0; | |
814 | ipc_port_destroy(port); /* releases lock */ | |
815 | return TRUE; | |
816 | ||
817 | case IKOT_UPL: | |
818 | upl_no_senders( | |
819 | request_header->msgh_remote_port, | |
820 | (mach_port_mscount_t) | |
821 | ((mach_no_senders_notification_t *) | |
822 | request_header)->not_count); | |
823 | reply_header->msgh_remote_port = MACH_PORT_NULL; | |
824 | return TRUE; | |
825 | ||
826 | #if CONFIG_AUDIT | |
827 | case IKOT_AU_SESSIONPORT: | |
828 | audit_session_nosenders(request_header); | |
829 | return TRUE; | |
b0d623f7 | 830 | #endif |
0a7de745 A |
831 | case IKOT_FILEPORT: |
832 | fileport_notify(request_header); | |
833 | return TRUE; | |
5ba3f43e | 834 | |
0a7de745 A |
835 | case IKOT_WORK_INTERVAL: |
836 | work_interval_port_notify(request_header); | |
837 | return TRUE; | |
838 | } | |
5ba3f43e | 839 | break; |
0b4e3aa0 | 840 | |
0a7de745 A |
841 | case MACH_NOTIFY_PORT_DELETED: |
842 | case MACH_NOTIFY_PORT_DESTROYED: | |
843 | case MACH_NOTIFY_SEND_ONCE: | |
844 | case MACH_NOTIFY_DEAD_NAME: | |
1c79356b A |
845 | break; |
846 | ||
0a7de745 | 847 | default: |
1c79356b A |
848 | return FALSE; |
849 | } | |
850 | switch (ip_kotype(port)) { | |
1c79356b | 851 | #ifdef IOKIT |
0a7de745 A |
852 | case IKOT_IOKIT_OBJECT: |
853 | case IKOT_IOKIT_CONNECT: | |
854 | case IKOT_IOKIT_IDENT: | |
cb323159 | 855 | case IKOT_UEXT_OBJECT: |
0a7de745 A |
856 | { |
857 | return iokit_notify(request_header); | |
858 | } | |
1c79356b | 859 | #endif |
0a7de745 A |
860 | case IKOT_TASK_RESUME: |
861 | { | |
862 | return task_suspension_notify(request_header); | |
863 | } | |
39236c6e | 864 | |
0a7de745 A |
865 | default: |
866 | return FALSE; | |
867 | } | |
1c79356b | 868 | } |