xref: /xnu-12377.61.12/osfmk/ipc/ipc_kmsg.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2000-2004 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 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 SPARTA, Inc.
62  */
63 /*
64  */
65 /*
66  *	File:	ipc/ipc_kmsg.h
67  *	Author:	Rich Draves
68  *	Date:	1989
69  *
70  *	Definitions for kernel messages.
71  */
72 
73 #ifndef _IPC_IPC_KMSG_H_
74 #define _IPC_IPC_KMSG_H_
75 
76 #include <mach/vm_types.h>
77 #include <mach/message.h>
78 #include <kern/kern_types.h>
79 #include <kern/assert.h>
80 #include <kern/macro_help.h>
81 #include <kern/kalloc.h>
82 #include <kern/circle_queue.h>
83 #include <ipc/ipc_types.h>
84 #include <ipc/ipc_object.h>
85 #include <sys/kdebug.h>
86 
87 /*
88  *	This structure is only the header for a kmsg buffer;
89  *	the actual buffer is normally larger. The rest of the buffer
90  *	holds the body of the message.
91  *
92  *	In a kmsg, the port fields hold pointers to ports instead
93  *	of port names. These pointers hold references.
94  *
95  *	The ikm_header.msgh_remote_port field is the destination
96  *	of the message.
97  */
98 
99 /*
100  * A kmsg can be in one of the following four layouts
101  *
102  * @see <doc/mach_ipc/kmsg.md> for a visual representation.
103  */
104 __enum_decl(ipc_kmsg_type_t, uint8_t, {
105 	/*
106 	 * IKM_TYPE_ALL_INLINED: The entire message (and aux) is allocated inline.
107 	 * mach_msg_header_t is immediately after the kmsg header. An optional aux
108 	 * may be following the inline message proper.
109 	 */
110 	IKM_TYPE_ALL_INLINED    = 0,
111 	/*
112 	 * IKM_TYPE_UDATA_OOL: Message header and descriptors are allocated inline,
113 	 * and message data, trailer, and aux are in buffer pointed to by ikm_udata.
114 	 * mach_msg_header_t is immediately after the kmsg header.
115 	 */
116 	IKM_TYPE_UDATA_OOL      = 1,
117 	/*
118 	 * IKM_TYPE_KDATA_OOL: The entire message is allocated out-of-line.
119 	 * {ikm_kdata, ikm_kdata_size} specifies the address and size
120 	 * of the allocation.
121 	 *
122 	 * There is no aux data.
123 	 */
124 	IKM_TYPE_KDATA_OOL      = 2,
125 	/*
126 	 * IKM_TYPE_ALL_OOL: Everything is allocated out-of-line. Message header
127 	 * and descriptors are allocated from typed kernel heap (kalloc_type)
128 	 * and pointed at by ikm_kdata.  The message data, trailer, and aux are
129 	 * in data buffer pointed to by ikm_udata.
130 	 */
131 	IKM_TYPE_ALL_OOL        = 3
132 });
133 
134 #define IKM_ALLOC_SIZE          256
135 #define IKM_SMALL_MSG_SIZE      168 /* for !IKM_TYPE_ALL_INLINED */
136 #define IKM_BIG_MSG_SIZE        192 /* for IKM_TYPE_ALL_INLINED  */
137 
138 /* used by mk_timer to prevent the message from being freed */
139 __options_closed_decl(ipc_kmsg_keep_alive_t, uint8_t, {
140 	IKM_KEEP_ALIVE_NONE   = 0x0, /* keep-alive not used on this message     */
141 	IKM_KEEP_ALIVE_OWNED  = 0x1, /* keep-alive used, owned by the subsystem */
142 	IKM_KEEP_ALIVE_IN_USE = 0x2, /* keep-alive used, message is in flight   */
143 });
144 
145 struct ipc_kmsg {
146 	queue_chain_t              ikm_link;
147 	ipc_port_t                 XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_voucher_port") ikm_voucher_port;   /* voucher port carried */
148 	struct ipc_importance_elem *ikm_importance;  /* inherited from */
149 	queue_chain_t              ikm_inheritance;  /* inherited from link */
150 	uint16_t                   ikm_aux_size;     /* size reserved for auxiliary data */
151 	ipc_kmsg_keep_alive_t      ikm_keep_alive;   /* only used for IKM_TYPE_ALL_INLINED */
152 	uint8_t                    __ikm_padding;
153 	uint32_t                   ikm_ppriority;    /* pthread priority of this kmsg */
154 	uint32_t                   ikm_signature;    /* sig for all kernel-processed data */
155 	ipc_object_copyin_flags_t  ikm_flags;
156 	mach_msg_qos_t             ikm_qos_override; /* qos override on this kmsg */
157 
158 	mach_msg_type_name_t       ikm_voucher_type: 6; /* disposition type the voucher came in with */
159 	ipc_kmsg_type_t            ikm_type: 2;
160 
161 	union {
162 		/*
163 		 * - ikm_big_data (IKM_TYPE_ALL_INLINED)
164 		 *   inline buffer used for everything
165 		 *
166 		 * - ikm_small_data (IKM_TYPE_UDATA_OOL)
167 		 *   inline buffer used for the kernel data/header
168 		 *
169 		 * - ikm_kdata{,_size} (IKM_TYPE_KDATA_OOL, IKM_TYPE_ALL_OOL)
170 		 *   kernel data buffer and size (with kernel pointers).
171 		 *
172 		 * - ikm_udata{,_size} (IKM_TYPE_UDATA_OOL, IKM_TYPE_ALL_OOL)
173 		 *   user data buffer and size (no kernel pointers).
174 		 *
175 		 * Note: ikm_big_data and ikm_small_data are at the same address
176 		 *       so that `ikm_header()` only has two cases.
177 		 *
178 		 *       dPAC-ed pointers follow so that linear overflows are
179 		 *       unlikely to be exploitable.
180 		 */
181 		uint32_t           ikm_big_data[IKM_BIG_MSG_SIZE / 4];
182 		struct {
183 			uint32_t   ikm_small_data[IKM_SMALL_MSG_SIZE / 4];
184 			void *XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_kdata") ikm_kdata;
185 			void *XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_udata") ikm_udata;
186 			mach_msg_size_t ikm_kdata_size;
187 			mach_msg_size_t ikm_udata_size;
188 		} __attribute__((packed, aligned(4)));
189 	};
190 };
191 
192 static_assert(sizeof(struct ipc_kmsg) == IKM_ALLOC_SIZE);
193 static_assert(offsetof(struct ipc_kmsg, ikm_big_data) +
194     IKM_BIG_MSG_SIZE == IKM_ALLOC_SIZE);
195 static_assert(offsetof(struct ipc_kmsg, ikm_small_data) + IKM_SMALL_MSG_SIZE +
196     2 * sizeof(void *) + 2 * sizeof(mach_msg_size_t) == IKM_ALLOC_SIZE);
197 
198 KALLOC_TYPE_VAR_DECLARE(KT_IPC_KMSG_KDATA_OOL);
199 
200 /*
201  * Exported interfaces
202  */
203 
204 typedef circle_queue_t                  ipc_kmsg_queue_t;
205 
206 #define ipc_kmsg_queue_init(queue)      circle_queue_init(queue)
207 
208 #define ipc_kmsg_queue_empty(queue)     circle_queue_empty(queue)
209 
210 #define ipc_kmsg_queue_element(elem) \
211 	cqe_element(elem, struct ipc_kmsg, ikm_link)
212 
213 #define ipc_kmsg_queue_first(queue) \
214 	cqe_queue_first(queue, struct ipc_kmsg, ikm_link)
215 
216 #define ipc_kmsg_queue_next(queue, elt) \
217 	cqe_queue_next(&(elt)->ikm_link, queue, struct ipc_kmsg, ikm_link)
218 
219 #define ipc_kmsg_enqueue(queue, kmsg) \
220 	circle_enqueue_tail(queue, &(kmsg)->ikm_link)
221 
222 #define ipc_kmsg_rmqueue(queue, kmsg) \
223 	circle_dequeue(queue, &(kmsg)->ikm_link)
224 
225 extern bool ipc_kmsg_enqueue_qos(
226 	ipc_kmsg_queue_t        queue,
227 	ipc_kmsg_t              kmsg);
228 
229 extern bool ipc_kmsg_override_qos(
230 	ipc_kmsg_queue_t        queue,
231 	ipc_kmsg_t              kmsg,
232 	mach_msg_qos_t          qos_ovr);
233 
234 /* Pull the (given) first kmsg out of a queue */
235 extern void ipc_kmsg_rmqueue_first(
236 	ipc_kmsg_queue_t        queue,
237 	ipc_kmsg_t              kmsg);
238 
239 __options_decl(ipc_kmsg_alloc_flags_t, uint32_t, {
240 	/* specify either user or kernel flag */
241 	IPC_KMSG_ALLOC_USER             = 0x0000,
242 	IPC_KMSG_ALLOC_KERNEL           = 0x0001,
243 
244 	IPC_KMSG_ALLOC_ZERO             = 0x0002,
245 	IPC_KMSG_ALLOC_ALL_INLINE       = 0x0004,
246 	IPC_KMSG_ALLOC_NOFAIL           = 0x0008,
247 	IPC_KMSG_ALLOC_LINEAR           = 0x0010,
248 	IPC_KMSG_ALLOC_USE_KEEP_ALIVE   = 0x0020, /* must call ipc_kmsg_keep_alive_abandon () */
249 });
250 
251 /* Allocate a kernel message */
252 extern ipc_kmsg_t ipc_kmsg_alloc(
253 	mach_msg_size_t         msg_size,
254 	mach_msg_size_t         aux_size,
255 	mach_msg_size_t         desc_count,
256 	ipc_kmsg_alloc_flags_t  flags);
257 
258 /* Free a kernel message buffer */
259 extern void ipc_kmsg_free(
260 	ipc_kmsg_t              kmsg);
261 
262 extern void ipc_kmsg_clean_descriptors(
263 	mach_msg_kdescriptor_t * kdesc __counted_by(number),
264 	mach_msg_type_number_t  number);
265 
266 extern void ipc_kmsg_sign_descriptors(
267 	mach_msg_kdescriptor_t *kdesc,
268 	mach_msg_size_t         dsc_count);
269 
270 __options_decl(ipc_kmsg_destroy_flags_t, uint32_t, {
271 	IPC_KMSG_DESTROY_ALL           = 0x0000,
272 	IPC_KMSG_DESTROY_SKIP_REMOTE   = 0x0001,
273 	IPC_KMSG_DESTROY_SKIP_LOCAL    = 0x0002,
274 	IPC_KMSG_DESTROY_NOT_SIGNED    = 0x0004,
275 });
276 /* Destroy kernel message */
277 extern void ipc_kmsg_destroy(
278 	ipc_kmsg_t                kmsg,
279 	ipc_kmsg_destroy_flags_t  flags);
280 
281 /* Enqueue kernel message for deferred destruction */
282 extern bool ipc_kmsg_delayed_destroy(
283 	ipc_kmsg_t              kmsg);
284 
285 /* Enqueue queue of kernel messages for deferred destruction */
286 extern bool ipc_kmsg_delayed_destroy_queue(
287 	ipc_kmsg_queue_t        queue);
288 
289 /* Process all the delayed message destroys */
290 extern void ipc_kmsg_reap_delayed(void);
291 
292 /* Try to mark a message as in use (setting IKM_KEEP_ALIVE_IN_USE). */
293 extern bool ipc_kmsg_keep_alive_try_reusing(
294 	ipc_kmsg_t              kmsg);
295 
296 /* Abandons a message that was allocated with IPC_KMSG_ALLOC_USE_KEEP_ALIVE. */
297 extern void ipc_kmsg_keep_alive_abandon(
298 	ipc_kmsg_t              kmsg);
299 
300 /* get the unshifted message header of a kmsg */
301 extern mach_msg_header_t *ikm_header(
302 	ipc_kmsg_t              kmsg);
303 
304 /* get the start address of user data (after the last descriptor) for a kmsg */
305 extern void *ikm_udata(
306 	ipc_kmsg_t              kmsg,
307 	mach_msg_size_t         desc_count,
308 	bool                    complex);
309 
310 extern void * ikm_udata_from_header(
311 	ipc_kmsg_t              kmsg);
312 
313 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
314 extern mach_msg_return_t ipc_kmsg_get_from_kernel(
315 	mach_msg_header_t      *msg,
316 	mach_msg_size_t         size,
317 	mach_msg_option64_t     options,
318 	ipc_kmsg_t             *kmsgp);
319 
320 /* Send a message to a port */
321 extern mach_msg_return_t ipc_kmsg_send(
322 	ipc_kmsg_t              kmsg,
323 	mach_msg_option64_t     options,
324 	mach_msg_timeout_t      timeout_val);
325 
326 /* Copy a kernel message buffer to a user message */
327 extern mach_msg_return_t ipc_kmsg_put_to_user(
328 	ipc_kmsg_t              kmsg,     /* scalar or vector */
329 	mach_msg_recv_bufs_t   *recv_bufs,
330 	mach_msg_recv_result_t *msgr,
331 	mach_msg_option64_t     option,
332 	vm_map_t                map,
333 	mach_msg_return_t       mr);
334 
335 /* Copy a kernel message buffer to a kernel message */
336 extern void ipc_kmsg_put_to_kernel(
337 	mach_msg_header_t      *msg,
338 	mach_msg_option64_t     options,
339 	ipc_kmsg_t              kmsg,
340 	mach_msg_size_t         size);
341 
342 /* Copyin data, port rights and out-of-line memory from a user message */
343 extern mach_msg_return_t ipc_kmsg_copyin_from_user(
344 	ipc_kmsg_t              kmsg,
345 	mach_msg_send_uctx_t   *send_uctx,
346 	ipc_space_t             space,
347 	vm_map_t                map,
348 	mach_msg_priority_t     priority,
349 	mach_msg_option64_t    *optionp);
350 
351 /* Copyin port rights and out-of-line memory from a kernel message */
352 extern mach_msg_return_t ipc_kmsg_copyin_from_kernel(
353 	ipc_kmsg_t              kmsg);
354 
355 /* Copyout the header and body to a user message */
356 extern mach_msg_return_t ipc_kmsg_copyout(
357 	ipc_kmsg_t              kmsg,
358 	ipc_space_t             space,
359 	vm_map_t                map,
360 	mach_msg_option64_t     option);
361 
362 /* Copyout port rights and out-of-line memory to a user message,
363  *  not reversing the ports in the header */
364 extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
365 	ipc_kmsg_t              kmsg,
366 	ipc_space_t             space,
367 	vm_map_t                map);
368 
369 /* Compute size of message as copied out to the specified space/map */
370 extern mach_msg_size_t ipc_kmsg_copyout_size(
371 	ipc_kmsg_t              kmsg,
372 	vm_map_t                map);
373 
374 /* Copyout the destination port in the message */
375 extern void ipc_kmsg_copyout_dest_to_user(
376 	ipc_kmsg_t              kmsg,
377 	ipc_space_t             space);
378 
379 /* kernel's version of ipc_kmsg_copyout_dest_to_user */
380 extern void ipc_kmsg_copyout_dest_to_kernel(
381 	ipc_kmsg_t              kmsg,
382 	ipc_space_t             space);
383 
384 /* Returns a pointer to a thread group in the kmsg if any. Caller has a
385  * reference to the kmsg */
386 extern struct thread_group *ipc_kmsg_get_thread_group(
387 	ipc_kmsg_t              kmsg);
388 
389 extern mach_msg_trailer_size_t ipc_kmsg_trailer_size(
390 	mach_msg_option64_t     option,
391 	vm_map_t                map);
392 
393 extern mach_msg_max_trailer_t *ipc_kmsg_get_trailer(
394 	ipc_kmsg_t              kmsg);
395 
396 extern void ipc_kmsg_set_voucher_port(
397 	ipc_kmsg_t              kmsg,
398 	ipc_port_t              voucher,
399 	mach_msg_type_name_t    type);
400 
401 extern ipc_port_t ipc_kmsg_get_voucher_port(
402 	ipc_kmsg_t              kmsg);
403 
404 extern void ipc_kmsg_clear_voucher_port(
405 	ipc_kmsg_t              kmsg);
406 
407 /* checks signature and returns descriptor count */
408 extern mach_msg_size_t ipc_kmsg_validate_signature(
409 	ipc_kmsg_t              kmsg) __result_use_check;
410 
411 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
412 extern void ipc_kmsg_trace_send(
413 	ipc_kmsg_t              kmsg,
414 	mach_msg_option64_t     option);
415 #else
416 #define ipc_kmsg_trace_send(a, b) do { } while (0)
417 #endif
418 
419 #if (DEVELOPMENT || DEBUG)
420 vm_offset_t ikm_kdata_end(ipc_kmsg_t kmsg);
421 #endif
422 
423 #endif  /* _IPC_IPC_KMSG_H_ */
424