]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_kobject.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_kobject.c
1 /*
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 * Copyright (c) 2005 SPARTA, Inc.
62 */
63 /*
64 */
65 /*
66 * File: 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>
75 #include <mach_machine_routines.h>
76 #include <mach_rt.h>
77 #include <platforms.h>
78
79 #include <mach/mig.h>
80 #include <mach/port.h>
81 #include <mach/kern_return.h>
82 #include <mach/message.h>
83 #include <mach/mig_errors.h>
84 #include <mach/notify.h>
85 #include <mach/ndr.h>
86 #include <mach/vm_param.h>
87
88 #include <mach/mach_vm_server.h>
89 #include <mach/mach_port_server.h>
90 #include <mach/mach_host_server.h>
91 #include <mach/host_priv_server.h>
92 #include <mach/host_security_server.h>
93 #include <mach/clock_server.h>
94 #include <mach/clock_priv_server.h>
95 #include <mach/lock_set_server.h>
96 #include <default_pager/default_pager_object_server.h>
97 #include <mach/memory_object_server.h>
98 #include <mach/memory_object_control_server.h>
99 #include <mach/memory_object_default_server.h>
100 #include <mach/memory_object_name_server.h>
101 #include <mach/processor_server.h>
102 #include <mach/processor_set_server.h>
103 #include <mach/task_server.h>
104 #if VM32_SUPPORT
105 #include <mach/vm32_map_server.h>
106 #endif
107 #include <mach/thread_act_server.h>
108
109 #include <device/device_types.h>
110 #include <device/device_server.h>
111
112 #include <UserNotification/UNDReplyServer.h>
113
114 #if CONFIG_AUDIT
115 #include <kern/audit_sessionport.h>
116 #endif
117
118 #if MACH_MACHINE_ROUTINES
119 #include <machine/machine_routines.h>
120 #endif /* MACH_MACHINE_ROUTINES */
121 #if XK_PROXY
122 #include <uk_xkern/xk_uproxy_server.h>
123 #endif /* XK_PROXY */
124
125 #include <kern/ipc_tt.h>
126 #include <kern/ipc_mig.h>
127 #include <kern/ipc_misc.h>
128 #include <kern/ipc_kobject.h>
129 #include <kern/host_notify.h>
130 #include <kern/mk_timer.h>
131 #include <kern/misc_protos.h>
132 #include <ipc/ipc_kmsg.h>
133 #include <ipc/ipc_port.h>
134 #include <ipc/ipc_labelh.h>
135 #include <kern/counters.h>
136
137 #include <vm/vm_protos.h>
138
139 #include <security/mac_mach_internal.h>
140
141 extern char *proc_name_address(void *p);
142 extern int proc_pid(void *p);
143
144 /*
145 * Routine: ipc_kobject_notify
146 * Purpose:
147 * Deliver notifications to kobjects that care about them.
148 */
149 boolean_t
150 ipc_kobject_notify(
151 mach_msg_header_t *request_header,
152 mach_msg_header_t *reply_header);
153
154 typedef struct {
155 mach_msg_id_t num;
156 mig_routine_t routine;
157 int size;
158 #if MACH_COUNTERS
159 mach_counter_t callcount;
160 #endif
161 } mig_hash_t;
162
163 #define MAX_MIG_ENTRIES 1024
164 #define MIG_HASH(x) (x)
165
166 #ifndef max
167 #define max(a,b) (((a) > (b)) ? (a) : (b))
168 #endif /* max */
169
170 mig_hash_t mig_buckets[MAX_MIG_ENTRIES];
171 int mig_table_max_displ;
172 mach_msg_size_t mig_reply_size;
173
174 #if CONFIG_MACF
175 #include <mach/security_server.h>
176 #endif
177
178
179
180 const struct mig_subsystem *mig_e[] = {
181 (const struct mig_subsystem *)&mach_vm_subsystem,
182 (const struct mig_subsystem *)&mach_port_subsystem,
183 (const struct mig_subsystem *)&mach_host_subsystem,
184 (const struct mig_subsystem *)&host_priv_subsystem,
185 (const struct mig_subsystem *)&host_security_subsystem,
186 (const struct mig_subsystem *)&clock_subsystem,
187 (const struct mig_subsystem *)&clock_priv_subsystem,
188 (const struct mig_subsystem *)&processor_subsystem,
189 (const struct mig_subsystem *)&processor_set_subsystem,
190 (const struct mig_subsystem *)&is_iokit_subsystem,
191 (const struct mig_subsystem *)&memory_object_name_subsystem,
192 (const struct mig_subsystem *)&lock_set_subsystem,
193 (const struct mig_subsystem *)&task_subsystem,
194 (const struct mig_subsystem *)&thread_act_subsystem,
195 #if VM32_SUPPORT
196 (const struct mig_subsystem *)&vm32_map_subsystem,
197 #endif
198 (const struct mig_subsystem *)&UNDReply_subsystem,
199 (const struct mig_subsystem *)&default_pager_object_subsystem,
200
201 #if XK_PROXY
202 (const struct mig_subsystem *)&do_uproxy_xk_uproxy_subsystem,
203 #endif /* XK_PROXY */
204 #if MACH_MACHINE_ROUTINES
205 (const struct mig_subsystem *)&MACHINE_SUBSYSTEM,
206 #endif /* MACH_MACHINE_ROUTINES */
207 #if MCMSG && iPSC860
208 (const struct mig_subsystem *)&mcmsg_info_subsystem,
209 #endif /* MCMSG && iPSC860 */
210
211 #if CONFIG_MACF
212 (const struct mig_subsystem *)&security_subsystem,
213 #endif
214 };
215
216 void
217 mig_init(void)
218 {
219 unsigned int i, n = sizeof(mig_e)/sizeof(const struct mig_subsystem *);
220 int howmany;
221 mach_msg_id_t j, pos, nentry, range;
222
223 for (i = 0; i < n; i++) {
224 range = mig_e[i]->end - mig_e[i]->start;
225 if (!mig_e[i]->start || range < 0)
226 panic("the msgh_ids in mig_e[] aren't valid!");
227 mig_reply_size = max(mig_reply_size, mig_e[i]->maxsize);
228
229 for (j = 0; j < range; j++) {
230 if (mig_e[i]->routine[j].stub_routine) {
231 /* Only put real entries in the table */
232 nentry = j + mig_e[i]->start;
233 for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1;
234 mig_buckets[pos].num;
235 pos++, pos = pos % MAX_MIG_ENTRIES, howmany++) {
236 if (mig_buckets[pos].num == nentry) {
237 printf("message id = %d\n", nentry);
238 panic("multiple entries with the same msgh_id");
239 }
240 if (howmany == MAX_MIG_ENTRIES)
241 panic("the mig dispatch table is too small");
242 }
243
244 mig_buckets[pos].num = nentry;
245 mig_buckets[pos].routine = mig_e[i]->routine[j].stub_routine;
246 if (mig_e[i]->routine[j].max_reply_msg)
247 mig_buckets[pos].size = mig_e[i]->routine[j].max_reply_msg;
248 else
249 mig_buckets[pos].size = mig_e[i]->maxsize;
250
251 mig_table_max_displ = max(howmany, mig_table_max_displ);
252 }
253 }
254 }
255 printf("mig_table_max_displ = %d\n", mig_table_max_displ);
256 }
257
258
259 /*
260 * Routine: ipc_kobject_server
261 * Purpose:
262 * Handle a message sent to the kernel.
263 * Generates a reply message.
264 * Version for Untyped IPC.
265 * Conditions:
266 * Nothing locked.
267 */
268
269 ipc_kmsg_t
270 ipc_kobject_server(
271 ipc_kmsg_t request)
272 {
273 mach_msg_size_t reply_size;
274 ipc_kmsg_t reply;
275 kern_return_t kr;
276 ipc_port_t *destp;
277 mach_msg_format_0_trailer_t *trailer;
278 register mig_hash_t *ptr;
279
280 /*
281 * Find out corresponding mig_hash entry if any
282 */
283 {
284 register int key = request->ikm_header->msgh_id;
285 register int i = MIG_HASH(key);
286 register int max_iter = mig_table_max_displ;
287
288 do
289 ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES];
290 while (key != ptr->num && ptr->num && --max_iter);
291
292 if (!ptr->routine || key != ptr->num) {
293 ptr = (mig_hash_t *)0;
294 reply_size = mig_reply_size;
295 } else {
296 reply_size = ptr->size;
297 #if MACH_COUNTER
298 ptr->callcount++;
299 #endif
300 }
301 }
302
303 /* round up for trailer size */
304 reply_size += MAX_TRAILER_SIZE;
305 reply = ipc_kmsg_alloc(reply_size);
306
307 if (reply == IKM_NULL) {
308 printf("ipc_kobject_server: dropping request\n");
309 ipc_kmsg_destroy(request);
310 return IKM_NULL;
311 }
312
313 /*
314 * Initialize reply message.
315 */
316 {
317 #define InP ((mach_msg_header_t *) request->ikm_header)
318 #define OutP ((mig_reply_error_t *) reply->ikm_header)
319
320 /*
321 * MIG should really assure no data leakage -
322 * but until it does, pessimistically zero the
323 * whole reply buffer.
324 */
325 bzero((void *)OutP, reply_size);
326
327 OutP->NDR = NDR_record;
328 OutP->Head.msgh_size = sizeof(mig_reply_error_t);
329
330 OutP->Head.msgh_bits =
331 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP->msgh_bits), 0);
332 OutP->Head.msgh_remote_port = InP->msgh_local_port;
333 OutP->Head.msgh_local_port = MACH_PORT_NULL;
334 OutP->Head.msgh_reserved = (mach_msg_size_t)InP->msgh_id; /* useful for debug */
335 OutP->Head.msgh_id = InP->msgh_id + 100;
336
337 #undef InP
338 #undef OutP
339 }
340
341 /*
342 * Find the routine to call, and call it
343 * to perform the kernel function
344 */
345 {
346 if (ptr) {
347 (*ptr->routine)(request->ikm_header, reply->ikm_header);
348 kernel_task->messages_received++;
349 }
350 else {
351 if (!ipc_kobject_notify(request->ikm_header, reply->ikm_header)){
352 #if MACH_IPC_TEST
353 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
354 request->ikm_header->msgh_id);
355 #endif /* MACH_IPC_TEST */
356 _MIG_MSGID_INVALID(request->ikm_header->msgh_id);
357
358 ((mig_reply_error_t *) reply->ikm_header)->RetCode
359 = MIG_BAD_ID;
360 }
361 else
362 kernel_task->messages_received++;
363 }
364 kernel_task->messages_sent++;
365 }
366
367 /*
368 * Destroy destination. The following code differs from
369 * ipc_object_destroy in that we release the send-once
370 * right instead of generating a send-once notification
371 * (which would bring us here again, creating a loop).
372 * It also differs in that we only expect send or
373 * send-once rights, never receive rights.
374 *
375 * We set msgh_remote_port to IP_NULL so that the kmsg
376 * destroy routines don't try to destroy the port twice.
377 */
378 destp = (ipc_port_t *) &request->ikm_header->msgh_remote_port;
379 switch (MACH_MSGH_BITS_REMOTE(request->ikm_header->msgh_bits)) {
380 case MACH_MSG_TYPE_PORT_SEND:
381 ipc_port_release_send(*destp);
382 break;
383
384 case MACH_MSG_TYPE_PORT_SEND_ONCE:
385 ipc_port_release_sonce(*destp);
386 break;
387
388 default:
389 panic("ipc_kobject_server: strange destination rights");
390 }
391 *destp = IP_NULL;
392
393 if (!(reply->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
394 ((mig_reply_error_t *) reply->ikm_header)->RetCode != KERN_SUCCESS)
395 kr = ((mig_reply_error_t *) reply->ikm_header)->RetCode;
396 else
397 kr = KERN_SUCCESS;
398
399 if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) {
400 /*
401 * The server function is responsible for the contents
402 * of the message. The reply port right is moved
403 * to the reply message, and we have deallocated
404 * the destination port right, so we just need
405 * to free the kmsg.
406 */
407 ipc_kmsg_free(request);
408
409 } else {
410 /*
411 * The message contents of the request are intact.
412 * Destroy everthing except the reply port right,
413 * which is needed in the reply message.
414 */
415 request->ikm_header->msgh_local_port = MACH_PORT_NULL;
416 ipc_kmsg_destroy(request);
417 }
418
419 if (kr == MIG_NO_REPLY) {
420 /*
421 * The server function will send a reply message
422 * using the reply port right, which it has saved.
423 */
424
425 ipc_kmsg_free(reply);
426
427 return IKM_NULL;
428 } else if (!IP_VALID((ipc_port_t)reply->ikm_header->msgh_remote_port)) {
429 /*
430 * Can't queue the reply message if the destination
431 * (the reply port) isn't valid.
432 */
433
434 ipc_kmsg_destroy(reply);
435
436 return IKM_NULL;
437 }
438
439 trailer = (mach_msg_format_0_trailer_t *)
440 ((vm_offset_t)reply->ikm_header + (int)reply->ikm_header->msgh_size);
441
442 trailer->msgh_sender = KERNEL_SECURITY_TOKEN;
443 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
444 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
445
446 return reply;
447 }
448
449 /*
450 * Routine: ipc_kobject_set
451 * Purpose:
452 * Make a port represent a kernel object of the given type.
453 * The caller is responsible for handling refs for the
454 * kernel object, if necessary.
455 * Conditions:
456 * Nothing locked. The port must be active if setting
457 * a kobject linkage. Clearing a linkage is OK on an
458 * inactive port.
459 */
460 void
461 ipc_kobject_set(
462 ipc_port_t port,
463 ipc_kobject_t kobject,
464 ipc_kobject_type_t type)
465 {
466 ip_lock(port);
467 ipc_kobject_set_atomically(port, kobject, type);
468
469 #if CONFIG_MACF_MACH
470 mac_port_label_update_kobject (&port->ip_label, type);
471 #endif
472
473 ip_unlock(port);
474 }
475
476 void
477 ipc_kobject_set_atomically(
478 ipc_port_t port,
479 ipc_kobject_t kobject,
480 ipc_kobject_type_t type)
481 {
482 assert(type == IKOT_NONE || ip_active(port));
483 #if MACH_ASSERT
484 port->ip_spares[2] = (port->ip_bits & IO_BITS_KOTYPE);
485 #endif /* MACH_ASSERT */
486 port->ip_bits = (port->ip_bits &~ IO_BITS_KOTYPE) | type;
487 port->ip_kobject = kobject;
488 }
489
490 /*
491 * Routine: ipc_kobject_destroy
492 * Purpose:
493 * Release any kernel object resources associated
494 * with the port, which is being destroyed.
495 *
496 * This should only be needed when resources are
497 * associated with a user's port. In the normal case,
498 * when the kernel is the receiver, the code calling
499 * ipc_port_dealloc_kernel should clean up the resources.
500 * Conditions:
501 * The port is not locked, but it is dead.
502 */
503
504 void
505 ipc_kobject_destroy(
506 ipc_port_t port)
507 {
508 switch (ip_kotype(port)) {
509
510 case IKOT_TIMER:
511 mk_timer_port_destroy(port);
512 break;
513
514 case IKOT_NAMED_ENTRY:
515 mach_destroy_memory_entry(port);
516 break;
517
518 case IKOT_HOST_NOTIFY:
519 host_notify_port_destroy(port);
520 break;
521
522 #if CONFIG_MACF_MACH
523 case IKOT_LABELH:
524 labelh_destroy(port);
525 break;
526 #endif
527
528 default:
529 break;
530 }
531 }
532
533
534 boolean_t
535 ipc_kobject_notify(
536 mach_msg_header_t *request_header,
537 mach_msg_header_t *reply_header)
538 {
539 ipc_port_t port = (ipc_port_t) request_header->msgh_remote_port;
540
541 ((mig_reply_error_t *) reply_header)->RetCode = MIG_NO_REPLY;
542 switch (request_header->msgh_id) {
543 case MACH_NOTIFY_NO_SENDERS:
544 if(ip_kotype(port) == IKOT_NAMED_ENTRY) {
545 ip_lock(port);
546
547 /*
548 * Bring the sequence number and mscount in
549 * line with ipc_port_destroy assertion.
550 */
551 port->ip_mscount = 0;
552 port->ip_messages.imq_seqno = 0;
553 ipc_port_destroy(port); /* releases lock */
554 return TRUE;
555 }
556 if (ip_kotype(port) == IKOT_UPL) {
557 upl_no_senders(
558 request_header->msgh_remote_port,
559 (mach_port_mscount_t)
560 ((mach_no_senders_notification_t *)
561 request_header)->not_count);
562 reply_header->msgh_remote_port = MACH_PORT_NULL;
563 return TRUE;
564 }
565 #if CONFIG_AUDIT
566 if (ip_kotype(port) == IKOT_AU_SESSIONPORT) {
567 audit_session_nosenders(request_header);
568 return TRUE;
569 }
570 #endif
571 if (ip_kotype(port) == IKOT_FILEPORT) {
572 fileport_notify(request_header);
573 return TRUE;
574 }
575
576 break;
577
578 case MACH_NOTIFY_PORT_DELETED:
579 case MACH_NOTIFY_PORT_DESTROYED:
580 case MACH_NOTIFY_SEND_ONCE:
581 case MACH_NOTIFY_DEAD_NAME:
582 break;
583
584 default:
585 return FALSE;
586 }
587 switch (ip_kotype(port)) {
588
589 #ifdef IOKIT
590 case IKOT_IOKIT_OBJECT:
591 case IKOT_IOKIT_CONNECT:
592 case IKOT_IOKIT_SPARE:
593 {
594 return iokit_notify(request_header);
595 }
596 #endif
597 case IKOT_TASK_RESUME:
598 {
599 return task_suspension_notify(request_header);
600 }
601
602 default:
603 return FALSE;
604 }
605 }