2 * Copyright (c) 2000-2007 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,
65 * File: ipc/ipc_object.h
69 * Definitions for IPC objects, for which tasks have capabilities.
72 #ifndef _IPC_IPC_OBJECT_H_
73 #define _IPC_IPC_OBJECT_H_
75 #include <mach/kern_return.h>
76 #include <mach/message.h>
77 #include <kern/locks.h>
78 #include <kern/macro_help.h>
79 #include <kern/assert.h>
80 #include <kern/zalloc.h>
81 #include <ipc/ipc_types.h>
82 #include <libkern/OSAtomic.h>
84 typedef natural_t ipc_object_refs_t
; /* for ipc/ipc_object.h */
85 typedef natural_t ipc_object_bits_t
;
86 typedef natural_t ipc_object_type_t
;
89 * The ipc_object is used to both tag and reference count these two data
90 * structures, and (Noto Bene!) pointers to either of these or the
91 * ipc_object at the head of these are freely cast back and forth; hence
92 * the ipc_object MUST BE FIRST in the ipc_common_data.
94 * If the RPC implementation enabled user-mode code to use kernel-level
95 * data structures (as ours used to), this peculiar structuring would
96 * avoid having anything in user code depend on the kernel configuration
97 * (with which lock size varies).
100 ipc_object_bits_t io_bits
;
101 ipc_object_refs_t io_references
;
102 lck_spin_t io_lock_data
;
103 } __attribute__((aligned(8)));
106 * If another object type needs to participate in io_kotype()-based
107 * dispatching, it must include a stub structure as the first
110 struct ipc_object_header
{
111 ipc_object_bits_t io_bits
;
113 natural_t io_padding
; /* pad to natural boundary */
118 * Legacy defines. Should use IPC_OBJECT_NULL, etc...
120 #define IO_NULL ((ipc_object_t) 0)
121 #define IO_DEAD ((ipc_object_t) ~0UL)
122 #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD))
125 * IPC steals the high-order bits from the kotype to use
126 * for its own purposes. This allows IPC to record facts
127 * about ports that aren't otherwise obvious from the
128 * existing port fields. In particular, IPC can optionally
129 * mark a port for no more senders detection. Any change
130 * to IO_BITS_PORT_INFO must be coordinated with bitfield
131 * definitions in ipc_port.h.
133 #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */
134 #define IO_BITS_KOTYPE 0x000003ff /* used by the object */
135 #define IO_BITS_KOBJECT 0x00000800 /* port belongs to a kobject */
136 #define IO_BITS_KOLABEL 0x00000400 /* The kobject has a label */
137 #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */
138 #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */
140 #define io_active(io) (((io)->io_bits & IO_BITS_ACTIVE) != 0)
142 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
143 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
144 #define io_is_kobject(io) (((io)->io_bits & IO_BITS_KOBJECT) != IKOT_NONE)
145 #define io_is_kolabeled(io) (((io)->io_bits & IO_BITS_KOLABEL) != 0)
146 #define io_makebits(active, otype, kotype) \
147 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
150 * Object types: ports, port sets, kernel-loaded ports
153 #define IOT_PORT_SET 1
154 #define IOT_NUMBER 2 /* number of types used */
156 extern zone_t ipc_object_zones
[IOT_NUMBER
];
157 extern lck_grp_t ipc_lck_grp
;
159 #define io_alloc(otype) \
160 ((ipc_object_t) zalloc(ipc_object_zones[(otype)]))
164 ipc_object_t object
);
167 * Here we depend on the ipc_object being first within the kernel struct
168 * (ipc_port and ipc_pset).
170 #define io_lock_init(io) \
171 lck_spin_init(&(io)->io_lock_data, &ipc_lck_grp, &ipc_lck_attr)
172 #define io_lock_destroy(io) \
173 lck_spin_destroy(&(io)->io_lock_data, &ipc_lck_grp)
174 #define io_lock_held(io) \
175 LCK_SPIN_ASSERT(&(io)->io_lock_data, LCK_ASSERT_OWNED)
176 #define io_lock_held_kdp(io) \
177 kdp_lck_spin_is_acquired(&(io)->io_lock_data)
178 #define io_unlock(io) \
179 lck_spin_unlock(&(io)->io_lock_data)
183 extern boolean_t
io_lock_try(
186 #define _VOLATILE_ volatile
188 /* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
189 * If it is larger than max int, it has been corrupted or leaked,
190 * probably by being modified into an address (this is architecture
191 * dependent, but it's safe to assume there cannot really be max int
192 * references unless some code is leaking the io_reference without leaking
193 * object). Saturate the io_reference on release kernel if it reaches
194 * max int to avoid use after free.
196 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
197 * structs, because the io_references field is the first word of the struct,
198 * and zfree modifies that to point to the next free zone element.
200 #define IO_MAX_REFERENCES \
201 (unsigned)(~0U ^ (1U << (sizeof(int)*BYTE_SIZE - 1)))
204 io_reference(ipc_object_t io
)
206 ipc_object_refs_t new_io_references
;
207 ipc_object_refs_t old_io_references
;
209 if ((io
)->io_references
== 0 ||
210 (io
)->io_references
>= IO_MAX_REFERENCES
) {
211 panic("%s: reference count %u is invalid\n", __func__
, (io
)->io_references
);
215 old_io_references
= (io
)->io_references
;
216 new_io_references
= old_io_references
+ 1;
217 if (old_io_references
== IO_MAX_REFERENCES
) {
220 } while (OSCompareAndSwap(old_io_references
, new_io_references
,
221 &((io
)->io_references
)) == FALSE
);
226 io_release(ipc_object_t io
)
228 ipc_object_refs_t new_io_references
;
229 ipc_object_refs_t old_io_references
;
231 if ((io
)->io_references
== 0 ||
232 (io
)->io_references
>= IO_MAX_REFERENCES
) {
233 panic("%s: reference count %u is invalid\n", __func__
, (io
)->io_references
);
237 old_io_references
= (io
)->io_references
;
238 new_io_references
= old_io_references
- 1;
239 if (old_io_references
== IO_MAX_REFERENCES
) {
242 } while (OSCompareAndSwap(old_io_references
, new_io_references
,
243 &((io
)->io_references
)) == FALSE
);
245 /* If we just removed the last reference count */
246 if (1 == old_io_references
) {
247 /* Free the object */
248 io_free(io_otype((io
)), (io
));
253 * Retrieve a label for use in a kernel call that takes a security
254 * label as a parameter. If necessary, io_getlabel acquires internal
255 * (not io_lock) locks, and io_unlocklabel releases them.
259 extern struct label
*io_getlabel(ipc_object_t obj
);
260 #define io_unlocklabel(obj)
263 * Exported interfaces
266 /* Take a reference to an object */
267 extern void ipc_object_reference(
268 ipc_object_t object
);
270 /* Release a reference to an object */
271 extern void ipc_object_release(
272 ipc_object_t object
);
274 /* Look up an object in a space */
275 extern kern_return_t
ipc_object_translate(
277 mach_port_name_t name
,
278 mach_port_right_t right
,
279 ipc_object_t
*objectp
);
281 /* Look up two objects in a space, locking them in the order described */
282 extern kern_return_t
ipc_object_translate_two(
284 mach_port_name_t name1
,
285 mach_port_right_t right1
,
286 ipc_object_t
*objectp1
,
287 mach_port_name_t name2
,
288 mach_port_right_t right2
,
289 ipc_object_t
*objectp2
);
291 /* Validate an object as belonging to the correct zone */
292 extern void ipc_object_validate(
293 ipc_object_t object
);
295 /* Allocate a dead-name entry */
297 ipc_object_alloc_dead(
299 mach_port_name_t
*namep
);
301 /* Allocate a dead-name entry, with a specific name */
302 extern kern_return_t
ipc_object_alloc_dead_name(
304 mach_port_name_t name
);
306 /* Allocate an object */
307 extern kern_return_t
ipc_object_alloc(
309 ipc_object_type_t otype
,
310 mach_port_type_t type
,
311 mach_port_urefs_t urefs
,
312 mach_port_name_t
*namep
,
313 ipc_object_t
*objectp
);
315 /* Allocate an object, with a specific name */
316 extern kern_return_t
ipc_object_alloc_name(
318 ipc_object_type_t otype
,
319 mach_port_type_t type
,
320 mach_port_urefs_t urefs
,
321 mach_port_name_t name
,
322 ipc_object_t
*objectp
);
324 /* Convert a send type name to a received type name */
325 extern mach_msg_type_name_t
ipc_object_copyin_type(
326 mach_msg_type_name_t msgt_name
);
328 /* Copyin a capability from a space */
329 extern kern_return_t
ipc_object_copyin(
331 mach_port_name_t name
,
332 mach_msg_type_name_t msgt_name
,
333 ipc_object_t
*objectp
,
334 mach_port_context_t context
,
335 mach_msg_guard_flags_t
*guard_flags
,
336 uint16_t kmsg_flags
);
338 /* Copyin a naked capability from the kernel */
339 extern void ipc_object_copyin_from_kernel(
341 mach_msg_type_name_t msgt_name
);
343 /* Destroy a naked capability */
344 extern void ipc_object_destroy(
346 mach_msg_type_name_t msgt_name
);
348 /* Destroy a naked destination capability */
349 extern void ipc_object_destroy_dest(
351 mach_msg_type_name_t msgt_name
);
353 /* Insert a send right into an object already in the current space */
354 extern kern_return_t
ipc_object_insert_send_right(
356 mach_port_name_t name
,
357 mach_msg_type_name_t msgt_name
);
359 /* Copyout a capability, placing it into a space */
360 extern kern_return_t
ipc_object_copyout(
363 mach_msg_type_name_t msgt_name
,
364 mach_port_context_t
*context
,
365 mach_msg_guard_flags_t
*guard_flags
,
366 mach_port_name_t
*namep
);
368 /* Copyout a capability with a name, placing it into a space */
369 extern kern_return_t
ipc_object_copyout_name(
372 mach_msg_type_name_t msgt_name
,
373 mach_port_name_t name
);
375 /* Translate/consume the destination right of a message */
376 extern void ipc_object_copyout_dest(
379 mach_msg_type_name_t msgt_name
,
380 mach_port_name_t
*namep
);
382 #endif /* _IPC_IPC_OBJECT_H_ */