xref: /xnu-8796.121.2/osfmk/kern/ipc_kobject.h (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
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 
147 	/* magic catch-all; should be the last entry */
148 	IKOT_UNKNOWN,
149 });
150 
151 #define IKOT_MAX_TYPE   (IKOT_UNKNOWN+1)        /* # of IKOT_ types	*/
152 
153 #ifdef __cplusplus
154 /* preserve legacy ABI for c++ */
155 typedef natural_t ipc_kobject_type_t;
156 #else
157 typedef ipc_kotype_t ipc_kobject_type_t;
158 #endif
159 
160 /* set the bitstring index for kobject */
161 extern kern_return_t ipc_kobject_set_kobjidx(
162 	int                         msgid,
163 	int                         index);
164 
165 #ifdef MACH_KERNEL_PRIVATE
166 
167 /*!
168  * @typedef ipc_kobject_ops_t
169  *
170  * @brief
171  * Describes the operations for a given kobject.
172  *
173  * @field iko_ko_type
174  * An @c IKOT_* value.
175  *
176  * @field iko_op_stable
177  * The kobject/port association is stable:
178  * - ipc_kobject_dealloc_port() cannot be called
179  *   while there are outstanding send rights,
180  * - ipc_kobject_enable() is never called.
181  * - ipc_kobject_disable() is never called.
182  *
183  * @field iko_op_permanent
184  * The port is never destroyed.
185  * This doesn't necessarily imply iko_op_stable.
186  *
187  * @field iko_op_no_senders
188  * A callback to run when a NO_SENDERS notification fires.
189  *
190  * Kobjects that destroy their port on no senders only are guaranteed
191  * to be called with an active port only.
192  *
193  * However kobject ports that can be destroyed concurrently need
194  * to be prepared for no senders to fail to acquire the kobject port.
195  *
196  * @field iko_op_destroy
197  * A callback to run as part of destroying the kobject port.
198  *
199  * When this callback is set, @c ipc_kobject_dealloc_port()
200  * will not implicitly call @c ipc_kobject_disable().
201  *
202  * The callback runs after the port has been marked inactive,
203  * hence @c ipc_kobject_get_raw() needs to be used to get to the port.
204  */
205 typedef const struct ipc_kobject_ops {
206 	ipc_kobject_type_t iko_op_type;
207 	unsigned long
208 	    iko_op_stable        : 1,
209 	    iko_op_permanent     : 1;
210 	const char        *iko_op_name;
211 	void (*iko_op_no_senders)(ipc_port_t port, mach_port_mscount_t mscount);
212 	void (*iko_op_destroy)(ipc_port_t port);
213 } *ipc_kobject_ops_t;
214 
215 #define IPC_KOBJECT_DEFINE(type, ...) \
216 	__startup_data \
217 	static struct ipc_kobject_ops ipc_kobject_ops_##type = { \
218 	    .iko_op_type = type, \
219 	    .iko_op_name = #type, \
220 	    __VA_ARGS__ \
221 	}; \
222 	STARTUP_ARG(MACH_IPC, STARTUP_RANK_FIRST, ipc_kobject_register_startup, \
223 	    &ipc_kobject_ops_##type)
224 
225 struct ipc_kobject_label {
226 	ipc_label_t   ikol_label;       /* [private] mandatory access label */
227 	ipc_port_t XNU_PTRAUTH_SIGNED_PTR("ipc_kobject_label.ikol_alt_port") ikol_alt_port;
228 };
229 
230 __options_decl(ipc_kobject_alloc_options_t, uint32_t, {
231 	/* Just make the naked port */
232 	IPC_KOBJECT_ALLOC_NONE      = 0x00000000,
233 	/* Make a send right */
234 	IPC_KOBJECT_ALLOC_MAKE_SEND = 0x00000001,
235 	/* Register for no-more-senders */
236 	IPC_KOBJECT_ALLOC_NSREQUEST = 0x00000002,
237 	/* Make it no grant port */
238 	IPC_KOBJECT_ALLOC_NO_GRANT  = 0x00000004,
239 	/* Mark the port as immovable send right */
240 	IPC_KOBJECT_ALLOC_IMMOVABLE_SEND = 0x00000008,
241 	/* Add a label structure to the port */
242 	IPC_KOBJECT_ALLOC_LABEL     = 0x00000010,
243 	/* Mark the port as pinned (non dealloc-able) in an ipc space */
244 	IPC_KOBJECT_ALLOC_PINNED    = 0x00000020,
245 });
246 
247 /* Allocates a kobject port, never fails */
248 extern ipc_port_t ipc_kobject_alloc_port(
249 	ipc_kobject_t               kobject,
250 	ipc_kobject_type_t          type,
251 	ipc_kobject_alloc_options_t options);
252 
253 /* Allocates a kobject port, never fails */
254 extern ipc_port_t ipc_kobject_alloc_labeled_port(
255 	ipc_kobject_t               kobject,
256 	ipc_kobject_type_t          type,
257 	ipc_label_t                 label,
258 	ipc_kobject_alloc_options_t options);
259 
260 extern ipc_port_t ipc_kobject_alloc_subst_once(
261 	ipc_port_t                  target);
262 
263 /* Makes a send right, lazily allocating a kobject port, arming for no-senders, never fails */
264 extern bool ipc_kobject_make_send_lazy_alloc_port(
265 	ipc_port_t                 *port_store,
266 	ipc_kobject_t               kobject,
267 	ipc_kobject_type_t          type,
268 	ipc_kobject_alloc_options_t alloc_opts);
269 
270 /* Makes a send right, lazily allocating a kobject port, arming for no-senders, never fails */
271 extern boolean_t ipc_kobject_make_send_lazy_alloc_labeled_port(
272 	ipc_port_t                 *port_store,
273 	ipc_kobject_t               kobject,
274 	ipc_kobject_type_t          type,
275 	ipc_label_t                 label) __result_use_check;
276 
277 extern kern_return_t ipc_kobject_nsrequest(
278 	ipc_port_t                  port,
279 	mach_port_mscount_t         sync,
280 	mach_port_mscount_t        *mscount) __result_use_check;
281 
282 /*!
283  * @function ipc_kobject_copy_send()
284  *
285  * @brief
286  * Copies a naked send right for the specified kobject port.
287  *
288  * @decription
289  * This function will validate that the specified port is pointing
290  * to the expected kobject pointer and type (by calling ipc_kobject_require()).
291  *
292  * @param port          The target port.
293  * @param kobject       The kobject pointer this port should be associated to.
294  * @param kotype        The kobject type this port should have.
295  *
296  * @returns
297  * - IP_DEAD            if @c port was dead.
298  * - @c port            if @c port was valid, in which case
299  *                      a naked send right was made.
300  */
301 extern ipc_port_t ipc_kobject_copy_send(
302 	ipc_port_t                  port,
303 	ipc_kobject_t               kobject,
304 	ipc_kobject_type_t          kotype) __result_use_check;
305 
306 /*!
307  * @function ipc_kobject_make_send()
308  *
309  * @brief
310  * Makes a naked send right for the specified kobject port.
311  *
312  * @decription
313  * @see ipc_port_make_send_any_locked() for a general warning about
314  * making send rights.
315  *
316  * This function will validate that the specified port is pointing
317  * to the expected kobject pointer and type (by calling ipc_kobject_require()).
318  *
319  * @param port          The target port.
320  * @param kobject       The kobject pointer this port should be associated to.
321  * @param kotype        The kobject type this port should have.
322  *
323  * @returns
324  * - IP_DEAD            if @c port was dead.
325  * - @c port            if @c port was valid, in which case
326  *                      a naked send right was made.
327  */
328 extern ipc_port_t ipc_kobject_make_send(
329 	ipc_port_t                  port,
330 	ipc_kobject_t               kobject,
331 	ipc_kobject_type_t          kotype) __result_use_check;
332 
333 /*!
334  * @function ipc_kobject_make_send_nsrequest()
335  *
336  * @brief
337  * Makes a naked send right for the specified kobject port,
338  * and arms no-more-senders if it wasn't already.
339  *
340  * @decription
341  * @see ipc_port_make_send_any_locked() for a general warning about
342  * making send rights.
343  *
344  * This function will validate that the specified port is pointing
345  * to the expected kobject pointer and type (by calling ipc_kobject_require()).
346  *
347  * @param port          The target port.
348  * @param kobject       The kobject pointer this port should be associated to.
349  * @param kotype        The kobject type this port should have.
350  *
351  * @returns
352  * - KERN_SUCCESS:           the notification was armed
353  * - KERN_ALREADY_WAITING:   the notification was already armed
354  * - KERN_INVALID_RIGHT:     the port is dead
355  */
356 extern kern_return_t ipc_kobject_make_send_nsrequest(
357 	ipc_port_t                  port,
358 	ipc_kobject_t               kobject,
359 	ipc_kobject_type_t          kotype) __result_use_check;
360 
361 extern ipc_kobject_t ipc_kobject_dealloc_port_and_unlock(
362 	ipc_port_t                  port,
363 	mach_port_mscount_t         mscount,
364 	ipc_kobject_type_t          type);
365 
366 extern ipc_kobject_t ipc_kobject_dealloc_port(
367 	ipc_port_t                  port,
368 	mach_port_mscount_t         mscount,
369 	ipc_kobject_type_t          type);
370 
371 extern void         ipc_kobject_enable(
372 	ipc_port_t                  port,
373 	ipc_kobject_t               kobject,
374 	ipc_kobject_type_t          type);
375 
376 /*!
377  * @function ipc_kobject_require()
378  *
379  * @brief
380  * Asserts that a given port is of the specified type
381  * with the expected kobject pointer.
382  *
383  * @decription
384  * Port type confusion can lead to catastrophic system compromise,
385  * this function can be used in choke points to ensure ports are
386  * what they're expected to be before their use.
387  *
388  * @note It is allowed for the kobject pointer to be NULL,
389  *       as in some cases ipc_kobject_disable() can be raced with this check.
390  *
391  * @param port          The target port.
392  * @param kobject       The kobject pointer this port should be associated to.
393  * @param kotype        The kobject type this port should have.
394  */
395 extern void         ipc_kobject_require(
396 	ipc_port_t                  port,
397 	ipc_kobject_t               kobject,
398 	ipc_kobject_type_t          kotype);
399 
400 extern ipc_kobject_t ipc_kobject_get_raw(
401 	ipc_port_t                  port,
402 	ipc_kobject_type_t          type);
403 
404 extern ipc_kobject_t ipc_kobject_get_locked(
405 	ipc_port_t                  port,
406 	ipc_kobject_type_t          type);
407 
408 extern ipc_kobject_t ipc_kobject_get_stable(
409 	ipc_port_t                  port,
410 	ipc_kobject_type_t          type);
411 
412 extern ipc_kobject_t ipc_kobject_disable_locked(
413 	ipc_port_t                  port,
414 	ipc_kobject_type_t          type);
415 
416 extern ipc_kobject_t ipc_kobject_disable(
417 	ipc_port_t                  port,
418 	ipc_kobject_type_t          type);
419 
420 extern void         ipc_kobject_upgrade_mktimer_locked(
421 	ipc_port_t                  port,
422 	ipc_kobject_t               kobject);
423 
424 /* in mk_timer.c */
425 extern void         ipc_kobject_mktimer_require_locked(
426 	ipc_port_t                  port);
427 
428 /* Check if a kobject can be copied out to a given space */
429 extern bool     ipc_kobject_label_check(
430 	ipc_space_t                 space,
431 	ipc_port_t                  port,
432 	mach_msg_type_name_t        msgt_name,
433 	ipc_object_copyout_flags_t *flags,
434 	ipc_port_t                 *subst_portp) __result_use_check;
435 
436 __result_use_check
437 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)438 ip_label_check(
439 	ipc_space_t                 space,
440 	ipc_port_t                  port,
441 	mach_msg_type_name_t        msgt_name,
442 	ipc_object_copyout_flags_t *flags,
443 	ipc_port_t                 *subst_portp)
444 {
445 	if (!ip_is_kolabeled(port)) {
446 		*subst_portp = IP_NULL;
447 		return true;
448 	}
449 	return ipc_kobject_label_check(space, port, msgt_name, flags, subst_portp);
450 }
451 
452 /* implementation details */
453 
454 __startup_func
455 extern void ipc_kobject_register_startup(
456 	ipc_kobject_ops_t           ops);
457 
458 /* initialization of kobject subsystem */
459 extern void ipc_kobject_init(void);
460 
461 /* Dispatch a kernel server function */
462 extern ipc_kmsg_t ipc_kobject_server(
463 	ipc_port_t                  receiver,
464 	ipc_kmsg_t                  request,
465 	mach_msg_option_t           option);
466 
467 /* Release any kernel object resources associated with a port */
468 extern void ipc_kobject_destroy(
469 	ipc_port_t                  port);
470 
471 #define null_conversion(port)   (port)
472 
473 extern void ipc_kobject_notify_no_senders(
474 	ipc_port_t                  port,
475 	mach_port_mscount_t         mscount);
476 
477 extern void ipc_kobject_notify_send_once_and_unlock(
478 	ipc_port_t                  port);
479 
480 extern kern_return_t uext_server(
481 	ipc_port_t                  receiver,
482 	ipc_kmsg_t                  request,
483 	ipc_kmsg_t                  *reply);
484 
485 #endif /* MACH_KERNEL_PRIVATE */
486 
487 #pragma GCC visibility pop
488 __END_DECLS
489 
490 #endif /* _KERN_IPC_KOBJECT_H_ */
491