1 /*
2 * Copyright (c) 2000-2006 Apple Computer, 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 [email protected]
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 SPARTA, Inc. in 2005 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 */
62 /*
63 */
64 /*
65 * File: kern/ipc_kobject.h
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Declarations for letting a port represent a kernel object.
70 */
71
72 #ifndef _KERN_IPC_KOBJECT_H_
73 #define _KERN_IPC_KOBJECT_H_
74
75 #ifdef MACH_KERNEL_PRIVATE
76 #include <ipc/ipc_kmsg.h>
77 #include <ipc/ipc_port.h>
78 #include <kern/startup.h>
79 #endif /* MACH_KERNEL_PRIVATE */
80 #include <mach/machine/vm_types.h>
81 #include <mach/mach_types.h>
82
83 __BEGIN_DECLS
84 #pragma GCC visibility push(hidden)
85
86 __enum_decl(ipc_kotype_t, natural_t, {
87 IKOT_NONE = 0,
88 IKOT_THREAD_CONTROL = 1,
89 IKOT_TASK_CONTROL = 2,
90 IKOT_HOST = 3,
91 IKOT_HOST_PRIV = 4,
92 IKOT_PROCESSOR = 5,
93 IKOT_PSET = 6,
94 IKOT_PSET_NAME = 7,
95 IKOT_TIMER = 8,
96 IKOT_PORT_SUBST_ONCE = 9,
97 // IKOT_MIG = 10,
98 IKOT_MEMORY_OBJECT = 11,
99 // IKOT_XMM_PAGER = 12,
100 // IKOT_XMM_KERNEL = 13,
101 // IKOT_XMM_REPLY = 14,
102 IKOT_UND_REPLY = 15,
103 // IKOT_HOST_NOTIFY = 16,
104 // IKOT_HOST_SECURITY = 17,
105 // IKOT_LEDGER = 18,
106 IKOT_MAIN_DEVICE = 19,
107 IKOT_TASK_NAME = 20,
108 // IKOT_SUBSYSTEM = 21,
109 // IKOT_IO_DONE_QUEUE = 22,
110 IKOT_SEMAPHORE = 23,
111 // IKOT_LOCK_SET = 24,
112 IKOT_CLOCK = 25,
113 // IKOT_CLOCK_CTRL = 26,
114 IKOT_IOKIT_IDENT = 27,
115 IKOT_NAMED_ENTRY = 28,
116 IKOT_IOKIT_CONNECT = 29,
117 IKOT_IOKIT_OBJECT = 30,
118 // IKOT_UPL = 31,
119 // IKOT_MEM_OBJ_CONTROL = 32,
120 #if CONFIG_AUDIT
121 IKOT_AU_SESSIONPORT = 33,
122 #endif
123 IKOT_FILEPORT = 34,
124 // IKOT_LABELH = 35,
125 IKOT_TASK_RESUME = 36,
126 IKOT_VOUCHER = 37,
127 // IKOT_VOUCHER_ATTR_CONTROL = 38,
128 IKOT_WORK_INTERVAL = 39,
129 IKOT_UX_HANDLER = 40,
130 IKOT_UEXT_OBJECT = 41,
131 IKOT_ARCADE_REG = 42,
132 IKOT_EVENTLINK = 43,
133 IKOT_TASK_INSPECT = 44,
134 IKOT_TASK_READ = 45,
135 IKOT_THREAD_INSPECT = 46,
136 IKOT_THREAD_READ = 47,
137 // IKOT_SUID_CRED = 48,
138 #if HYPERVISOR
139 IKOT_HYPERVISOR = 49,
140 #endif
141 IKOT_TASK_ID_TOKEN = 50,
142 #if CONFIG_PROC_RESOURCE_LIMITS
143 IKOT_TASK_FATAL = 51,
144 #endif
145 IKOT_KCDATA = 52,
146 /* magic catch-all; should be the last entry */
147 IKOT_UNKNOWN,
148 });
149
150 #define IKOT_MAX_TYPE (IKOT_UNKNOWN+1) /* # of IKOT_ types */
151
152 #ifdef __cplusplus
153 /* preserve legacy ABI for c++ */
154 typedef natural_t ipc_kobject_type_t;
155 #else
156 typedef ipc_kotype_t ipc_kobject_type_t;
157 #endif
158
159 /* set the bitstring index for kobject */
160 extern kern_return_t ipc_kobject_set_kobjidx(
161 int msgid,
162 int index);
163
164 #ifdef MACH_KERNEL_PRIVATE
165
166 /*!
167 * @typedef ipc_kobject_ops_t
168 *
169 * @brief
170 * Describes the operations for a given kobject.
171 *
172 * @field iko_ko_type
173 * An @c IKOT_* value.
174 *
175 * @field iko_op_stable
176 * The kobject/port association is stable:
177 * - ipc_kobject_dealloc_port() cannot be called
178 * while there are outstanding send rights,
179 * - ipc_kobject_enable() is never called.
180 * - ipc_kobject_disable() is never called.
181 *
182 * @field iko_op_permanent
183 * The port is never destroyed.
184 * This doesn't necessarily imply iko_op_stable.
185 *
186 * @field iko_op_no_senders
187 * A callback to run when a NO_SENDERS notification fires.
188 *
189 * Kobjects that destroy their port on no senders only are guaranteed
190 * to be called with an active port only.
191 *
192 * However kobject ports that can be destroyed concurrently need
193 * to be prepared for no senders to fail to acquire the kobject port.
194 *
195 * @field iko_op_destroy
196 * A callback to run as part of destroying the kobject port.
197 *
198 * When this callback is set, @c ipc_kobject_dealloc_port()
199 * will not implicitly call @c ipc_kobject_disable().
200 *
201 * The callback runs after the port has been marked inactive,
202 * hence @c ipc_kobject_get_raw() needs to be used to get to the port.
203 */
204 typedef const struct ipc_kobject_ops {
205 ipc_kobject_type_t iko_op_type;
206 unsigned long
207 iko_op_stable : 1,
208 iko_op_permanent : 1;
209 const char *iko_op_name;
210 void (*iko_op_no_senders)(ipc_port_t port, mach_port_mscount_t mscount);
211 void (*iko_op_destroy)(ipc_port_t port);
212 } *ipc_kobject_ops_t;
213
214 #define IPC_KOBJECT_DEFINE(type, ...) \
215 __startup_data \
216 static struct ipc_kobject_ops ipc_kobject_ops_##type = { \
217 .iko_op_type = type, \
218 .iko_op_name = #type, \
219 __VA_ARGS__ \
220 }; \
221 STARTUP_ARG(MACH_IPC, STARTUP_RANK_FIRST, ipc_kobject_register_startup, \
222 &ipc_kobject_ops_##type)
223
224 struct ipc_kobject_label {
225 ipc_label_t ikol_label; /* [private] mandatory access label */
226 ipc_port_t XNU_PTRAUTH_SIGNED_PTR("ipc_kobject_label.ikol_alt_port") ikol_alt_port;
227 };
228
229 __options_decl(ipc_kobject_alloc_options_t, uint32_t, {
230 /* Just make the naked port */
231 IPC_KOBJECT_ALLOC_NONE = 0x00000000,
232 /* Make a send right */
233 IPC_KOBJECT_ALLOC_MAKE_SEND = 0x00000001,
234 /* Register for no-more-senders */
235 IPC_KOBJECT_ALLOC_NSREQUEST = 0x00000002,
236 /* Make it no grant port */
237 IPC_KOBJECT_ALLOC_NO_GRANT = 0x00000004,
238 /* Mark the port as immovable send right */
239 IPC_KOBJECT_ALLOC_IMMOVABLE_SEND = 0x00000008,
240 /* Add a label structure to the port */
241 IPC_KOBJECT_ALLOC_LABEL = 0x00000010,
242 /* Mark the port as pinned (non dealloc-able) in an ipc space */
243 IPC_KOBJECT_ALLOC_PINNED = 0x00000020,
244 });
245
246 /* Allocates a kobject port, never fails */
247 extern ipc_port_t ipc_kobject_alloc_port(
248 ipc_kobject_t kobject,
249 ipc_kobject_type_t type,
250 ipc_kobject_alloc_options_t options);
251
252 /* Allocates a kobject port, never fails */
253 extern ipc_port_t ipc_kobject_alloc_labeled_port(
254 ipc_kobject_t kobject,
255 ipc_kobject_type_t type,
256 ipc_label_t label,
257 ipc_kobject_alloc_options_t options);
258
259 extern ipc_port_t ipc_kobject_alloc_subst_once(
260 ipc_port_t target);
261
262 /* Makes a send right, lazily allocating a kobject port, arming for no-senders, never fails */
263 extern bool ipc_kobject_make_send_lazy_alloc_port(
264 ipc_port_t *port_store,
265 ipc_kobject_t kobject,
266 ipc_kobject_type_t type,
267 ipc_kobject_alloc_options_t alloc_opts);
268
269 /* Makes a send right, lazily allocating a kobject port, arming for no-senders, never fails */
270 extern boolean_t ipc_kobject_make_send_lazy_alloc_labeled_port(
271 ipc_port_t *port_store,
272 ipc_kobject_t kobject,
273 ipc_kobject_type_t type,
274 ipc_label_t label) __result_use_check;
275
276 extern kern_return_t ipc_kobject_nsrequest(
277 ipc_port_t port,
278 mach_port_mscount_t sync,
279 mach_port_mscount_t *mscount) __result_use_check;
280
281 /*!
282 * @function ipc_kobject_copy_send()
283 *
284 * @brief
285 * Copies a naked send right for the specified kobject port.
286 *
287 * @decription
288 * This function will validate that the specified port is pointing
289 * to the expected kobject pointer and type (by calling ipc_kobject_require()).
290 *
291 * @param port The target port.
292 * @param kobject The kobject pointer this port should be associated to.
293 * @param kotype The kobject type this port should have.
294 *
295 * @returns
296 * - IP_DEAD if @c port was dead.
297 * - @c port if @c port was valid, in which case
298 * a naked send right was made.
299 */
300 extern ipc_port_t ipc_kobject_copy_send(
301 ipc_port_t port,
302 ipc_kobject_t kobject,
303 ipc_kobject_type_t kotype) __result_use_check;
304
305 /*!
306 * @function ipc_kobject_make_send()
307 *
308 * @brief
309 * Makes a naked send right for the specified kobject port.
310 *
311 * @decription
312 * @see ipc_port_make_send_any_locked() for a general warning about
313 * making send rights.
314 *
315 * This function will validate that the specified port is pointing
316 * to the expected kobject pointer and type (by calling ipc_kobject_require()).
317 *
318 * @param port The target port.
319 * @param kobject The kobject pointer this port should be associated to.
320 * @param kotype The kobject type this port should have.
321 *
322 * @returns
323 * - IP_DEAD if @c port was dead.
324 * - @c port if @c port was valid, in which case
325 * a naked send right was made.
326 */
327 extern ipc_port_t ipc_kobject_make_send(
328 ipc_port_t port,
329 ipc_kobject_t kobject,
330 ipc_kobject_type_t kotype) __result_use_check;
331
332 /*!
333 * @function ipc_kobject_make_send_nsrequest()
334 *
335 * @brief
336 * Makes a naked send right for the specified kobject port,
337 * and arms no-more-senders if it wasn't already.
338 *
339 * @decription
340 * @see ipc_port_make_send_any_locked() for a general warning about
341 * making send rights.
342 *
343 * This function will validate that the specified port is pointing
344 * to the expected kobject pointer and type (by calling ipc_kobject_require()).
345 *
346 * @param port The target port.
347 * @param kobject The kobject pointer this port should be associated to.
348 * @param kotype The kobject type this port should have.
349 *
350 * @returns
351 * - KERN_SUCCESS: the notification was armed
352 * - KERN_ALREADY_WAITING: the notification was already armed
353 * - KERN_INVALID_RIGHT: the port is dead
354 */
355 extern kern_return_t ipc_kobject_make_send_nsrequest(
356 ipc_port_t port,
357 ipc_kobject_t kobject,
358 ipc_kobject_type_t kotype) __result_use_check;
359
360 extern ipc_kobject_t ipc_kobject_dealloc_port_and_unlock(
361 ipc_port_t port,
362 mach_port_mscount_t mscount,
363 ipc_kobject_type_t type);
364
365 extern ipc_kobject_t ipc_kobject_dealloc_port(
366 ipc_port_t port,
367 mach_port_mscount_t mscount,
368 ipc_kobject_type_t type);
369
370 extern void ipc_kobject_enable(
371 ipc_port_t port,
372 ipc_kobject_t kobject,
373 ipc_kobject_type_t type);
374
375 /*!
376 * @function ipc_kobject_require()
377 *
378 * @brief
379 * Asserts that a given port is of the specified type
380 * with the expected kobject pointer.
381 *
382 * @decription
383 * Port type confusion can lead to catastrophic system compromise,
384 * this function can be used in choke points to ensure ports are
385 * what they're expected to be before their use.
386 *
387 * @note It is allowed for the kobject pointer to be NULL,
388 * as in some cases ipc_kobject_disable() can be raced with this check.
389 *
390 * @param port The target port.
391 * @param kobject The kobject pointer this port should be associated to.
392 * @param kotype The kobject type this port should have.
393 */
394 extern void ipc_kobject_require(
395 ipc_port_t port,
396 ipc_kobject_t kobject,
397 ipc_kobject_type_t kotype);
398
399 extern ipc_kobject_t ipc_kobject_get_raw(
400 ipc_port_t port,
401 ipc_kobject_type_t type);
402
403 extern ipc_kobject_t ipc_kobject_get_locked(
404 ipc_port_t port,
405 ipc_kobject_type_t type);
406
407 extern ipc_kobject_t ipc_kobject_get_stable(
408 ipc_port_t port,
409 ipc_kobject_type_t type);
410
411 extern ipc_kobject_t ipc_kobject_disable_locked(
412 ipc_port_t port,
413 ipc_kobject_type_t type);
414
415 extern ipc_kobject_t ipc_kobject_disable(
416 ipc_port_t port,
417 ipc_kobject_type_t type);
418
419 extern void ipc_kobject_upgrade_mktimer_locked(
420 ipc_port_t port,
421 ipc_kobject_t kobject);
422
423 /* in mk_timer.c */
424 extern void ipc_kobject_mktimer_require_locked(
425 ipc_port_t port);
426
427 /* Check if a kobject can be copied out to a given space */
428 extern bool ipc_kobject_label_check(
429 ipc_space_t space,
430 ipc_port_t port,
431 mach_msg_type_name_t msgt_name,
432 ipc_object_copyout_flags_t *flags,
433 ipc_port_t *subst_portp) __result_use_check;
434
435 __result_use_check
436 static inline bool
ip_label_check(ipc_space_t space,ipc_port_t port,mach_msg_type_name_t msgt_name,ipc_object_copyout_flags_t * flags,ipc_port_t * subst_portp)437 ip_label_check(
438 ipc_space_t space,
439 ipc_port_t port,
440 mach_msg_type_name_t msgt_name,
441 ipc_object_copyout_flags_t *flags,
442 ipc_port_t *subst_portp)
443 {
444 if (!ip_is_kolabeled(port)) {
445 *subst_portp = IP_NULL;
446 return true;
447 }
448 return ipc_kobject_label_check(space, port, msgt_name, flags, subst_portp);
449 }
450
451 /* implementation details */
452
453 __startup_func
454 extern void ipc_kobject_register_startup(
455 ipc_kobject_ops_t ops);
456
457 /* initialization of kobject subsystem */
458 extern void ipc_kobject_init(void);
459
460 /* Dispatch a kernel server function */
461 extern ipc_kmsg_t ipc_kobject_server(
462 ipc_port_t receiver,
463 ipc_kmsg_t request,
464 mach_msg_option_t option);
465
466 /* Release any kernel object resources associated with a port */
467 extern void ipc_kobject_destroy(
468 ipc_port_t port);
469
470 #define null_conversion(port) (port)
471
472 extern void ipc_kobject_notify_no_senders(
473 ipc_port_t port,
474 mach_port_mscount_t mscount);
475
476 extern void ipc_kobject_notify_send_once_and_unlock(
477 ipc_port_t port);
478
479 extern kern_return_t uext_server(
480 ipc_port_t receiver,
481 ipc_kmsg_t request,
482 ipc_kmsg_t *reply);
483
484 #endif /* MACH_KERNEL_PRIVATE */
485
486 #pragma GCC visibility pop
487 __END_DECLS
488
489 #endif /* _KERN_IPC_KOBJECT_H_ */
490