]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_object.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_object.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
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-2006 SPARTA, Inc.
62 */
1c79356b
A
63/*
64 */
65/*
66 * File: ipc/ipc_object.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Functions to manipulate IPC objects.
71 */
72
73#include <mach_rt.h>
74
91447636 75#include <mach/mach_types.h>
1c79356b
A
76#include <mach/boolean.h>
77#include <mach/kern_return.h>
78#include <mach/port.h>
79#include <mach/message.h>
91447636
A
80
81#include <kern/kern_types.h>
1c79356b 82#include <kern/misc_protos.h>
2d21ac55 83#include <kern/ipc_kobject.h>
91447636
A
84
85#include <ipc/ipc_types.h>
fe8ab488 86#include <ipc/ipc_importance.h>
1c79356b
A
87#include <ipc/port.h>
88#include <ipc/ipc_space.h>
89#include <ipc/ipc_entry.h>
90#include <ipc/ipc_object.h>
91#include <ipc/ipc_hash.h>
92#include <ipc/ipc_right.h>
93#include <ipc/ipc_notify.h>
6d2010ae 94#include <ipc/ipc_port.h>
1c79356b 95#include <ipc/ipc_pset.h>
2d21ac55
A
96
97#include <security/mac_mach_internal.h>
1c79356b
A
98
99zone_t ipc_object_zones[IOT_NUMBER];
100
101/*
102 * Routine: ipc_object_reference
103 * Purpose:
104 * Take a reference to an object.
105 */
106
107void
108ipc_object_reference(
109 ipc_object_t object)
110{
1c79356b 111 io_reference(object);
1c79356b
A
112}
113
114/*
115 * Routine: ipc_object_release
116 * Purpose:
117 * Release a reference to an object.
118 */
119
120void
121ipc_object_release(
122 ipc_object_t object)
123{
1c79356b 124 io_release(object);
1c79356b
A
125}
126
127/*
128 * Routine: ipc_object_translate
129 * Purpose:
130 * Look up an object in a space.
131 * Conditions:
132 * Nothing locked before. If successful, the object
133 * is returned locked. The caller doesn't get a ref.
134 * Returns:
135 * KERN_SUCCESS Object returned locked.
136 * KERN_INVALID_TASK The space is dead.
00867663
A
137 * KERN_INVALID_NAME The name doesn't denote a right
138 * KERN_INVALID_RIGHT Name doesn't denote the correct right
1c79356b 139 */
1c79356b
A
140kern_return_t
141ipc_object_translate(
142 ipc_space_t space,
143 mach_port_name_t name,
144 mach_port_right_t right,
145 ipc_object_t *objectp)
146{
147 ipc_entry_t entry;
148 ipc_object_t object;
149 kern_return_t kr;
150
151 kr = ipc_right_lookup_read(space, name, &entry);
152 if (kr != KERN_SUCCESS)
153 return kr;
154 /* space is read-locked and active */
155
156 if ((entry->ie_bits & MACH_PORT_TYPE(right)) == MACH_PORT_TYPE_NONE) {
157 is_read_unlock(space);
158 return KERN_INVALID_RIGHT;
159 }
160
161 object = entry->ie_object;
162 assert(object != IO_NULL);
163
164 io_lock(object);
165 is_read_unlock(space);
166
167 *objectp = object;
168 return KERN_SUCCESS;
169}
170
171/*
172 * Routine: ipc_object_translate_two
173 * Purpose:
174 * Look up two objects in a space.
175 * Conditions:
176 * Nothing locked before. If successful, the objects
177 * are returned locked. The caller doesn't get a ref.
178 * Returns:
179 * KERN_SUCCESS Objects returned locked.
180 * KERN_INVALID_TASK The space is dead.
181 * KERN_INVALID_NAME A name doesn't denote a right.
182 * KERN_INVALID_RIGHT A name doesn't denote the correct right.
183 */
184
185kern_return_t
186ipc_object_translate_two(
187 ipc_space_t space,
188 mach_port_name_t name1,
189 mach_port_right_t right1,
190 ipc_object_t *objectp1,
191 mach_port_name_t name2,
192 mach_port_right_t right2,
193 ipc_object_t *objectp2)
194{
195 ipc_entry_t entry1;
196 ipc_entry_t entry2;
197 ipc_object_t object;
198 kern_return_t kr;
199
200 kr = ipc_right_lookup_two_read(space, name1, &entry1, name2, &entry2);
201 if (kr != KERN_SUCCESS)
202 return kr;
203 /* space is read-locked and active */
204
205 if ((entry1->ie_bits & MACH_PORT_TYPE(right1)) == MACH_PORT_TYPE_NONE) {
206 is_read_unlock(space);
207 return KERN_INVALID_RIGHT;
208 }
209
210 if ((entry2->ie_bits & MACH_PORT_TYPE(right2)) == MACH_PORT_TYPE_NONE) {
211 is_read_unlock(space);
212 return KERN_INVALID_RIGHT;
213 }
214
215 object = entry1->ie_object;
216 assert(object != IO_NULL);
217 io_lock(object);
218 *objectp1 = object;
219
220 object = entry2->ie_object;
221 assert(object != IO_NULL);
222 io_lock(object);
223 *objectp2 = object;
224
225 is_read_unlock(space);
226 return KERN_SUCCESS;
227}
228
229/*
230 * Routine: ipc_object_alloc_dead
231 * Purpose:
232 * Allocate a dead-name entry.
233 * Conditions:
234 * Nothing locked.
235 * Returns:
236 * KERN_SUCCESS The dead name is allocated.
237 * KERN_INVALID_TASK The space is dead.
238 * KERN_NO_SPACE No room for an entry in the space.
239 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
240 */
241
242kern_return_t
243ipc_object_alloc_dead(
244 ipc_space_t space,
245 mach_port_name_t *namep)
246{
247 ipc_entry_t entry;
248 kern_return_t kr;
249
1c79356b
A
250 kr = ipc_entry_alloc(space, namep, &entry);
251 if (kr != KERN_SUCCESS)
252 return kr;
253 /* space is write-locked */
254
255 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
256
257 assert(entry->ie_object == IO_NULL);
258 entry->ie_bits |= MACH_PORT_TYPE_DEAD_NAME | 1;
316670eb 259 ipc_entry_modified(space, *namep, entry);
1c79356b
A
260 is_write_unlock(space);
261 return KERN_SUCCESS;
262}
263
264/*
265 * Routine: ipc_object_alloc_dead_name
266 * Purpose:
267 * Allocate a dead-name entry, with a specific name.
268 * Conditions:
269 * Nothing locked.
270 * Returns:
271 * KERN_SUCCESS The dead name is allocated.
272 * KERN_INVALID_TASK The space is dead.
273 * KERN_NAME_EXISTS The name already denotes a right.
274 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
275 */
276
277kern_return_t
278ipc_object_alloc_dead_name(
279 ipc_space_t space,
280 mach_port_name_t name)
281{
282 ipc_entry_t entry;
283 kern_return_t kr;
284
1c79356b
A
285 kr = ipc_entry_alloc_name(space, name, &entry);
286 if (kr != KERN_SUCCESS)
287 return kr;
288 /* space is write-locked */
289
290 if (ipc_right_inuse(space, name, entry))
291 return KERN_NAME_EXISTS;
292
293 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
294
295 assert(entry->ie_object == IO_NULL);
296 entry->ie_bits |= MACH_PORT_TYPE_DEAD_NAME | 1;
316670eb 297 ipc_entry_modified(space, name, entry);
1c79356b
A
298 is_write_unlock(space);
299 return KERN_SUCCESS;
300}
301
302/*
303 * Routine: ipc_object_alloc
304 * Purpose:
305 * Allocate an object.
306 * Conditions:
307 * Nothing locked. If successful, the object is returned locked.
99c3a104 308 * The space is write locked on successful return.
1c79356b
A
309 * The caller doesn't get a reference for the object.
310 * Returns:
311 * KERN_SUCCESS The object is allocated.
312 * KERN_INVALID_TASK The space is dead.
313 * KERN_NO_SPACE No room for an entry in the space.
314 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
315 */
316
317kern_return_t
318ipc_object_alloc(
319 ipc_space_t space,
320 ipc_object_type_t otype,
321 mach_port_type_t type,
322 mach_port_urefs_t urefs,
323 mach_port_name_t *namep,
324 ipc_object_t *objectp)
325{
326 ipc_object_t object;
327 ipc_entry_t entry;
328 kern_return_t kr;
329
330 assert(otype < IOT_NUMBER);
331 assert((type & MACH_PORT_TYPE_ALL_RIGHTS) == type);
332 assert(type != MACH_PORT_TYPE_NONE);
333 assert(urefs <= MACH_PORT_UREFS_MAX);
334
335 object = io_alloc(otype);
336 if (object == IO_NULL)
337 return KERN_RESOURCE_SHORTAGE;
338
339 if (otype == IOT_PORT) {
340 ipc_port_t port = (ipc_port_t)object;
341
342 bzero((char *)port, sizeof(*port));
343 } else if (otype == IOT_PORT_SET) {
344 ipc_pset_t pset = (ipc_pset_t)object;
345
346 bzero((char *)pset, sizeof(*pset));
347 }
348
349 io_lock_init(object);
b0d623f7 350 *namep = CAST_MACH_PORT_TO_NAME(object);
1c79356b
A
351 kr = ipc_entry_alloc(space, namep, &entry);
352 if (kr != KERN_SUCCESS) {
353 io_free(otype, object);
354 return kr;
355 }
356 /* space is write-locked */
357
358 entry->ie_bits |= type | urefs;
359 entry->ie_object = object;
316670eb 360 ipc_entry_modified(space, *namep, entry);
1c79356b
A
361
362 io_lock(object);
1c79356b
A
363
364 object->io_references = 1; /* for entry, not caller */
365 object->io_bits = io_makebits(TRUE, otype, 0);
366
367 *objectp = object;
368 return KERN_SUCCESS;
369}
370
371/*
372 * Routine: ipc_object_alloc_name
373 * Purpose:
374 * Allocate an object, with a specific name.
375 * Conditions:
376 * Nothing locked. If successful, the object is returned locked.
377 * The caller doesn't get a reference for the object.
378 * Returns:
379 * KERN_SUCCESS The object is allocated.
380 * KERN_INVALID_TASK The space is dead.
381 * KERN_NAME_EXISTS The name already denotes a right.
382 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
383 */
384
385kern_return_t
386ipc_object_alloc_name(
387 ipc_space_t space,
388 ipc_object_type_t otype,
389 mach_port_type_t type,
390 mach_port_urefs_t urefs,
391 mach_port_name_t name,
392 ipc_object_t *objectp)
393{
394 ipc_object_t object;
395 ipc_entry_t entry;
396 kern_return_t kr;
397
398 assert(otype < IOT_NUMBER);
399 assert((type & MACH_PORT_TYPE_ALL_RIGHTS) == type);
400 assert(type != MACH_PORT_TYPE_NONE);
401 assert(urefs <= MACH_PORT_UREFS_MAX);
402
403 object = io_alloc(otype);
404 if (object == IO_NULL)
405 return KERN_RESOURCE_SHORTAGE;
406
407 if (otype == IOT_PORT) {
408 ipc_port_t port = (ipc_port_t)object;
409
410 bzero((char *)port, sizeof(*port));
411 } else if (otype == IOT_PORT_SET) {
412 ipc_pset_t pset = (ipc_pset_t)object;
413
414 bzero((char *)pset, sizeof(*pset));
415 }
416
417 io_lock_init(object);
418 kr = ipc_entry_alloc_name(space, name, &entry);
419 if (kr != KERN_SUCCESS) {
420 io_free(otype, object);
421 return kr;
422 }
423 /* space is write-locked */
424
425 if (ipc_right_inuse(space, name, entry)) {
426 io_free(otype, object);
427 return KERN_NAME_EXISTS;
428 }
429
430 entry->ie_bits |= type | urefs;
431 entry->ie_object = object;
316670eb 432 ipc_entry_modified(space, name, entry);
1c79356b
A
433
434 io_lock(object);
435 is_write_unlock(space);
436
437 object->io_references = 1; /* for entry, not caller */
438 object->io_bits = io_makebits(TRUE, otype, 0);
439
440 *objectp = object;
441 return KERN_SUCCESS;
442}
443
444/*
445 * Routine: ipc_object_copyin_type
446 * Purpose:
447 * Convert a send type name to a received type name.
448 */
449
450mach_msg_type_name_t
451ipc_object_copyin_type(
452 mach_msg_type_name_t msgt_name)
453{
454 switch (msgt_name) {
455
456 case MACH_MSG_TYPE_MOVE_RECEIVE:
1c79356b
A
457 return MACH_MSG_TYPE_PORT_RECEIVE;
458
459 case MACH_MSG_TYPE_MOVE_SEND_ONCE:
460 case MACH_MSG_TYPE_MAKE_SEND_ONCE:
461 return MACH_MSG_TYPE_PORT_SEND_ONCE;
462
463 case MACH_MSG_TYPE_MOVE_SEND:
464 case MACH_MSG_TYPE_MAKE_SEND:
465 case MACH_MSG_TYPE_COPY_SEND:
466 return MACH_MSG_TYPE_PORT_SEND;
467
fe8ab488
A
468 case MACH_MSG_TYPE_DISPOSE_RECEIVE:
469 case MACH_MSG_TYPE_DISPOSE_SEND:
470 case MACH_MSG_TYPE_DISPOSE_SEND_ONCE:
471 /* fall thru */
1c79356b
A
472 default:
473 return MACH_MSG_TYPE_PORT_NONE;
474 }
475}
476
477/*
478 * Routine: ipc_object_copyin
479 * Purpose:
480 * Copyin a capability from a space.
481 * If successful, the caller gets a ref
482 * for the resulting object, unless it is IO_DEAD.
483 * Conditions:
484 * Nothing locked.
485 * Returns:
486 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
487 * KERN_INVALID_TASK The space is dead.
488 * KERN_INVALID_NAME Name doesn't exist in space.
489 * KERN_INVALID_RIGHT Name doesn't denote correct right.
490 */
491
492kern_return_t
493ipc_object_copyin(
494 ipc_space_t space,
495 mach_port_name_t name,
496 mach_msg_type_name_t msgt_name,
497 ipc_object_t *objectp)
498{
499 ipc_entry_t entry;
500 ipc_port_t soright;
316670eb 501 ipc_port_t release_port;
1c79356b 502 kern_return_t kr;
39236c6e 503 int assertcnt = 0;
1c79356b 504
1c79356b
A
505 /*
506 * Could first try a read lock when doing
507 * MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND,
508 * and MACH_MSG_TYPE_MAKE_SEND_ONCE.
509 */
510
511 kr = ipc_right_lookup_write(space, name, &entry);
512 if (kr != KERN_SUCCESS)
513 return kr;
514 /* space is write-locked and active */
515
316670eb 516 release_port = IP_NULL;
1c79356b
A
517 kr = ipc_right_copyin(space, name, entry,
518 msgt_name, TRUE,
316670eb
A
519 objectp, &soright,
520 &release_port,
3e170ce0 521 &assertcnt);
1c79356b
A
522 if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
523 ipc_entry_dealloc(space, name, entry);
524 is_write_unlock(space);
525
39236c6e 526#if IMPORTANCE_INHERITANCE
fe8ab488
A
527 if (0 < assertcnt && ipc_importance_task_is_any_receiver_type(current_task()->task_imp_base)) {
528 ipc_importance_task_drop_internal_assertion(current_task()->task_imp_base, assertcnt);
39236c6e
A
529 }
530#endif /* IMPORTANCE_INHERITANCE */
531
316670eb
A
532 if (release_port != IP_NULL)
533 ip_release(release_port);
534
1c79356b
A
535 if ((kr == KERN_SUCCESS) && (soright != IP_NULL))
536 ipc_notify_port_deleted(soright, name);
537
538 return kr;
539}
540
541/*
542 * Routine: ipc_object_copyin_from_kernel
543 * Purpose:
544 * Copyin a naked capability from the kernel.
545 *
546 * MACH_MSG_TYPE_MOVE_RECEIVE
55e303ae
A
547 * The receiver must be ipc_space_kernel
548 * or the receive right must already be in limbo.
1c79356b
A
549 * Consumes the naked receive right.
550 * MACH_MSG_TYPE_COPY_SEND
551 * A naked send right must be supplied.
552 * The port gains a reference, and a send right
553 * if the port is still active.
554 * MACH_MSG_TYPE_MAKE_SEND
555 * The receiver must be ipc_space_kernel.
556 * The port gains a reference and a send right.
557 * MACH_MSG_TYPE_MOVE_SEND
558 * Consumes a naked send right.
559 * MACH_MSG_TYPE_MAKE_SEND_ONCE
560 * The port gains a reference and a send-once right.
561 * Receiver also be the caller of device subsystem,
562 * so no assertion.
563 * MACH_MSG_TYPE_MOVE_SEND_ONCE
564 * Consumes a naked send-once right.
565 * Conditions:
566 * Nothing locked.
567 */
568
569void
570ipc_object_copyin_from_kernel(
571 ipc_object_t object,
572 mach_msg_type_name_t msgt_name)
573{
574 assert(IO_VALID(object));
575
576 switch (msgt_name) {
577 case MACH_MSG_TYPE_MOVE_RECEIVE: {
578 ipc_port_t port = (ipc_port_t) object;
579
580 ip_lock(port);
581 assert(ip_active(port));
55e303ae
A
582 if (port->ip_destination != IP_NULL) {
583 assert(port->ip_receiver == ipc_space_kernel);
1c79356b 584
55e303ae
A
585 /* relevant part of ipc_port_clear_receiver */
586 ipc_port_set_mscount(port, 0);
1c79356b 587
55e303ae
A
588 port->ip_receiver_name = MACH_PORT_NULL;
589 port->ip_destination = IP_NULL;
590 }
1c79356b
A
591 ip_unlock(port);
592 break;
593 }
594
595 case MACH_MSG_TYPE_COPY_SEND: {
596 ipc_port_t port = (ipc_port_t) object;
597
598 ip_lock(port);
599 if (ip_active(port)) {
600 assert(port->ip_srights > 0);
601 port->ip_srights++;
602 }
603 ip_reference(port);
604 ip_unlock(port);
605 break;
606 }
607
608 case MACH_MSG_TYPE_MAKE_SEND: {
609 ipc_port_t port = (ipc_port_t) object;
610
611 ip_lock(port);
39236c6e
A
612 if (ip_active(port)) {
613 assert(port->ip_receiver_name != MACH_PORT_NULL);
39037602
A
614 assert((port->ip_receiver == ipc_space_kernel) ||
615 (port->ip_receiver->is_node_id != HOST_LOCAL_NODE));
39236c6e
A
616 port->ip_mscount++;
617 }
1c79356b 618
1c79356b 619 port->ip_srights++;
39236c6e 620 ip_reference(port);
1c79356b
A
621 ip_unlock(port);
622 break;
623 }
624
55e303ae 625 case MACH_MSG_TYPE_MOVE_SEND: {
1c79356b 626 /* move naked send right into the message */
91447636 627 assert(((ipc_port_t)object)->ip_srights);
1c79356b 628 break;
55e303ae 629 }
1c79356b
A
630
631 case MACH_MSG_TYPE_MAKE_SEND_ONCE: {
632 ipc_port_t port = (ipc_port_t) object;
633
634 ip_lock(port);
39236c6e
A
635 if (ip_active(port)) {
636 assert(port->ip_receiver_name != MACH_PORT_NULL);
637 }
1c79356b 638 port->ip_sorights++;
39236c6e 639 ip_reference(port);
1c79356b
A
640 ip_unlock(port);
641 break;
642 }
643
55e303ae 644 case MACH_MSG_TYPE_MOVE_SEND_ONCE: {
1c79356b 645 /* move naked send-once right into the message */
91447636 646 assert(((ipc_port_t)object)->ip_sorights);
1c79356b 647 break;
55e303ae 648 }
1c79356b
A
649
650 default:
651 panic("ipc_object_copyin_from_kernel: strange rights");
652 }
653}
654
655/*
656 * Routine: ipc_object_destroy
657 * Purpose:
658 * Destroys a naked capability.
659 * Consumes a ref for the object.
660 *
661 * A receive right should be in limbo or in transit.
662 * Conditions:
663 * Nothing locked.
664 */
665
666void
667ipc_object_destroy(
668 ipc_object_t object,
669 mach_msg_type_name_t msgt_name)
670{
671 assert(IO_VALID(object));
672 assert(io_otype(object) == IOT_PORT);
673
674 switch (msgt_name) {
675 case MACH_MSG_TYPE_PORT_SEND:
676 ipc_port_release_send((ipc_port_t) object);
677 break;
678
679 case MACH_MSG_TYPE_PORT_SEND_ONCE:
680 ipc_notify_send_once((ipc_port_t) object);
681 break;
682
683 case MACH_MSG_TYPE_PORT_RECEIVE:
684 ipc_port_release_receive((ipc_port_t) object);
685 break;
686
687 default:
688 panic("ipc_object_destroy: strange rights");
689 }
690}
691
6d2010ae
A
692/*
693 * Routine: ipc_object_destroy_dest
694 * Purpose:
695 * Destroys a naked capability for the destination of
696 * of a message. Consumes a ref for the object.
697 *
698 * Conditions:
699 * Nothing locked.
700 */
701
702void
703ipc_object_destroy_dest(
704 ipc_object_t object,
705 mach_msg_type_name_t msgt_name)
706{
707 assert(IO_VALID(object));
708 assert(io_otype(object) == IOT_PORT);
709
710 switch (msgt_name) {
711 case MACH_MSG_TYPE_PORT_SEND:
712 ipc_port_release_send((ipc_port_t) object);
713 break;
714
715 case MACH_MSG_TYPE_PORT_SEND_ONCE:
716 if (io_active(object) &&
717 !ip_full_kernel((ipc_port_t) object))
718 ipc_notify_send_once((ipc_port_t) object);
719 else
720 ipc_port_release_sonce((ipc_port_t) object);
721 break;
722
723 default:
724 panic("ipc_object_destroy_dest: strange rights");
725 }
726}
727
1c79356b
A
728/*
729 * Routine: ipc_object_copyout
730 * Purpose:
731 * Copyout a capability, placing it into a space.
732 * If successful, consumes a ref for the object.
733 * Conditions:
734 * Nothing locked.
735 * Returns:
736 * KERN_SUCCESS Copied out object, consumed ref.
737 * KERN_INVALID_TASK The space is dead.
738 * KERN_INVALID_CAPABILITY The object is dead.
739 * KERN_NO_SPACE No room in space for another right.
740 * KERN_RESOURCE_SHORTAGE No memory available.
741 * KERN_UREFS_OVERFLOW Urefs limit exceeded
742 * and overflow wasn't specified.
743 */
744
745kern_return_t
746ipc_object_copyout(
747 ipc_space_t space,
748 ipc_object_t object,
749 mach_msg_type_name_t msgt_name,
750 boolean_t overflow,
751 mach_port_name_t *namep)
752{
753 mach_port_name_t name;
754 ipc_entry_t entry;
755 kern_return_t kr;
756
757 assert(IO_VALID(object));
758 assert(io_otype(object) == IOT_PORT);
759
760 is_write_lock(space);
761
762 for (;;) {
316670eb 763 if (!is_active(space)) {
1c79356b
A
764 is_write_unlock(space);
765 return KERN_INVALID_TASK;
766 }
767
768 if ((msgt_name != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
769 ipc_right_reverse(space, object, &name, &entry)) {
770 /* object is locked and active */
771
772 assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
773 break;
774 }
775
b0d623f7 776 name = CAST_MACH_PORT_TO_NAME(object);
1c79356b
A
777 kr = ipc_entry_get(space, &name, &entry);
778 if (kr != KERN_SUCCESS) {
779 /* unlocks/locks space, so must start again */
780
781 kr = ipc_entry_grow_table(space, ITS_SIZE_NONE);
782 if (kr != KERN_SUCCESS)
783 return kr; /* space is unlocked */
784
785 continue;
786 }
787
788 assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE);
789 assert(entry->ie_object == IO_NULL);
790
791 io_lock(object);
792 if (!io_active(object)) {
793 io_unlock(object);
794 ipc_entry_dealloc(space, name, entry);
795 is_write_unlock(space);
796 return KERN_INVALID_CAPABILITY;
797 }
798
799 entry->ie_object = object;
800 break;
801 }
802
803 /* space is write-locked and active, object is locked and active */
804
805 kr = ipc_right_copyout(space, name, entry,
806 msgt_name, overflow, object);
39236c6e 807
1c79356b
A
808 /* object is unlocked */
809 is_write_unlock(space);
810
811 if (kr == KERN_SUCCESS)
812 *namep = name;
813 return kr;
814}
815
816/*
817 * Routine: ipc_object_copyout_name
818 * Purpose:
819 * Copyout a capability, placing it into a space.
820 * The specified name is used for the capability.
821 * If successful, consumes a ref for the object.
822 * Conditions:
823 * Nothing locked.
824 * Returns:
825 * KERN_SUCCESS Copied out object, consumed ref.
826 * KERN_INVALID_TASK The space is dead.
827 * KERN_INVALID_CAPABILITY The object is dead.
828 * KERN_RESOURCE_SHORTAGE No memory available.
829 * KERN_UREFS_OVERFLOW Urefs limit exceeded
830 * and overflow wasn't specified.
831 * KERN_RIGHT_EXISTS Space has rights under another name.
832 * KERN_NAME_EXISTS Name is already used.
833 */
834
835kern_return_t
836ipc_object_copyout_name(
837 ipc_space_t space,
838 ipc_object_t object,
839 mach_msg_type_name_t msgt_name,
840 boolean_t overflow,
841 mach_port_name_t name)
842{
843 mach_port_name_t oname;
844 ipc_entry_t oentry;
845 ipc_entry_t entry;
846 kern_return_t kr;
847
39236c6e
A
848#if IMPORTANCE_INHERITANCE
849 int assertcnt = 0;
fe8ab488 850 ipc_importance_task_t task_imp = IIT_NULL;
39236c6e
A
851#endif /* IMPORTANCE_INHERITANCE */
852
1c79356b
A
853 assert(IO_VALID(object));
854 assert(io_otype(object) == IOT_PORT);
855
856 kr = ipc_entry_alloc_name(space, name, &entry);
857 if (kr != KERN_SUCCESS)
858 return kr;
859 /* space is write-locked and active */
860
861 if ((msgt_name != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
862 ipc_right_reverse(space, object, &oname, &oentry)) {
863 /* object is locked and active */
864
865 if (name != oname) {
866 io_unlock(object);
867
868 if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
869 ipc_entry_dealloc(space, name, entry);
870
871 is_write_unlock(space);
872 return KERN_RIGHT_EXISTS;
873 }
874
875 assert(entry == oentry);
876 assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
877 } else {
878 if (ipc_right_inuse(space, name, entry))
879 return KERN_NAME_EXISTS;
880
881 assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE);
882 assert(entry->ie_object == IO_NULL);
883
884 io_lock(object);
885 if (!io_active(object)) {
886 io_unlock(object);
887 ipc_entry_dealloc(space, name, entry);
888 is_write_unlock(space);
889 return KERN_INVALID_CAPABILITY;
890 }
891
892 entry->ie_object = object;
893 }
894
895 /* space is write-locked and active, object is locked and active */
896
39236c6e
A
897#if IMPORTANCE_INHERITANCE
898 /*
899 * We are slamming a receive right into the space, without
900 * first having been enqueued on a port destined there. So,
901 * we have to arrange to boost the task appropriately if this
902 * port has assertions (and the task wants them).
903 */
904 if (msgt_name == MACH_MSG_TYPE_PORT_RECEIVE) {
905 ipc_port_t port = (ipc_port_t)object;
906
fe8ab488
A
907 if (space->is_task != TASK_NULL) {
908 task_imp = space->is_task->task_imp_base;
909 if (ipc_importance_task_is_any_receiver_type(task_imp)) {
910 assertcnt = port->ip_impcount;
911 ipc_importance_task_reference(task_imp);
912 }
39236c6e
A
913 }
914
915 /* take port out of limbo */
916 assert(port->ip_tempowner != 0);
917 port->ip_tempowner = 0;
918 }
919
920#endif /* IMPORTANCE_INHERITANCE */
921
1c79356b
A
922 kr = ipc_right_copyout(space, name, entry,
923 msgt_name, overflow, object);
39236c6e 924
1c79356b
A
925 /* object is unlocked */
926 is_write_unlock(space);
39236c6e
A
927
928#if IMPORTANCE_INHERITANCE
929 /*
930 * Add the assertions to the task that we captured before
931 */
fe8ab488
A
932 if (task_imp != IIT_NULL) {
933 ipc_importance_task_hold_internal_assertion(task_imp, assertcnt);
934 ipc_importance_task_release(task_imp);
39236c6e
A
935 }
936#endif /* IMPORTANCE_INHERITANCE */
937
1c79356b
A
938 return kr;
939}
940
941/*
942 * Routine: ipc_object_copyout_dest
943 * Purpose:
944 * Translates/consumes the destination right of a message.
945 * This is unlike normal copyout because the right is consumed
946 * in a funny way instead of being given to the receiving space.
947 * The receiver gets his name for the port, if he has receive
948 * rights, otherwise MACH_PORT_NULL.
949 * Conditions:
950 * The object is locked and active. Nothing else locked.
951 * The object is unlocked and loses a reference.
952 */
953
954void
955ipc_object_copyout_dest(
956 ipc_space_t space,
957 ipc_object_t object,
958 mach_msg_type_name_t msgt_name,
959 mach_port_name_t *namep)
960{
961 mach_port_name_t name;
962
963 assert(IO_VALID(object));
964 assert(io_active(object));
965
966 io_release(object);
967
968 /*
969 * If the space is the receiver/owner of the object,
970 * then we quietly consume the right and return
971 * the space's name for the object. Otherwise
972 * we destroy the right and return MACH_PORT_NULL.
973 */
974
975 switch (msgt_name) {
976 case MACH_MSG_TYPE_PORT_SEND: {
977 ipc_port_t port = (ipc_port_t) object;
978 ipc_port_t nsrequest = IP_NULL;
979 mach_port_mscount_t mscount;
980
981 if (port->ip_receiver == space)
982 name = port->ip_receiver_name;
983 else
984 name = MACH_PORT_NULL;
985
986 assert(port->ip_srights > 0);
987 if (--port->ip_srights == 0 &&
988 port->ip_nsrequest != IP_NULL) {
989 nsrequest = port->ip_nsrequest;
990 port->ip_nsrequest = IP_NULL;
991 mscount = port->ip_mscount;
992 ip_unlock(port);
993 ipc_notify_no_senders(nsrequest, mscount);
994 } else
995 ip_unlock(port);
996 break;
997 }
998
999 case MACH_MSG_TYPE_PORT_SEND_ONCE: {
1000 ipc_port_t port = (ipc_port_t) object;
1001
1002 assert(port->ip_sorights > 0);
1003
1004 if (port->ip_receiver == space) {
1005 /* quietly consume the send-once right */
1006
1007 port->ip_sorights--;
1008 name = port->ip_receiver_name;
1009 ip_unlock(port);
1010 } else {
1011 /*
1012 * A very bizarre case. The message
1013 * was received, but before this copyout
1014 * happened the space lost receive rights.
1015 * We can't quietly consume the soright
1016 * out from underneath some other task,
1017 * so generate a send-once notification.
1018 */
1019
1020 ip_reference(port); /* restore ref */
1021 ip_unlock(port);
1022
1023 ipc_notify_send_once(port);
1024 name = MACH_PORT_NULL;
1025 }
1026
1027 break;
1028 }
1029
1030 default:
1031 panic("ipc_object_copyout_dest: strange rights");
91447636 1032 name = MACH_PORT_DEAD;
1c79356b
A
1033 }
1034
1035 *namep = name;
1036}
1037
1038/*
1039 * Routine: ipc_object_rename
1040 * Purpose:
1041 * Rename an entry in a space.
1042 * Conditions:
1043 * Nothing locked.
1044 * Returns:
1045 * KERN_SUCCESS Renamed the entry.
1046 * KERN_INVALID_TASK The space was dead.
1047 * KERN_INVALID_NAME oname didn't denote an entry.
1048 * KERN_NAME_EXISTS nname already denoted an entry.
1049 * KERN_RESOURCE_SHORTAGE Couldn't allocate new entry.
1050 */
1051
1052kern_return_t
1053ipc_object_rename(
1054 ipc_space_t space,
1055 mach_port_name_t oname,
1056 mach_port_name_t nname)
1057{
1058 ipc_entry_t oentry, nentry;
1059 kern_return_t kr;
1060
1c79356b
A
1061 kr = ipc_entry_alloc_name(space, nname, &nentry);
1062 if (kr != KERN_SUCCESS)
1063 return kr;
1064
1065 /* space is write-locked and active */
1066
1067 if (ipc_right_inuse(space, nname, nentry)) {
1068 /* space is unlocked */
1069 return KERN_NAME_EXISTS;
1070 }
1071
1072 /* don't let ipc_entry_lookup see the uninitialized new entry */
1073
1074 if ((oname == nname) ||
1075 ((oentry = ipc_entry_lookup(space, oname)) == IE_NULL)) {
1076 ipc_entry_dealloc(space, nname, nentry);
1077 is_write_unlock(space);
1078 return KERN_INVALID_NAME;
1079 }
1080
1081 kr = ipc_right_rename(space, oname, oentry, nname, nentry);
1082 /* space is unlocked */
1083 return kr;
1084}
1085
1c79356b
A
1086/*
1087 * Check whether the object is a port if so, free it. But
1088 * keep track of that fact.
1089 */
1090void
1091io_free(
1092 unsigned int otype,
1093 ipc_object_t object)
1094{
1095 ipc_port_t port;
1096
1097 if (otype == IOT_PORT) {
1098 port = (ipc_port_t) object;
6d2010ae 1099 ipc_port_finalize(port);
1c79356b 1100 }
b0d623f7 1101 io_lock_destroy(object);
91447636 1102 zfree(ipc_object_zones[otype], object);
1c79356b 1103}