2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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,
61 * Copyright (c) 2005-2006 SPARTA, Inc.
66 * File: ipc/ipc_object.c
70 * Functions to manipulate IPC objects.
73 #include <mach/mach_types.h>
74 #include <mach/boolean.h>
75 #include <mach/kern_return.h>
76 #include <mach/port.h>
77 #include <mach/message.h>
79 #include <kern/kern_types.h>
80 #include <kern/misc_protos.h>
81 #include <kern/ipc_kobject.h>
83 #include <ipc/ipc_types.h>
84 #include <ipc/ipc_importance.h>
86 #include <ipc/ipc_space.h>
87 #include <ipc/ipc_entry.h>
88 #include <ipc/ipc_object.h>
89 #include <ipc/ipc_hash.h>
90 #include <ipc/ipc_right.h>
91 #include <ipc/ipc_notify.h>
92 #include <ipc/ipc_port.h>
93 #include <ipc/ipc_pset.h>
95 #include <security/mac_mach_internal.h>
97 SECURITY_READ_ONLY_LATE(zone_t
) ipc_object_zones
[IOT_NUMBER
];
99 ZONE_INIT(&ipc_object_zones
[IOT_PORT
], "ipc ports", sizeof(struct ipc_port
),
100 ZC_NOENCRYPT
| ZC_CACHING
| ZC_ZFREE_CLEARMEM
| ZC_NOSEQUESTER
,
101 ZONE_ID_IPC_PORT
, NULL
);
103 ZONE_INIT(&ipc_object_zones
[IOT_PORT_SET
], "ipc port sets",
104 sizeof(struct ipc_pset
),
105 ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
| ZC_NOSEQUESTER
,
106 ZONE_ID_IPC_PORT_SET
, NULL
);
109 * Routine: ipc_object_reference
111 * Take a reference to an object.
115 ipc_object_reference(
118 io_reference(object
);
122 * Routine: ipc_object_release
124 * Release a reference to an object.
135 * Routine: ipc_object_translate
137 * Look up an object in a space.
139 * Nothing locked before. If successful, the object
140 * is returned active and locked. The caller doesn't get a ref.
142 * KERN_SUCCESS Object returned locked.
143 * KERN_INVALID_TASK The space is dead.
144 * KERN_INVALID_NAME The name doesn't denote a right
145 * KERN_INVALID_RIGHT Name doesn't denote the correct right
148 ipc_object_translate(
150 mach_port_name_t name
,
151 mach_port_right_t right
,
152 ipc_object_t
*objectp
)
158 if (!MACH_PORT_RIGHT_VALID_TRANSLATE(right
)) {
159 return KERN_INVALID_RIGHT
;
162 kr
= ipc_right_lookup_read(space
, name
, &entry
);
163 if (kr
!= KERN_SUCCESS
) {
166 /* space is read-locked and active */
168 if ((entry
->ie_bits
& MACH_PORT_TYPE(right
)) == MACH_PORT_TYPE_NONE
) {
169 is_read_unlock(space
);
170 return KERN_INVALID_RIGHT
;
173 object
= entry
->ie_object
;
174 assert(object
!= IO_NULL
);
177 is_read_unlock(space
);
179 if (!io_active(object
)) {
181 return KERN_INVALID_NAME
;
189 * Routine: ipc_object_translate_two
191 * Look up two objects in a space.
193 * Nothing locked before. If successful, the objects
194 * are returned locked. The caller doesn't get a ref.
196 * KERN_SUCCESS Objects returned locked.
197 * KERN_INVALID_TASK The space is dead.
198 * KERN_INVALID_NAME A name doesn't denote a right.
199 * KERN_INVALID_RIGHT A name doesn't denote the correct right.
203 ipc_object_translate_two(
205 mach_port_name_t name1
,
206 mach_port_right_t right1
,
207 ipc_object_t
*objectp1
,
208 mach_port_name_t name2
,
209 mach_port_right_t right2
,
210 ipc_object_t
*objectp2
)
214 ipc_object_t object1
, object2
;
216 boolean_t doguard
= TRUE
;
218 kr
= ipc_right_lookup_two_read(space
, name1
, &entry1
, name2
, &entry2
);
219 if (kr
!= KERN_SUCCESS
) {
222 /* space is read-locked and active */
224 if ((entry1
->ie_bits
& MACH_PORT_TYPE(right1
)) == MACH_PORT_TYPE_NONE
) {
225 /* If looking for receive, and the entry used to hold one, give a pass on EXC_GUARD */
226 if ((right1
& MACH_PORT_RIGHT_RECEIVE
) == MACH_PORT_RIGHT_RECEIVE
&&
227 (entry1
->ie_bits
& MACH_PORT_TYPE_EX_RECEIVE
) == MACH_PORT_TYPE_EX_RECEIVE
) {
230 is_read_unlock(space
);
232 mach_port_guard_exception(name1
, 0, 0, kGUARD_EXC_INVALID_RIGHT
);
234 return KERN_INVALID_RIGHT
;
237 if ((entry2
->ie_bits
& MACH_PORT_TYPE(right2
)) == MACH_PORT_TYPE_NONE
) {
238 /* If looking for receive, and the entry used to hold one, give a pass on EXC_GUARD */
239 if ((right2
& MACH_PORT_RIGHT_RECEIVE
) == MACH_PORT_RIGHT_RECEIVE
&&
240 (entry2
->ie_bits
& MACH_PORT_TYPE_EX_RECEIVE
) == MACH_PORT_TYPE_EX_RECEIVE
) {
243 is_read_unlock(space
);
245 mach_port_guard_exception(name2
, 0, 0, kGUARD_EXC_INVALID_RIGHT
);
247 return KERN_INVALID_RIGHT
;
250 object1
= entry1
->ie_object
;
251 assert(object1
!= IO_NULL
);
253 if (!io_active(object1
)) {
255 is_read_unlock(space
);
256 return KERN_INVALID_NAME
;
259 object2
= entry2
->ie_object
;
260 assert(object2
!= IO_NULL
);
262 if (!io_active(object2
)) {
265 is_read_unlock(space
);
266 return KERN_INVALID_NAME
;
272 is_read_unlock(space
);
277 * Routine: ipc_object_alloc_dead
279 * Allocate a dead-name entry.
283 * KERN_SUCCESS The dead name is allocated.
284 * KERN_INVALID_TASK The space is dead.
285 * KERN_NO_SPACE No room for an entry in the space.
286 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
290 ipc_object_alloc_dead(
292 mach_port_name_t
*namep
)
297 kr
= ipc_entry_alloc(space
, namep
, &entry
);
298 if (kr
!= KERN_SUCCESS
) {
301 /* space is write-locked */
303 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
305 assert(entry
->ie_object
== IO_NULL
);
306 entry
->ie_bits
|= MACH_PORT_TYPE_DEAD_NAME
| 1;
307 ipc_entry_modified(space
, *namep
, entry
);
308 is_write_unlock(space
);
313 * Routine: ipc_object_alloc_dead_name
315 * Allocate a dead-name entry, with a specific name.
319 * KERN_SUCCESS The dead name is allocated.
320 * KERN_INVALID_TASK The space is dead.
321 * KERN_NAME_EXISTS The name already denotes a right.
322 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
326 ipc_object_alloc_dead_name(
328 mach_port_name_t name
)
333 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
334 if (kr
!= KERN_SUCCESS
) {
337 /* space is write-locked */
339 if (ipc_right_inuse(entry
)) {
340 is_write_unlock(space
);
341 return KERN_NAME_EXISTS
;
344 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
346 assert(entry
->ie_object
== IO_NULL
);
347 entry
->ie_bits
|= MACH_PORT_TYPE_DEAD_NAME
| 1;
348 ipc_entry_modified(space
, name
, entry
);
349 is_write_unlock(space
);
354 * Routine: ipc_object_alloc
356 * Allocate an object.
358 * Nothing locked. If successful, the object is returned locked.
359 * The space is write locked on successful return.
360 * The caller doesn't get a reference for the object.
362 * KERN_SUCCESS The object is allocated.
363 * KERN_INVALID_TASK The space is dead.
364 * KERN_NO_SPACE No room for an entry in the space.
365 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
371 ipc_object_type_t otype
,
372 mach_port_type_t type
,
373 mach_port_urefs_t urefs
,
374 mach_port_name_t
*namep
,
375 ipc_object_t
*objectp
)
381 assert(otype
< IOT_NUMBER
);
382 assert((type
& MACH_PORT_TYPE_ALL_RIGHTS
) == type
);
383 assert(type
!= MACH_PORT_TYPE_NONE
);
384 assert(urefs
<= MACH_PORT_UREFS_MAX
);
386 object
= io_alloc(otype
, Z_WAITOK
| Z_ZERO
);
387 if (object
== IO_NULL
) {
388 return KERN_RESOURCE_SHORTAGE
;
391 io_lock_init(object
);
392 *namep
= CAST_MACH_PORT_TO_NAME(object
);
393 kr
= ipc_entry_alloc(space
, namep
, &entry
);
394 if (kr
!= KERN_SUCCESS
) {
395 io_free(otype
, object
);
398 /* space is write-locked */
400 entry
->ie_bits
|= type
| urefs
;
401 entry
->ie_object
= object
;
402 ipc_entry_modified(space
, *namep
, entry
);
404 object
->io_bits
= io_makebits(TRUE
, otype
, 0);
407 object
->io_references
= 1; /* for entry, not caller */
414 * Routine: ipc_object_alloc_name
416 * Allocate an object, with a specific name.
418 * Nothing locked. If successful, the object is returned locked.
419 * The caller doesn't get a reference for the object.
421 * KERN_SUCCESS The object is allocated.
422 * KERN_INVALID_TASK The space is dead.
423 * KERN_NAME_EXISTS The name already denotes a right.
424 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
428 ipc_object_alloc_name(
430 ipc_object_type_t otype
,
431 mach_port_type_t type
,
432 mach_port_urefs_t urefs
,
433 mach_port_name_t name
,
434 ipc_object_t
*objectp
)
440 assert(otype
< IOT_NUMBER
);
441 assert((type
& MACH_PORT_TYPE_ALL_RIGHTS
) == type
);
442 assert(type
!= MACH_PORT_TYPE_NONE
);
443 assert(urefs
<= MACH_PORT_UREFS_MAX
);
445 object
= io_alloc(otype
, Z_WAITOK
| Z_ZERO
);
446 if (object
== IO_NULL
) {
447 return KERN_RESOURCE_SHORTAGE
;
450 io_lock_init(object
);
451 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
452 if (kr
!= KERN_SUCCESS
) {
453 io_free(otype
, object
);
456 /* space is write-locked */
458 if (ipc_right_inuse(entry
)) {
459 is_write_unlock(space
);
460 io_free(otype
, object
);
461 return KERN_NAME_EXISTS
;
464 entry
->ie_bits
|= type
| urefs
;
465 entry
->ie_object
= object
;
466 ipc_entry_modified(space
, name
, entry
);
468 object
->io_bits
= io_makebits(TRUE
, otype
, 0);
471 is_write_unlock(space
);
473 object
->io_references
= 1; /* for entry, not caller */
479 /* Routine: ipc_object_validate
481 * Validates an ipc port or port set as belonging to the correct
489 if (io_otype(object
) != IOT_PORT_SET
) {
490 zone_id_require(ZONE_ID_IPC_PORT
,
491 sizeof(struct ipc_port
), object
);
493 zone_id_require(ZONE_ID_IPC_PORT_SET
,
494 sizeof(struct ipc_pset
), object
);
499 * Routine: ipc_object_copyin_type
501 * Convert a send type name to a received type name.
505 ipc_object_copyin_type(
506 mach_msg_type_name_t msgt_name
)
509 case MACH_MSG_TYPE_MOVE_RECEIVE
:
510 return MACH_MSG_TYPE_PORT_RECEIVE
;
512 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
513 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
514 return MACH_MSG_TYPE_PORT_SEND_ONCE
;
516 case MACH_MSG_TYPE_MOVE_SEND
:
517 case MACH_MSG_TYPE_MAKE_SEND
:
518 case MACH_MSG_TYPE_COPY_SEND
:
519 return MACH_MSG_TYPE_PORT_SEND
;
521 case MACH_MSG_TYPE_DISPOSE_RECEIVE
:
522 case MACH_MSG_TYPE_DISPOSE_SEND
:
523 case MACH_MSG_TYPE_DISPOSE_SEND_ONCE
:
526 return MACH_MSG_TYPE_PORT_NONE
;
531 * Routine: ipc_object_copyin
533 * Copyin a capability from a space.
534 * If successful, the caller gets a ref
535 * for the resulting object, unless it is IO_DEAD.
539 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
540 * KERN_INVALID_TASK The space is dead.
541 * KERN_INVALID_NAME Name doesn't exist in space.
542 * KERN_INVALID_RIGHT Name doesn't denote correct right.
548 mach_port_name_t name
,
549 mach_msg_type_name_t msgt_name
,
550 ipc_object_t
*objectp
,
551 mach_port_context_t context
,
552 mach_msg_guard_flags_t
*guard_flags
,
553 ipc_object_copyin_flags_t copyin_flags
)
557 ipc_port_t release_port
;
561 ipc_object_copyin_flags_t irc_flags
= IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND
|
562 IPC_OBJECT_COPYIN_FLAGS_SOFT_FAIL_IMMOVABLE_SEND
;
563 irc_flags
= (copyin_flags
& irc_flags
) | IPC_OBJECT_COPYIN_FLAGS_DEADOK
;
565 * Could first try a read lock when doing
566 * MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND,
567 * and MACH_MSG_TYPE_MAKE_SEND_ONCE.
570 kr
= ipc_right_lookup_write(space
, name
, &entry
);
571 if (kr
!= KERN_SUCCESS
) {
574 /* space is write-locked and active */
576 release_port
= IP_NULL
;
577 kr
= ipc_right_copyin(space
, name
, entry
,
578 msgt_name
, irc_flags
,
584 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
) {
585 ipc_entry_dealloc(space
, name
, entry
);
587 is_write_unlock(space
);
589 #if IMPORTANCE_INHERITANCE
590 if (0 < assertcnt
&& ipc_importance_task_is_any_receiver_type(current_task()->task_imp_base
)) {
591 ipc_importance_task_drop_internal_assertion(current_task()->task_imp_base
, assertcnt
);
593 #endif /* IMPORTANCE_INHERITANCE */
595 if (release_port
!= IP_NULL
) {
596 ip_release(release_port
);
599 if ((kr
== KERN_SUCCESS
) && (soright
!= IP_NULL
)) {
600 ipc_notify_port_deleted(soright
, name
);
607 * Routine: ipc_object_copyin_from_kernel
609 * Copyin a naked capability from the kernel.
611 * MACH_MSG_TYPE_MOVE_RECEIVE
612 * The receiver must be ipc_space_kernel
613 * or the receive right must already be in limbo.
614 * Consumes the naked receive right.
615 * MACH_MSG_TYPE_COPY_SEND
616 * A naked send right must be supplied.
617 * The port gains a reference, and a send right
618 * if the port is still active.
619 * MACH_MSG_TYPE_MAKE_SEND
620 * The receiver must be ipc_space_kernel.
621 * The port gains a reference and a send right.
622 * MACH_MSG_TYPE_MOVE_SEND
623 * Consumes a naked send right.
624 * MACH_MSG_TYPE_MAKE_SEND_ONCE
625 * The port gains a reference and a send-once right.
626 * Receiver also be the caller of device subsystem,
628 * MACH_MSG_TYPE_MOVE_SEND_ONCE
629 * Consumes a naked send-once right.
635 ipc_object_copyin_from_kernel(
637 mach_msg_type_name_t msgt_name
)
639 assert(IO_VALID(object
));
642 case MACH_MSG_TYPE_MOVE_RECEIVE
: {
643 ipc_port_t port
= ip_object_to_port(object
);
646 imq_lock(&port
->ip_messages
);
647 require_ip_active(port
);
648 if (port
->ip_destination
!= IP_NULL
) {
649 assert(port
->ip_receiver
== ipc_space_kernel
);
650 assert(port
->ip_immovable_receive
== 0);
652 /* relevant part of ipc_port_clear_receiver */
653 port
->ip_mscount
= 0;
654 port
->ip_receiver_name
= MACH_PORT_NULL
;
655 port
->ip_destination
= IP_NULL
;
657 imq_unlock(&port
->ip_messages
);
662 case MACH_MSG_TYPE_COPY_SEND
: {
663 ipc_port_t port
= ip_object_to_port(object
);
666 if (ip_active(port
)) {
667 assert(port
->ip_srights
> 0);
675 case MACH_MSG_TYPE_MAKE_SEND
: {
676 ipc_port_t port
= ip_object_to_port(object
);
679 if (ip_active(port
)) {
680 assert(port
->ip_receiver_name
!= MACH_PORT_NULL
);
681 assert((port
->ip_receiver
== ipc_space_kernel
) ||
682 (port
->ip_receiver
->is_node_id
!= HOST_LOCAL_NODE
));
692 case MACH_MSG_TYPE_MOVE_SEND
: {
693 /* move naked send right into the message */
694 assert(ip_object_to_port(object
)->ip_srights
);
698 case MACH_MSG_TYPE_MAKE_SEND_ONCE
: {
699 ipc_port_t port
= ip_object_to_port(object
);
702 if (ip_active(port
)) {
703 assert(port
->ip_receiver_name
!= MACH_PORT_NULL
);
705 ipc_port_make_sonce_locked(port
);
710 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
711 /* move naked send-once right into the message */
712 assert(ip_object_to_port(object
)->ip_sorights
);
717 panic("ipc_object_copyin_from_kernel: strange rights");
722 * Routine: ipc_object_destroy
724 * Destroys a naked capability.
725 * Consumes a ref for the object.
727 * A receive right should be in limbo or in transit.
735 mach_msg_type_name_t msgt_name
)
737 assert(IO_VALID(object
));
738 assert(io_otype(object
) == IOT_PORT
);
741 case MACH_MSG_TYPE_PORT_SEND
:
742 ipc_port_release_send(ip_object_to_port(object
));
745 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
746 ipc_notify_send_once(ip_object_to_port(object
));
749 case MACH_MSG_TYPE_PORT_RECEIVE
:
750 ipc_port_release_receive(ip_object_to_port(object
));
754 panic("ipc_object_destroy: strange rights");
759 * Routine: ipc_object_destroy_dest
761 * Destroys a naked capability for the destination of
762 * of a message. Consumes a ref for the object.
769 ipc_object_destroy_dest(
771 mach_msg_type_name_t msgt_name
)
773 assert(IO_VALID(object
));
774 assert(io_otype(object
) == IOT_PORT
);
777 case MACH_MSG_TYPE_PORT_SEND
:
778 ipc_port_release_send(ip_object_to_port(object
));
781 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
782 if (io_active(object
) &&
783 !ip_full_kernel(ip_object_to_port(object
))) {
784 ipc_notify_send_once(ip_object_to_port(object
));
786 ipc_port_release_sonce(ip_object_to_port(object
));
791 panic("ipc_object_destroy_dest: strange rights");
796 * Routine: ipc_object_insert_send_right
798 * Insert a send right into an object already in the space.
799 * The specified name must already point to a valid object.
801 * Note: This really is a combined copyin()/copyout(),
802 * that avoids most of the overhead of being implemented that way.
804 * This is the fastpath for mach_port_insert_right.
809 * msgt_name must be MACH_MSG_TYPE_MAKE_SEND_ONCE or
810 * MACH_MSG_TYPE_MOVE_SEND_ONCE.
813 * KERN_SUCCESS Copied out object, consumed ref.
814 * KERN_INVALID_TASK The space is dead.
815 * KERN_INVALID_NAME Name doesn't exist in space.
816 * KERN_INVALID_CAPABILITY The object is dead.
817 * KERN_RIGHT_EXISTS Space has rights under another name.
820 ipc_object_insert_send_right(
822 mach_port_name_t name
,
823 mach_msg_type_name_t msgt_name
)
825 ipc_entry_bits_t bits
;
830 assert(msgt_name
== MACH_MSG_TYPE_MAKE_SEND
||
831 msgt_name
== MACH_MSG_TYPE_COPY_SEND
);
833 kr
= ipc_right_lookup_write(space
, name
, &entry
);
834 if (kr
!= KERN_SUCCESS
) {
837 /* space is write-locked and active */
839 if (!IO_VALID(entry
->ie_object
)) {
840 is_write_unlock(space
);
841 return KERN_INVALID_CAPABILITY
;
844 bits
= entry
->ie_bits
;
845 object
= entry
->ie_object
;
848 if (!io_active(object
)) {
849 kr
= KERN_INVALID_CAPABILITY
;
850 } else if (msgt_name
== MACH_MSG_TYPE_MAKE_SEND
) {
851 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
852 ipc_port_t port
= ip_object_to_port(object
);
854 if ((bits
& MACH_PORT_TYPE_SEND
) == 0) {
856 bits
|= MACH_PORT_TYPE_SEND
;
858 /* leave urefs pegged to maximum if it overflowed */
859 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
) {
860 bits
+= 1; /* increment urefs */
862 entry
->ie_bits
= bits
;
863 ipc_entry_modified(space
, name
, entry
);
866 kr
= KERN_INVALID_RIGHT
;
868 } else { // MACH_MSG_TYPE_COPY_SEND
869 if (bits
& MACH_PORT_TYPE_SEND
) {
870 /* leave urefs pegged to maximum if it overflowed */
871 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
) {
872 entry
->ie_bits
= bits
+ 1; /* increment urefs */
874 ipc_entry_modified(space
, name
, entry
);
877 kr
= KERN_INVALID_RIGHT
;
882 is_write_unlock(space
);
888 * Routine: ipc_object_copyout
890 * Copyout a capability, placing it into a space.
891 * Always consumes a ref for the object.
895 * KERN_SUCCESS Copied out object, consumed ref.
896 * KERN_INVALID_TASK The space is dead.
897 * KERN_INVALID_CAPABILITY The object is dead.
898 * KERN_NO_SPACE No room in space for another right.
899 * KERN_RESOURCE_SHORTAGE No memory available.
900 * KERN_UREFS_OVERFLOW Urefs limit exceeded
901 * and overflow wasn't specified.
908 mach_msg_type_name_t msgt_name
,
909 ipc_object_copyout_flags_t flags
,
910 mach_port_context_t
*context
,
911 mach_msg_guard_flags_t
*guard_flags
,
912 mach_port_name_t
*namep
)
914 struct knote
*kn
= current_thread()->ith_knote
;
915 mach_port_name_t name
;
916 ipc_port_t port
= ip_object_to_port(object
);
920 assert(IO_VALID(object
));
921 assert(io_otype(object
) == IOT_PORT
);
923 if (ITH_KNOTE_VALID(kn
, msgt_name
)) {
924 filt_machport_turnstile_prepare_lazily(kn
, msgt_name
, port
);
927 is_write_lock(space
);
930 ipc_port_t port_subst
= IP_NULL
;
932 if (!is_active(space
)) {
933 is_write_unlock(space
);
934 kr
= KERN_INVALID_TASK
;
938 kr
= ipc_entries_hold(space
, 1);
939 if (kr
!= KERN_SUCCESS
) {
940 /* unlocks/locks space, so must start again */
942 kr
= ipc_entry_grow_table(space
, ITS_SIZE_NONE
);
943 if (kr
!= KERN_SUCCESS
) {
944 /* space is unlocked */
951 if (!io_active(object
)) {
953 is_write_unlock(space
);
954 kr
= KERN_INVALID_CAPABILITY
;
958 /* Don't actually copyout rights we aren't allowed to */
959 if (!ip_label_check(space
, port
, msgt_name
, &flags
, &port_subst
)) {
961 is_write_unlock(space
);
962 assert(port_subst
== IP_NULL
);
963 kr
= KERN_INVALID_CAPABILITY
;
967 /* is the kolabel requesting a substitution */
968 if (port_subst
!= IP_NULL
) {
970 * port is unlocked, its right consumed
973 assert(msgt_name
== MACH_MSG_TYPE_PORT_SEND
);
975 if (!IP_VALID(port
)) {
977 kr
= KERN_INVALID_CAPABILITY
;
981 object
= ip_to_object(port
);
982 is_write_lock(space
);
989 /* space is write-locked and active, object is locked and active */
991 if ((msgt_name
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
992 ipc_right_reverse(space
, object
, &name
, &entry
)) {
993 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
995 ipc_entry_claim(space
, &name
, &entry
);
997 assert(!ipc_right_inuse(entry
));
998 assert(entry
->ie_object
== IO_NULL
);
1000 entry
->ie_object
= object
;
1003 kr
= ipc_right_copyout(space
, name
, entry
,
1004 msgt_name
, flags
, context
, guard_flags
, object
);
1006 /* object is unlocked */
1007 is_write_unlock(space
);
1010 if (kr
== KERN_SUCCESS
) {
1012 } else if (IO_VALID(object
)) {
1013 ipc_object_destroy(object
, msgt_name
);
1020 * Routine: ipc_object_copyout_name
1022 * Copyout a capability, placing it into a space.
1023 * The specified name is used for the capability.
1024 * If successful, consumes a ref for the object.
1028 * KERN_SUCCESS Copied out object, consumed ref.
1029 * KERN_INVALID_TASK The space is dead.
1030 * KERN_INVALID_CAPABILITY The object is dead.
1031 * KERN_RESOURCE_SHORTAGE No memory available.
1032 * KERN_UREFS_OVERFLOW Urefs limit exceeded
1033 * and overflow wasn't specified.
1034 * KERN_RIGHT_EXISTS Space has rights under another name.
1035 * KERN_NAME_EXISTS Name is already used.
1039 ipc_object_copyout_name(
1041 ipc_object_t object
,
1042 mach_msg_type_name_t msgt_name
,
1043 mach_port_name_t name
)
1045 ipc_port_t port
= ip_object_to_port(object
);
1046 mach_port_name_t oname
;
1051 #if IMPORTANCE_INHERITANCE
1053 ipc_importance_task_t task_imp
= IIT_NULL
;
1054 #endif /* IMPORTANCE_INHERITANCE */
1056 assert(IO_VALID(object
));
1057 assert(io_otype(object
) == IOT_PORT
);
1059 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
1060 if (kr
!= KERN_SUCCESS
) {
1063 /* space is write-locked and active */
1068 * Don't actually copyout rights we aren't allowed to
1070 * In particular, kolabel-ed objects do not allow callers
1071 * to pick the name they end up with.
1073 if (!io_active(object
) || ip_is_kolabeled(port
)) {
1075 if (!ipc_right_inuse(entry
)) {
1076 ipc_entry_dealloc(space
, name
, entry
);
1078 is_write_unlock(space
);
1079 return KERN_INVALID_CAPABILITY
;
1082 /* space is write-locked and active, object is locked and active */
1084 if ((msgt_name
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
1085 ipc_right_reverse(space
, object
, &oname
, &oentry
)) {
1086 if (name
!= oname
) {
1088 if (!ipc_right_inuse(entry
)) {
1089 ipc_entry_dealloc(space
, name
, entry
);
1091 is_write_unlock(space
);
1092 return KERN_RIGHT_EXISTS
;
1095 assert(entry
== oentry
);
1096 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
1097 } else if (ipc_right_inuse(entry
)) {
1099 is_write_unlock(space
);
1100 return KERN_NAME_EXISTS
;
1102 assert(entry
->ie_object
== IO_NULL
);
1104 entry
->ie_object
= object
;
1107 #if IMPORTANCE_INHERITANCE
1109 * We are slamming a receive right into the space, without
1110 * first having been enqueued on a port destined there. So,
1111 * we have to arrange to boost the task appropriately if this
1112 * port has assertions (and the task wants them).
1114 if (msgt_name
== MACH_MSG_TYPE_PORT_RECEIVE
) {
1115 if (space
->is_task
!= TASK_NULL
) {
1116 task_imp
= space
->is_task
->task_imp_base
;
1117 if (ipc_importance_task_is_any_receiver_type(task_imp
)) {
1118 assertcnt
= port
->ip_impcount
;
1119 ipc_importance_task_reference(task_imp
);
1121 task_imp
= IIT_NULL
;
1125 /* take port out of limbo */
1126 assert(port
->ip_tempowner
!= 0);
1127 port
->ip_tempowner
= 0;
1130 #endif /* IMPORTANCE_INHERITANCE */
1132 kr
= ipc_right_copyout(space
, name
, entry
,
1133 msgt_name
, IPC_OBJECT_COPYOUT_FLAGS_NONE
, NULL
, NULL
, object
);
1135 /* object is unlocked */
1136 is_write_unlock(space
);
1138 #if IMPORTANCE_INHERITANCE
1140 * Add the assertions to the task that we captured before
1142 if (task_imp
!= IIT_NULL
) {
1143 ipc_importance_task_hold_internal_assertion(task_imp
, assertcnt
);
1144 ipc_importance_task_release(task_imp
);
1146 #endif /* IMPORTANCE_INHERITANCE */
1152 * Routine: ipc_object_copyout_dest
1154 * Translates/consumes the destination right of a message.
1155 * This is unlike normal copyout because the right is consumed
1156 * in a funny way instead of being given to the receiving space.
1157 * The receiver gets his name for the port, if he has receive
1158 * rights, otherwise MACH_PORT_NULL.
1160 * The object is locked and active. Nothing else locked.
1161 * The object is unlocked and loses a reference.
1165 ipc_object_copyout_dest(
1167 ipc_object_t object
,
1168 mach_msg_type_name_t msgt_name
,
1169 mach_port_name_t
*namep
)
1171 mach_port_name_t name
;
1173 assert(IO_VALID(object
));
1174 assert(io_active(object
));
1177 * If the space is the receiver/owner of the object,
1178 * then we quietly consume the right and return
1179 * the space's name for the object. Otherwise
1180 * we destroy the right and return MACH_PORT_NULL.
1183 switch (msgt_name
) {
1184 case MACH_MSG_TYPE_PORT_SEND
: {
1185 ipc_port_t port
= ip_object_to_port(object
);
1186 ipc_port_t nsrequest
= IP_NULL
;
1187 mach_port_mscount_t mscount
;
1189 if (port
->ip_receiver
== space
) {
1190 name
= port
->ip_receiver_name
;
1192 name
= MACH_PORT_NULL
;
1195 assert(port
->ip_srights
> 0);
1196 if (--port
->ip_srights
== 0 &&
1197 port
->ip_nsrequest
!= IP_NULL
) {
1198 nsrequest
= port
->ip_nsrequest
;
1199 port
->ip_nsrequest
= IP_NULL
;
1200 mscount
= port
->ip_mscount
;
1201 ipc_port_clear_sync_rcv_thread_boost_locked(port
);
1203 ipc_notify_no_senders(nsrequest
, mscount
);
1205 ipc_port_clear_sync_rcv_thread_boost_locked(port
);
1213 case MACH_MSG_TYPE_PORT_SEND_ONCE
: {
1214 ipc_port_t port
= ip_object_to_port(object
);
1216 assert(port
->ip_sorights
> 0);
1218 if (port
->ip_receiver
== space
) {
1219 /* quietly consume the send-once right */
1221 port
->ip_sorights
--;
1222 name
= port
->ip_receiver_name
;
1223 ipc_port_clear_sync_rcv_thread_boost_locked(port
);
1228 * A very bizarre case. The message
1229 * was received, but before this copyout
1230 * happened the space lost receive rights.
1231 * We can't quietly consume the soright
1232 * out from underneath some other task,
1233 * so generate a send-once notification.
1238 ipc_notify_send_once(port
);
1239 name
= MACH_PORT_NULL
;
1246 panic("ipc_object_copyout_dest: strange rights");
1247 name
= MACH_PORT_DEAD
;
1256 * Validate, then acquire a lock on an ipc object
1260 io_lock(ipc_object_t io
)
1262 ipc_object_validate(io
);
1263 lck_spin_lock_grp(&(io
)->io_lock_data
, &ipc_lck_grp
);
1267 * Routine: io_lock_try
1269 * Validate, then try to acquire a lock on an object,
1270 * fail if there is an existing busy lock
1274 io_lock_try(ipc_object_t io
)
1276 ipc_object_validate(io
);
1277 return lck_spin_try_lock_grp(&(io
)->io_lock_data
, &ipc_lck_grp
);
1281 * Check whether the object is a port if so, free it. But
1282 * keep track of that fact.
1287 ipc_object_t object
)
1289 if (otype
== IOT_PORT
) {
1290 ipc_port_finalize(ip_object_to_port(object
));
1292 io_lock_destroy(object
);
1293 zfree(ipc_object_zones
[otype
], object
);