1 /*
2 * Copyright (c) 2000-2020 Apple 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.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Operations on kernel messages.
71 */
72
73
74 #include <mach/mach_types.h>
75 #include <mach/boolean.h>
76 #include <mach/kern_return.h>
77 #include <mach/message.h>
78 #include <mach/port.h>
79 #include <mach/vm_map.h>
80 #include <mach/mach_vm.h>
81 #include <mach/vm_statistics.h>
82
83 #include <kern/kern_types.h>
84 #include <kern/assert.h>
85 #include <kern/debug.h>
86 #include <kern/ipc_kobject.h>
87 #include <kern/kalloc.h>
88 #include <kern/zalloc.h>
89 #include <kern/processor.h>
90 #include <kern/thread.h>
91 #include <kern/thread_group.h>
92 #include <kern/sched_prim.h>
93 #include <kern/misc_protos.h>
94 #include <kern/cpu_data.h>
95 #include <kern/policy_internal.h>
96 #include <kern/mach_filter.h>
97
98 #include <pthread/priority_private.h>
99
100 #include <machine/limits.h>
101
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_kern.h>
105
106 #include <ipc/port.h>
107 #include <ipc/ipc_types.h>
108 #include <ipc/ipc_entry.h>
109 #include <ipc/ipc_kmsg.h>
110 #include <ipc/ipc_notify.h>
111 #include <ipc/ipc_object.h>
112 #include <ipc/ipc_space.h>
113 #include <ipc/ipc_port.h>
114 #include <ipc/ipc_right.h>
115 #include <ipc/ipc_hash.h>
116 #include <ipc/ipc_importance.h>
117 #include <ipc/ipc_service_port.h>
118 #include <libkern/coreanalytics/coreanalytics.h>
119
120 #if MACH_FLIPC
121 #include <kern/mach_node.h>
122 #include <ipc/flipc.h>
123 #endif
124
125 #include <os/overflow.h>
126
127 #include <security/mac_mach_internal.h>
128
129 #include <device/device_server.h>
130
131 #include <string.h>
132
133 #if DEBUG
134 #define DEBUG_MSGS_K64 1
135 #endif
136
137 #include <sys/kdebug.h>
138 #include <libkern/OSAtomic.h>
139
140 #include <libkern/crypto/sha2.h>
141
142 #include <ptrauth.h>
143 #if __has_feature(ptrauth_calls)
144 #include <libkern/ptrauth_utils.h>
145 #endif
146
147 #if CONFIG_CSR
148 #include <sys/csr.h>
149 #endif
150
151 /*
152 * In kernel, complex mach msg have a simpler representation than userspace:
153 *
154 * <header>
155 * <desc-count>
156 * <descriptors> * desc-count
157 * <body>
158 *
159 * And the descriptors are of a fake type `mach_msg_descriptor_t`,
160 * that is large enough to accommodate for any possible representation.
161 *
162 * The `type` field of any desciptor is always at the same offset,
163 * and the smallest possible descriptor is of size MACH_MSG_DESC_MIN_SIZE.
164 *
165 * Note:
166 * - KERN_DESC_SIZE is 16 on all kernels
167 * - MACH_MSG_DESC_MIN_SIZE is 12 on all kernels
168 */
169
170 #define KERNEL_DESC_SIZE sizeof(mach_msg_descriptor_t)
171 #define MACH_MSG_DESC_MIN_SIZE sizeof(mach_msg_type_descriptor_t)
172
173 #define USER_HEADER_SIZE_DELTA \
174 ((mach_msg_size_t)(sizeof(mach_msg_header_t) - sizeof(mach_msg_user_header_t)))
175
176 #define USER_DESC_MAX_DELTA \
177 (KERNEL_DESC_SIZE - MACH_MSG_DESC_MIN_SIZE)
178
179 #define mach_validate_desc_type(t) \
180 static_assert(MACH_MSG_DESC_MIN_SIZE <= sizeof(t) && \
181 sizeof(t) <= sizeof(mach_msg_descriptor_t))
182
183 mach_validate_desc_type(mach_msg_descriptor_t);
184 mach_validate_desc_type(mach_msg_port_descriptor_t);
185 mach_validate_desc_type(mach_msg_user_port_descriptor_t);
186 mach_validate_desc_type(mach_msg_type_descriptor_t);
187 mach_validate_desc_type(mach_msg_ool_descriptor32_t);
188 mach_validate_desc_type(mach_msg_ool_descriptor64_t);
189 mach_validate_desc_type(mach_msg_ool_ports_descriptor32_t);
190 mach_validate_desc_type(mach_msg_ool_ports_descriptor64_t);
191 mach_validate_desc_type(mach_msg_guarded_port_descriptor32_t);
192 mach_validate_desc_type(mach_msg_guarded_port_descriptor64_t);
193
194 extern char *proc_name_address(struct proc *p);
195
196 /*
197 * As CA framework replies on successfully allocating zalloc memory,
198 * we maintain a small buffer that gets flushed when full. This helps us avoid taking spinlocks when working with CA.
199 */
200 #define REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE 2
201
202 struct reply_port_semantics_violations_rb_entry {
203 char proc_name[CA_PROCNAME_LEN];
204 };
205 struct reply_port_semantics_violations_rb_entry reply_port_semantics_violations_rb[REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE];
206 static uint8_t reply_port_semantics_violations_rb_index = 0;
207
208 LCK_GRP_DECLARE(reply_port_telemetry_lock_grp, "reply_port_telemetry_lock_grp");
209 LCK_SPIN_DECLARE(reply_port_telemetry_lock, &reply_port_telemetry_lock_grp);
210
211 /* Telemetry: report back the process name violating reply port semantics */
212 CA_EVENT(reply_port_semantics_violations,
213 CA_STATIC_STRING(CA_PROCNAME_LEN), proc_name);
214
215
216 /* Routine: flush_reply_port_semantics_violations_telemetry
217 * Conditions:
218 * Assumes the reply_port_telemetry_lock is held.
219 * Unlocks it before returning.
220 */
221 static void
flush_reply_port_semantics_violations_telemetry()222 flush_reply_port_semantics_violations_telemetry()
223 {
224 struct reply_port_semantics_violations_rb_entry local_rb[REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE];
225 uint8_t local_rb_index = 0;
226
227 if (__improbable(reply_port_semantics_violations_rb_index > REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE)) {
228 panic("Invalid reply port semantics violations buffer index %d > %d",
229 reply_port_semantics_violations_rb_index, REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE);
230 }
231
232 /*
233 * We operate on local copy of telemetry buffer because CA framework relies on successfully
234 * allocating zalloc memory. It can not do that if we are accessing the shared buffer
235 * with spin locks held.
236 */
237 while (local_rb_index != reply_port_semantics_violations_rb_index) {
238 local_rb[local_rb_index] = reply_port_semantics_violations_rb[local_rb_index];
239 local_rb_index++;
240 }
241
242 lck_spin_unlock(&reply_port_telemetry_lock);
243
244 while (local_rb_index > 0) {
245 struct reply_port_semantics_violations_rb_entry *entry = &local_rb[--local_rb_index];
246
247 ca_event_t ca_event = CA_EVENT_ALLOCATE_FLAGS(reply_port_semantics_violations, Z_NOWAIT);
248 if (ca_event) {
249 CA_EVENT_TYPE(reply_port_semantics_violations) * event = ca_event->data;
250 strlcpy(event->proc_name, entry->proc_name, CA_PROCNAME_LEN);
251 CA_EVENT_SEND(ca_event);
252 }
253 }
254
255 /*
256 * Finally call out the buffer as empty. This is also a sort of rate limiting mechanisms for the events.
257 * Events will get dropped until the buffer is not fully flushed.
258 */
259 lck_spin_lock(&reply_port_telemetry_lock);
260 reply_port_semantics_violations_rb_index = 0;
261 }
262
263 static void
stash_reply_port_semantics_violations_telemetry()264 stash_reply_port_semantics_violations_telemetry()
265 {
266 struct reply_port_semantics_violations_rb_entry *entry;
267
268 lck_spin_lock(&reply_port_telemetry_lock);
269
270 if (reply_port_semantics_violations_rb_index == REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE) {
271 /* Dropping the event since buffer is full. */
272 lck_spin_unlock(&reply_port_telemetry_lock);
273 return;
274 }
275
276 task_t task = current_task_early();
277 if (task) {
278 char *proc_name = (char *) "unknown";
279 #ifdef MACH_BSD
280 proc_name = proc_name_address(get_bsdtask_info(task));
281 #endif /* MACH_BSD */
282 entry = &reply_port_semantics_violations_rb[reply_port_semantics_violations_rb_index++];
283 strlcpy(entry->proc_name, proc_name, CA_PROCNAME_LEN);
284 }
285
286 if (reply_port_semantics_violations_rb_index == REPLY_PORT_SEMANTICS_VIOLATIONS_RB_SIZE) {
287 flush_reply_port_semantics_violations_telemetry();
288 }
289
290 lck_spin_unlock(&reply_port_telemetry_lock);
291 }
292
293 /* Update following two helpers if new descriptor type is added */
294 static_assert(MACH_MSG_DESCRIPTOR_MAX == MACH_MSG_GUARDED_PORT_DESCRIPTOR);
295
296 static inline mach_msg_size_t
ikm_user_desc_size(mach_msg_descriptor_type_t type,bool is_task_64bit)297 ikm_user_desc_size(
298 mach_msg_descriptor_type_t type,
299 bool is_task_64bit)
300 {
301 if (is_task_64bit) {
302 switch (type) {
303 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
304 case MACH_MSG_OOL_DESCRIPTOR:
305 return sizeof(mach_msg_ool_descriptor64_t);
306 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
307 return sizeof(mach_msg_ool_ports_descriptor64_t);
308 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
309 return sizeof(mach_msg_guarded_port_descriptor64_t);
310 default: /* MACH_MSG_PORT_DESCRIPTOR */
311 return sizeof(mach_msg_user_port_descriptor_t);
312 }
313 } else {
314 switch (type) {
315 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
316 case MACH_MSG_OOL_DESCRIPTOR:
317 return sizeof(mach_msg_ool_descriptor32_t);
318 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
319 return sizeof(mach_msg_ool_ports_descriptor32_t);
320 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
321 return sizeof(mach_msg_guarded_port_descriptor32_t);
322 default: /* MACH_MSG_PORT_DESCRIPTOR */
323 return sizeof(mach_msg_user_port_descriptor_t);
324 }
325 }
326 }
327
328 static inline bool
ikm_user_desc_type_valid(mach_msg_descriptor_type_t type)329 ikm_user_desc_type_valid(
330 mach_msg_descriptor_type_t type)
331 {
332 return type <= MACH_MSG_DESCRIPTOR_MAX;
333 }
334
335 /*
336 * Measure the total descriptor size in a kmsg.
337 *
338 * Condition:
339 * Descriptors must have valid type and message is well-formed.
340 * See ikm_check_descriptors().
341 */
342 static mach_msg_size_t
ikm_total_desc_size(ipc_kmsg_t kmsg,vm_map_t map,mach_msg_size_t body_adj,mach_msg_size_t header_adj,bool user_descs)343 ikm_total_desc_size(
344 ipc_kmsg_t kmsg,
345 vm_map_t map,
346 mach_msg_size_t body_adj, /* gap formed during copyout_body memmove */
347 mach_msg_size_t header_adj, /* gap formed during put_to_user */
348 bool user_descs) /* are descriptors user sized */
349 {
350 mach_msg_size_t total = 0;
351 bool is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
352 mach_msg_size_t hdr_size = sizeof(mach_msg_header_t) - header_adj;
353 /*
354 * hdr can be of type (mach_msg_user_header_t *) or (mach_msg_header_t *).
355 * following code relies on the fact that both structs share the same
356 * first two fields. (msgh_bits and msgh_size)
357 */
358 static_assert(offsetof(mach_msg_user_header_t, msgh_bits) ==
359 offsetof(mach_msg_header_t, msgh_bits));
360 static_assert(offsetof(mach_msg_user_header_t, msgh_size) ==
361 offsetof(mach_msg_header_t, msgh_size));
362
363 mach_msg_header_t *hdr = (mach_msg_header_t *)((vm_offset_t)ikm_header(kmsg) + header_adj);
364
365 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
366 mach_msg_body_t *body;
367 mach_msg_type_number_t dsc_count;
368 mach_msg_size_t dsize;
369 mach_msg_descriptor_t *daddr;
370
371 body = (mach_msg_body_t *)((vm_offset_t)hdr + hdr_size);
372 dsc_count = body->msgh_descriptor_count;
373
374 if (!user_descs) {
375 return dsc_count * KERNEL_DESC_SIZE;
376 }
377
378 daddr = (mach_msg_descriptor_t *)((vm_offset_t)(body + 1) + body_adj);
379 for (uint32_t i = 0; i < dsc_count; i++) {
380 dsize = ikm_user_desc_size(daddr->type.type, is_task_64bit);
381 daddr = (mach_msg_descriptor_t *)((vm_offset_t)daddr + dsize);
382 total += dsize;
383 }
384 }
385
386 return total;
387 }
388
389 /* Pre-validate descriptors and message size during copyin */
390 __result_use_check
391 static mach_msg_return_t
ikm_check_descriptors(ipc_kmsg_t kmsg,vm_map_t map,mach_msg_size_t copied_in)392 ikm_check_descriptors(
393 ipc_kmsg_t kmsg, /* a complex message */
394 vm_map_t map,
395 mach_msg_size_t copied_in)
396 {
397 mach_msg_body_t *body;
398 mach_msg_type_number_t dsc_count;
399 mach_msg_size_t dsize;
400 vm_offset_t end;
401 mach_msg_descriptor_t *daddr;
402
403 bool is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
404 mach_msg_size_t hdr_size = sizeof(mach_msg_header_t);
405 mach_msg_size_t base_size = sizeof(mach_msg_base_t);
406 mach_msg_header_t *hdr = ikm_header(kmsg);
407
408 assert(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX);
409
410 body = (mach_msg_body_t *)((vm_offset_t)hdr + hdr_size);
411 dsc_count = body->msgh_descriptor_count;
412 daddr = (mach_msg_descriptor_t *)(vm_offset_t)(body + 1);
413 /* Maximum possible descriptor end address */
414 end = (vm_offset_t)hdr + base_size + copied_in;
415
416 for (uint32_t i = 0; i < dsc_count; i++) {
417 if ((vm_offset_t)daddr + MACH_MSG_DESC_MIN_SIZE > end) {
418 return MACH_SEND_MSG_TOO_SMALL;
419 }
420 /* Now we can access daddr->type safely */
421 if (!ikm_user_desc_type_valid(daddr->type.type)) {
422 return MACH_SEND_INVALID_TYPE;
423 }
424 dsize = ikm_user_desc_size(daddr->type.type, is_task_64bit);
425
426 if ((vm_offset_t)daddr + dsize > end) {
427 return MACH_SEND_MSG_TOO_SMALL;
428 }
429 daddr = (mach_msg_descriptor_t *)((vm_offset_t)daddr + dsize);
430 }
431
432 return MACH_MSG_SUCCESS;
433 }
434
435 /* Measure the size of user data content carried in kmsg. */
436 static mach_msg_size_t
ikm_content_size(ipc_kmsg_t kmsg,vm_map_t map,mach_msg_size_t header_adj,bool user_descs)437 ikm_content_size(
438 ipc_kmsg_t kmsg,
439 vm_map_t map,
440 mach_msg_size_t header_adj, /* gap formed during put_to_user */
441 bool user_descs) /* are descriptors user sized */
442 {
443 mach_msg_size_t hdr_size = sizeof(mach_msg_header_t) - header_adj;
444 mach_msg_size_t base_size = hdr_size + sizeof(mach_msg_body_t);
445 /*
446 * hdr can be of type (mach_msg_user_header_t *) or (mach_msg_header_t *).
447 * following code relies on the fact that both structs share the same
448 * first two fields. (msgh_bits and msgh_size)
449 */
450 mach_msg_header_t *hdr = (mach_msg_header_t *)((vm_offset_t)ikm_header(kmsg) + header_adj);
451
452 assert(hdr->msgh_size >= hdr_size);
453 if (hdr->msgh_size <= hdr_size) {
454 return 0;
455 }
456
457 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
458 assert(hdr->msgh_size >= base_size +
459 ikm_total_desc_size(kmsg, map, 0, header_adj, user_descs));
460 return hdr->msgh_size - base_size -
461 ikm_total_desc_size(kmsg, map, 0, header_adj, user_descs);
462 } else {
463 assert(hdr->msgh_size > hdr_size);
464 return hdr->msgh_size - hdr_size;
465 }
466 }
467
468 /* Size of kmsg header (plus body and descriptors for complex messages) */
469 static mach_msg_size_t
ikm_kdata_size(ipc_kmsg_t kmsg,vm_map_t map,mach_msg_size_t header_adj,bool user_descs)470 ikm_kdata_size(
471 ipc_kmsg_t kmsg,
472 vm_map_t map,
473 mach_msg_size_t header_adj,
474 bool user_descs)
475 {
476 mach_msg_size_t content_size = ikm_content_size(kmsg, map, header_adj, user_descs);
477 /*
478 * hdr can be of type (mach_msg_user_header_t *) or (mach_msg_header_t *).
479 * following code relies on the fact that both structs share the same
480 * first two fields. (msgh_bits and msgh_size)
481 */
482 mach_msg_header_t *hdr = (mach_msg_header_t *)((vm_offset_t)ikm_header(kmsg) + header_adj);
483
484 assert(hdr->msgh_size > content_size);
485 return hdr->msgh_size - content_size;
486 }
487
488 #if __has_feature(ptrauth_calls)
489 typedef uintptr_t ikm_sig_scratch_t;
490
491 static void
ikm_init_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)492 ikm_init_sig(
493 __unused ipc_kmsg_t kmsg,
494 ikm_sig_scratch_t *scratchp)
495 {
496 *scratchp = OS_PTRAUTH_DISCRIMINATOR("kmsg.ikm_signature");
497 }
498
499 static void
ikm_chunk_sig(ipc_kmsg_t kmsg,void * data,size_t len,ikm_sig_scratch_t * scratchp)500 ikm_chunk_sig(
501 ipc_kmsg_t kmsg,
502 void *data,
503 size_t len,
504 ikm_sig_scratch_t *scratchp)
505 {
506 int ptrauth_flags;
507 void *trailerp;
508
509 /*
510 * if we happen to be doing the trailer chunk,
511 * diversify with the ptrauth-ed trailer pointer -
512 * as that is unchanging for the kmsg
513 */
514 trailerp = (void *)ipc_kmsg_get_trailer(kmsg, false);
515
516 ptrauth_flags = (data == trailerp) ? PTRAUTH_ADDR_DIVERSIFY : 0;
517 *scratchp = ptrauth_utils_sign_blob_generic(data, len, *scratchp, ptrauth_flags);
518 }
519
520 static uintptr_t
ikm_finalize_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)521 ikm_finalize_sig(
522 __unused ipc_kmsg_t kmsg,
523 ikm_sig_scratch_t *scratchp)
524 {
525 return *scratchp;
526 }
527
528 #elif defined(CRYPTO_SHA2) && !defined(__x86_64__)
529
530 typedef SHA256_CTX ikm_sig_scratch_t;
531
532 static void
ikm_init_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)533 ikm_init_sig(
534 __unused ipc_kmsg_t kmsg,
535 ikm_sig_scratch_t *scratchp)
536 {
537 SHA256_Init(scratchp);
538 SHA256_Update(scratchp, &vm_kernel_addrhash_salt_ext, sizeof(uint64_t));
539 }
540
541 static void
ikm_chunk_sig(__unused ipc_kmsg_t kmsg,void * data,size_t len,ikm_sig_scratch_t * scratchp)542 ikm_chunk_sig(
543 __unused ipc_kmsg_t kmsg,
544 void *data,
545 size_t len,
546 ikm_sig_scratch_t *scratchp)
547 {
548 SHA256_Update(scratchp, data, len);
549 }
550
551 static uintptr_t
ikm_finalize_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)552 ikm_finalize_sig(
553 __unused ipc_kmsg_t kmsg,
554 ikm_sig_scratch_t *scratchp)
555 {
556 uintptr_t sha_digest[SHA256_DIGEST_LENGTH / sizeof(uintptr_t)];
557
558 SHA256_Final((uint8_t *)sha_digest, scratchp);
559
560 /*
561 * Only use one uintptr_t sized part of result for space and compat reasons.
562 * Truncation is better than XOR'ing the chunks together in hopes of higher
563 * entropy - because of its lower risk of collisions.
564 */
565 return *sha_digest;
566 }
567
568 #else
569 /* Stubbed out implementation (for __x86_64__ for now) */
570
571 typedef uintptr_t ikm_sig_scratch_t;
572
573 static void
ikm_init_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)574 ikm_init_sig(
575 __unused ipc_kmsg_t kmsg,
576 ikm_sig_scratch_t *scratchp)
577 {
578 *scratchp = 0;
579 }
580
581 static void
ikm_chunk_sig(__unused ipc_kmsg_t kmsg,__unused void * data,__unused size_t len,__unused ikm_sig_scratch_t * scratchp)582 ikm_chunk_sig(
583 __unused ipc_kmsg_t kmsg,
584 __unused void *data,
585 __unused size_t len,
586 __unused ikm_sig_scratch_t *scratchp)
587 {
588 return;
589 }
590
591 static uintptr_t
ikm_finalize_sig(__unused ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)592 ikm_finalize_sig(
593 __unused ipc_kmsg_t kmsg,
594 ikm_sig_scratch_t *scratchp)
595 {
596 return *scratchp;
597 }
598
599 #endif
600
601 static void
ikm_header_sig(ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)602 ikm_header_sig(
603 ipc_kmsg_t kmsg,
604 ikm_sig_scratch_t *scratchp)
605 {
606 mach_msg_size_t dsc_count;
607 mach_msg_base_t base;
608 boolean_t complex;
609
610 mach_msg_header_t *hdr = ikm_header(kmsg);
611 /* take a snapshot of the message header/body-count */
612 base.header = *hdr;
613 complex = ((base.header.msgh_bits & MACH_MSGH_BITS_COMPLEX) != 0);
614 if (complex) {
615 dsc_count = ((mach_msg_body_t *)(hdr + 1))->msgh_descriptor_count;
616 } else {
617 dsc_count = 0;
618 }
619 base.body.msgh_descriptor_count = dsc_count;
620
621 /* compute sig of a copy of the header with all varying bits masked off */
622 base.header.msgh_bits &= MACH_MSGH_BITS_USER;
623 base.header.msgh_bits &= ~MACH_MSGH_BITS_VOUCHER_MASK;
624 ikm_chunk_sig(kmsg, &base, sizeof(mach_msg_base_t), scratchp);
625 }
626
627 static void
ikm_trailer_sig(ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)628 ikm_trailer_sig(
629 ipc_kmsg_t kmsg,
630 ikm_sig_scratch_t *scratchp)
631 {
632 mach_msg_max_trailer_t *trailerp;
633
634 /* Add sig of the trailer contents */
635 trailerp = ipc_kmsg_get_trailer(kmsg, false);
636 ikm_chunk_sig(kmsg, trailerp, sizeof(*trailerp), scratchp);
637 }
638
639 /* Compute the signature for the body bits of a message */
640 static void
ikm_body_sig(ipc_kmsg_t kmsg,ikm_sig_scratch_t * scratchp)641 ikm_body_sig(
642 ipc_kmsg_t kmsg,
643 ikm_sig_scratch_t *scratchp)
644 {
645 mach_msg_descriptor_t *kern_dsc;
646 mach_msg_size_t dsc_count;
647 mach_msg_body_t *body;
648 mach_msg_size_t i;
649
650 mach_msg_header_t *hdr = ikm_header(kmsg);
651
652 if ((hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX) == 0) {
653 return;
654 }
655 body = (mach_msg_body_t *) (hdr + 1);
656 dsc_count = body->msgh_descriptor_count;
657
658 if (dsc_count == 0) {
659 return;
660 }
661
662 kern_dsc = (mach_msg_descriptor_t *) (body + 1);
663
664 /* Compute the signature for the whole descriptor array */
665 ikm_chunk_sig(kmsg, kern_dsc, sizeof(*kern_dsc) * dsc_count, scratchp);
666
667 /* look for descriptor contents that need a signature */
668 for (i = 0; i < dsc_count; i++) {
669 switch (kern_dsc[i].type.type) {
670 case MACH_MSG_PORT_DESCRIPTOR:
671 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
672 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
673 case MACH_MSG_OOL_DESCRIPTOR:
674 break;
675
676 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
677 mach_msg_ool_ports_descriptor_t *ports_dsc;
678
679 /* Compute sig for the port/object pointers */
680 ports_dsc = (mach_msg_ool_ports_descriptor_t *)&kern_dsc[i];
681 ikm_chunk_sig(kmsg, ports_dsc->address, ports_dsc->count * sizeof(ipc_object_t), scratchp);
682 break;
683 }
684 default: {
685 panic("ipc_kmsg_body_sig: invalid message descriptor");
686 }
687 }
688 }
689 }
690
691 static void
ikm_sign(ipc_kmsg_t kmsg)692 ikm_sign(ipc_kmsg_t kmsg)
693 {
694 ikm_sig_scratch_t scratch;
695 uintptr_t sig;
696
697 zone_require(ipc_kmsg_zone, kmsg);
698
699 ikm_init_sig(kmsg, &scratch);
700
701 /* First sign header and trailer and store a partial sig */
702 ikm_header_sig(kmsg, &scratch);
703 ikm_trailer_sig(kmsg, &scratch);
704
705 #if __has_feature(ptrauth_calls)
706 /*
707 * On PAC devices lower 32 bits of the signature generated by G Key are
708 * always zeros. Use that space to store header + trailer partial sig.
709 *
710 * See: ptrauth_utils_sign_blob_generic()
711 */
712 kmsg->ikm_sig_partial = (uint32_t)(ikm_finalize_sig(kmsg, &scratch) >> 32);
713 #endif
714
715 /* Then sign body, which may be large: ~ BigO(# descriptors) */
716 ikm_body_sig(kmsg, &scratch);
717
718 sig = ikm_finalize_sig(kmsg, &scratch);
719 #if __has_feature(ptrauth_calls)
720 kmsg->ikm_sig_full = (uint32_t)(sig >> 32);
721 #else
722 kmsg->ikm_signature = sig;
723 #endif
724 }
725
726 unsigned int ikm_signature_failures;
727 unsigned int ikm_signature_failure_id;
728 #if (DEVELOPMENT || DEBUG)
729 unsigned int ikm_signature_panic_disable;
730 unsigned int ikm_signature_header_failures;
731 unsigned int ikm_signature_trailer_failures;
732 #endif
733
734 /*
735 * Purpose:
736 * Validate kmsg signature.
737 * partial: Only validate header + trailer.
738 *
739 * Condition:
740 * On non-PAC devices, `partial` must be set to false.
741 */
742 static void
ikm_validate_sig_internal(ipc_kmsg_t kmsg,bool partial)743 ikm_validate_sig_internal(
744 ipc_kmsg_t kmsg,
745 bool partial)
746 {
747 ikm_sig_scratch_t scratch;
748 uintptr_t expected;
749 uintptr_t sig;
750 char *str;
751
752 zone_require(ipc_kmsg_zone, kmsg);
753
754 ikm_init_sig(kmsg, &scratch);
755
756 ikm_header_sig(kmsg, &scratch);
757
758 ikm_trailer_sig(kmsg, &scratch);
759
760 if (partial) {
761 #if __has_feature(ptrauth_calls)
762 /* Do partial evaluation of header + trailer signature */
763 sig = ikm_finalize_sig(kmsg, &scratch);
764 expected = (uintptr_t)kmsg->ikm_sig_partial << 32;
765 if (sig != expected) {
766 #if (DEVELOPMENT || DEBUG)
767 ikm_signature_trailer_failures++;
768 #endif
769 str = "header trailer";
770 goto failure;
771 }
772 return;
773 #else
774 panic("Partial kmsg signature validation only supported on PAC devices.");
775 #endif
776 }
777
778 ikm_body_sig(kmsg, &scratch);
779 sig = ikm_finalize_sig(kmsg, &scratch);
780
781 #if __has_feature(ptrauth_calls)
782 expected = (uintptr_t)kmsg->ikm_sig_full << 32;
783 #else
784 expected = kmsg->ikm_signature;
785 #endif
786
787 if (sig != expected) {
788 ikm_signature_failures++;
789 str = "full";
790
791 #if __has_feature(ptrauth_calls)
792 failure:
793 #endif
794 {
795 mach_msg_id_t id = ikm_header(kmsg)->msgh_id;
796
797 ikm_signature_failure_id = id;
798 #if (DEVELOPMENT || DEBUG)
799 if (ikm_signature_panic_disable) {
800 return;
801 }
802 #endif
803 panic("ikm_validate_sig: %s signature mismatch: kmsg=0x%p, id=%d, sig=0x%zx (expected 0x%zx)",
804 str, kmsg, id, sig, expected);
805 }
806 }
807 }
808
809 static void
ikm_validate_sig(ipc_kmsg_t kmsg)810 ikm_validate_sig(
811 ipc_kmsg_t kmsg)
812 {
813 ikm_validate_sig_internal(kmsg, false);
814 }
815
816 /*
817 * Purpose:
818 * Validate kmsg signature. [Exported in header]
819 * partial: Only validate header + trailer.
820 *
821 * Condition:
822 * On non-PAC devices, `partial` must be set to false.
823 */
824 void
ipc_kmsg_validate_sig(ipc_kmsg_t kmsg,bool partial)825 ipc_kmsg_validate_sig(
826 ipc_kmsg_t kmsg,
827 bool partial)
828 {
829 ikm_validate_sig_internal(kmsg, partial);
830 }
831
832 #if DEBUG_MSGS_K64
833 extern void ipc_pset_print64(
834 ipc_pset_t pset);
835
836 extern void ipc_kmsg_print64(
837 ipc_kmsg_t kmsg,
838 const char *str);
839
840 extern void ipc_msg_print64(
841 mach_msg_header_t *msgh);
842
843 extern ipc_port_t ipc_name_to_data64(
844 task_t task,
845 mach_port_name_t name);
846
847 /*
848 * Forward declarations
849 */
850 void ipc_msg_print_untyped64(
851 mach_msg_body_t *body);
852
853 const char * ipc_type_name64(
854 int type_name,
855 boolean_t received);
856
857 void ipc_print_type_name64(
858 int type_name);
859
860 const char *
861 msgh_bit_decode64(
862 mach_msg_bits_t bit);
863
864 const char *
865 mm_copy_options_string64(
866 mach_msg_copy_options_t option);
867
868 void db_print_msg_uid64(mach_msg_header_t *);
869
870 static void
ipc_msg_body_print64(void * body,int size)871 ipc_msg_body_print64(void *body, int size)
872 {
873 uint32_t *word = (uint32_t *) body;
874 uint32_t *end = (uint32_t *)(((uintptr_t) body) + size
875 - sizeof(mach_msg_header_t));
876 int i;
877
878 kprintf(" body(%p-%p):\n %p: ", body, end, word);
879 for (;;) {
880 for (i = 0; i < 8; i++, word++) {
881 if (word >= end) {
882 kprintf("\n");
883 return;
884 }
885 kprintf("%08x ", *word);
886 }
887 kprintf("\n %p: ", word);
888 }
889 }
890
891
892 const char *
ipc_type_name64(int type_name,boolean_t received)893 ipc_type_name64(
894 int type_name,
895 boolean_t received)
896 {
897 switch (type_name) {
898 case MACH_MSG_TYPE_PORT_NAME:
899 return "port_name";
900
901 case MACH_MSG_TYPE_MOVE_RECEIVE:
902 if (received) {
903 return "port_receive";
904 } else {
905 return "move_receive";
906 }
907
908 case MACH_MSG_TYPE_MOVE_SEND:
909 if (received) {
910 return "port_send";
911 } else {
912 return "move_send";
913 }
914
915 case MACH_MSG_TYPE_MOVE_SEND_ONCE:
916 if (received) {
917 return "port_send_once";
918 } else {
919 return "move_send_once";
920 }
921
922 case MACH_MSG_TYPE_COPY_SEND:
923 return "copy_send";
924
925 case MACH_MSG_TYPE_MAKE_SEND:
926 return "make_send";
927
928 case MACH_MSG_TYPE_MAKE_SEND_ONCE:
929 return "make_send_once";
930
931 default:
932 return (char *) 0;
933 }
934 }
935
936 void
ipc_print_type_name64(int type_name)937 ipc_print_type_name64(
938 int type_name)
939 {
940 const char *name = ipc_type_name64(type_name, TRUE);
941 if (name) {
942 kprintf("%s", name);
943 } else {
944 kprintf("type%d", type_name);
945 }
946 }
947
948 /*
949 * ipc_kmsg_print64 [ debug ]
950 */
951 void
ipc_kmsg_print64(ipc_kmsg_t kmsg,const char * str)952 ipc_kmsg_print64(
953 ipc_kmsg_t kmsg,
954 const char *str)
955 {
956 kprintf("%s kmsg=%p:\n", str, kmsg);
957 kprintf(" next=%p, prev=%p",
958 kmsg->ikm_link.next,
959 kmsg->ikm_link.prev);
960 kprintf("\n");
961 ipc_msg_print64(ikm_header(kmsg));
962 }
963
964 const char *
msgh_bit_decode64(mach_msg_bits_t bit)965 msgh_bit_decode64(
966 mach_msg_bits_t bit)
967 {
968 switch (bit) {
969 case MACH_MSGH_BITS_COMPLEX: return "complex";
970 case MACH_MSGH_BITS_CIRCULAR: return "circular";
971 default: return (char *) 0;
972 }
973 }
974
975 /*
976 * ipc_msg_print64 [ debug ]
977 */
978 void
ipc_msg_print64(mach_msg_header_t * msgh)979 ipc_msg_print64(
980 mach_msg_header_t *msgh)
981 {
982 mach_msg_bits_t mbits;
983 unsigned int bit, i;
984 const char *bit_name;
985 int needs_comma;
986
987 mbits = msgh->msgh_bits;
988 kprintf(" msgh_bits=0x%x: l=0x%x,r=0x%x\n",
989 mbits,
990 MACH_MSGH_BITS_LOCAL(msgh->msgh_bits),
991 MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
992
993 mbits = MACH_MSGH_BITS_OTHER(mbits) & MACH_MSGH_BITS_USED;
994 kprintf(" decoded bits: ");
995 needs_comma = 0;
996 for (i = 0, bit = 1; i < sizeof(mbits) * 8; ++i, bit <<= 1) {
997 if ((mbits & bit) == 0) {
998 continue;
999 }
1000 bit_name = msgh_bit_decode64((mach_msg_bits_t)bit);
1001 if (bit_name) {
1002 kprintf("%s%s", needs_comma ? "," : "", bit_name);
1003 } else {
1004 kprintf("%sunknown(0x%x),", needs_comma ? "," : "", bit);
1005 }
1006 ++needs_comma;
1007 }
1008 if (msgh->msgh_bits & ~MACH_MSGH_BITS_USED) {
1009 kprintf("%sunused=0x%x,", needs_comma ? "," : "",
1010 msgh->msgh_bits & ~MACH_MSGH_BITS_USED);
1011 }
1012 kprintf("\n");
1013
1014 needs_comma = 1;
1015 if (msgh->msgh_remote_port) {
1016 kprintf(" remote=%p(", msgh->msgh_remote_port);
1017 ipc_print_type_name64(MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
1018 kprintf(")");
1019 } else {
1020 kprintf(" remote=null");
1021 }
1022
1023 if (msgh->msgh_local_port) {
1024 kprintf("%slocal=%p(", needs_comma ? "," : "",
1025 msgh->msgh_local_port);
1026 ipc_print_type_name64(MACH_MSGH_BITS_LOCAL(msgh->msgh_bits));
1027 kprintf(")\n");
1028 } else {
1029 kprintf("local=null\n");
1030 }
1031
1032 kprintf(" msgh_id=%d, size=%d\n",
1033 msgh->msgh_id,
1034 msgh->msgh_size);
1035
1036 if (mbits & MACH_MSGH_BITS_COMPLEX) {
1037 ipc_msg_print_untyped64((mach_msg_body_t *) (msgh + 1));
1038 }
1039
1040 ipc_msg_body_print64((void *)(msgh + 1), msgh->msgh_size);
1041 }
1042
1043
1044 const char *
mm_copy_options_string64(mach_msg_copy_options_t option)1045 mm_copy_options_string64(
1046 mach_msg_copy_options_t option)
1047 {
1048 const char *name;
1049
1050 switch (option) {
1051 case MACH_MSG_PHYSICAL_COPY:
1052 name = "PHYSICAL";
1053 break;
1054 case MACH_MSG_VIRTUAL_COPY:
1055 name = "VIRTUAL";
1056 break;
1057 case MACH_MSG_OVERWRITE:
1058 name = "OVERWRITE(DEPRECATED)";
1059 break;
1060 case MACH_MSG_ALLOCATE:
1061 name = "ALLOCATE";
1062 break;
1063 case MACH_MSG_KALLOC_COPY_T:
1064 name = "KALLOC_COPY_T";
1065 break;
1066 default:
1067 name = "unknown";
1068 break;
1069 }
1070 return name;
1071 }
1072
1073 void
ipc_msg_print_untyped64(mach_msg_body_t * body)1074 ipc_msg_print_untyped64(
1075 mach_msg_body_t *body)
1076 {
1077 mach_msg_descriptor_t *saddr, *send;
1078 mach_msg_descriptor_type_t type;
1079
1080 kprintf(" %d descriptors: \n", body->msgh_descriptor_count);
1081
1082 saddr = (mach_msg_descriptor_t *) (body + 1);
1083 send = saddr + body->msgh_descriptor_count;
1084
1085 for (; saddr < send; saddr++) {
1086 type = saddr->type.type;
1087
1088 switch (type) {
1089 case MACH_MSG_PORT_DESCRIPTOR: {
1090 mach_msg_port_descriptor_t *dsc;
1091
1092 dsc = &saddr->port;
1093 kprintf(" PORT name = %p disp = ", dsc->name);
1094 ipc_print_type_name64(dsc->disposition);
1095 kprintf("\n");
1096 break;
1097 }
1098 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
1099 case MACH_MSG_OOL_DESCRIPTOR: {
1100 mach_msg_ool_descriptor_t *dsc;
1101
1102 dsc = (mach_msg_ool_descriptor_t *) &saddr->out_of_line;
1103 kprintf(" OOL%s addr = %p size = 0x%x copy = %s %s\n",
1104 type == MACH_MSG_OOL_DESCRIPTOR ? "" : " VOLATILE",
1105 dsc->address, dsc->size,
1106 mm_copy_options_string64(dsc->copy),
1107 dsc->deallocate ? "DEALLOC" : "");
1108 break;
1109 }
1110 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
1111 mach_msg_ool_ports_descriptor_t *dsc;
1112
1113 dsc = (mach_msg_ool_ports_descriptor_t *) &saddr->ool_ports;
1114
1115 kprintf(" OOL_PORTS addr = %p count = 0x%x ",
1116 dsc->address, dsc->count);
1117 kprintf("disp = ");
1118 ipc_print_type_name64(dsc->disposition);
1119 kprintf(" copy = %s %s\n",
1120 mm_copy_options_string64(dsc->copy),
1121 dsc->deallocate ? "DEALLOC" : "");
1122 break;
1123 }
1124 case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
1125 mach_msg_guarded_port_descriptor_t *dsc;
1126
1127 dsc = (mach_msg_guarded_port_descriptor_t *)&saddr->guarded_port;
1128 kprintf(" GUARDED_PORT name = %p flags = 0x%x disp = ", dsc->name, dsc->flags);
1129 ipc_print_type_name64(dsc->disposition);
1130 kprintf("\n");
1131 break;
1132 }
1133 default: {
1134 kprintf(" UNKNOWN DESCRIPTOR 0x%x\n", type);
1135 break;
1136 }
1137 }
1138 }
1139 }
1140
1141 #define DEBUG_IPC_KMSG_PRINT(kmsg, string) \
1142 __unreachable_ok_push \
1143 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) { \
1144 ipc_kmsg_print64(kmsg, string); \
1145 } \
1146 __unreachable_ok_pop
1147
1148 #define DEBUG_IPC_MSG_BODY_PRINT(body, size) \
1149 __unreachable_ok_push \
1150 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) { \
1151 ipc_msg_body_print64(body,size);\
1152 } \
1153 __unreachable_ok_pop
1154 #else /* !DEBUG_MSGS_K64 */
1155 #define DEBUG_IPC_KMSG_PRINT(kmsg, string)
1156 #define DEBUG_IPC_MSG_BODY_PRINT(body, size)
1157 #endif /* !DEBUG_MSGS_K64 */
1158
1159 extern vm_map_t ipc_kernel_copy_map;
1160 extern vm_size_t ipc_kmsg_max_space;
1161 extern const vm_size_t ipc_kmsg_max_vm_space;
1162 extern const vm_size_t msg_ool_size_small;
1163
1164 #define MSG_OOL_SIZE_SMALL msg_ool_size_small
1165
1166 #define KMSG_TRACE_FLAG_TRACED 0x000001
1167 #define KMSG_TRACE_FLAG_COMPLEX 0x000002
1168 #define KMSG_TRACE_FLAG_OOLMEM 0x000004
1169 #define KMSG_TRACE_FLAG_VCPY 0x000008
1170 #define KMSG_TRACE_FLAG_PCPY 0x000010
1171 #define KMSG_TRACE_FLAG_SND64 0x000020
1172 #define KMSG_TRACE_FLAG_RAISEIMP 0x000040
1173 #define KMSG_TRACE_FLAG_APP_SRC 0x000080
1174 #define KMSG_TRACE_FLAG_APP_DST 0x000100
1175 #define KMSG_TRACE_FLAG_DAEMON_SRC 0x000200
1176 #define KMSG_TRACE_FLAG_DAEMON_DST 0x000400
1177 #define KMSG_TRACE_FLAG_DST_NDFLTQ 0x000800
1178 #define KMSG_TRACE_FLAG_SRC_NDFLTQ 0x001000
1179 #define KMSG_TRACE_FLAG_DST_SONCE 0x002000
1180 #define KMSG_TRACE_FLAG_SRC_SONCE 0x004000
1181 #define KMSG_TRACE_FLAG_CHECKIN 0x008000
1182 #define KMSG_TRACE_FLAG_ONEWAY 0x010000
1183 #define KMSG_TRACE_FLAG_IOKIT 0x020000
1184 #define KMSG_TRACE_FLAG_SNDRCV 0x040000
1185 #define KMSG_TRACE_FLAG_DSTQFULL 0x080000
1186 #define KMSG_TRACE_FLAG_VOUCHER 0x100000
1187 #define KMSG_TRACE_FLAG_TIMER 0x200000
1188 #define KMSG_TRACE_FLAG_SEMA 0x400000
1189 #define KMSG_TRACE_FLAG_DTMPOWNER 0x800000
1190 #define KMSG_TRACE_FLAG_GUARDED_DESC 0x1000000
1191
1192 #define KMSG_TRACE_FLAGS_MASK 0x1ffffff
1193 #define KMSG_TRACE_FLAGS_SHIFT 8
1194
1195 #define KMSG_TRACE_PORTS_MASK 0xff
1196 #define KMSG_TRACE_PORTS_SHIFT 0
1197
1198 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
1199 #include <stdint.h>
1200
1201 void
ipc_kmsg_trace_send(ipc_kmsg_t kmsg,mach_msg_option_t option)1202 ipc_kmsg_trace_send(ipc_kmsg_t kmsg,
1203 mach_msg_option_t option)
1204 {
1205 task_t send_task = TASK_NULL;
1206 ipc_port_t dst_port, src_port;
1207 boolean_t is_task_64bit;
1208 mach_msg_header_t *msg;
1209 mach_msg_trailer_t *trailer;
1210
1211 int kotype = 0;
1212 uint32_t msg_size = 0;
1213 uint64_t msg_flags = KMSG_TRACE_FLAG_TRACED;
1214 uint32_t num_ports = 0;
1215 uint32_t send_pid, dst_pid;
1216
1217 /*
1218 * check to see not only if ktracing is enabled, but if we will
1219 * _actually_ emit the KMSG_INFO tracepoint. This saves us a
1220 * significant amount of processing (and a port lock hold) in
1221 * the non-tracing case.
1222 */
1223 if (__probable((kdebug_enable & KDEBUG_TRACE) == 0)) {
1224 return;
1225 }
1226 if (!kdebug_debugid_enabled(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO))) {
1227 return;
1228 }
1229
1230 msg = ikm_header(kmsg);
1231
1232 dst_port = msg->msgh_remote_port;
1233 if (!IPC_PORT_VALID(dst_port)) {
1234 return;
1235 }
1236
1237 /*
1238 * Message properties / options
1239 */
1240 if ((option & (MACH_SEND_MSG | MACH_RCV_MSG)) == (MACH_SEND_MSG | MACH_RCV_MSG)) {
1241 msg_flags |= KMSG_TRACE_FLAG_SNDRCV;
1242 }
1243
1244 if (msg->msgh_id >= is_iokit_subsystem.start &&
1245 msg->msgh_id < is_iokit_subsystem.end + 100) {
1246 msg_flags |= KMSG_TRACE_FLAG_IOKIT;
1247 }
1248 /* magic XPC checkin message id (XPC_MESSAGE_ID_CHECKIN) from libxpc */
1249 else if (msg->msgh_id == 0x77303074u /* w00t */) {
1250 msg_flags |= KMSG_TRACE_FLAG_CHECKIN;
1251 }
1252
1253 if (msg->msgh_bits & MACH_MSGH_BITS_RAISEIMP) {
1254 msg_flags |= KMSG_TRACE_FLAG_RAISEIMP;
1255 }
1256
1257 if (unsafe_convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg))) {
1258 msg_flags |= KMSG_TRACE_FLAG_VOUCHER;
1259 }
1260
1261 /*
1262 * Sending task / port
1263 */
1264 send_task = current_task();
1265 send_pid = task_pid(send_task);
1266
1267 if (send_pid != 0) {
1268 if (task_is_daemon(send_task)) {
1269 msg_flags |= KMSG_TRACE_FLAG_DAEMON_SRC;
1270 } else if (task_is_app(send_task)) {
1271 msg_flags |= KMSG_TRACE_FLAG_APP_SRC;
1272 }
1273 }
1274
1275 is_task_64bit = (send_task->map->max_offset > VM_MAX_ADDRESS);
1276 if (is_task_64bit) {
1277 msg_flags |= KMSG_TRACE_FLAG_SND64;
1278 }
1279
1280 src_port = msg->msgh_local_port;
1281 if (src_port) {
1282 if (src_port->ip_messages.imq_qlimit != MACH_PORT_QLIMIT_DEFAULT) {
1283 msg_flags |= KMSG_TRACE_FLAG_SRC_NDFLTQ;
1284 }
1285 switch (MACH_MSGH_BITS_LOCAL(msg->msgh_bits)) {
1286 case MACH_MSG_TYPE_MOVE_SEND_ONCE:
1287 msg_flags |= KMSG_TRACE_FLAG_SRC_SONCE;
1288 break;
1289 default:
1290 break;
1291 }
1292 } else {
1293 msg_flags |= KMSG_TRACE_FLAG_ONEWAY;
1294 }
1295
1296
1297 /*
1298 * Destination task / port
1299 */
1300 ip_mq_lock(dst_port);
1301 if (!ip_active(dst_port)) {
1302 /* dst port is being torn down */
1303 dst_pid = (uint32_t)0xfffffff0;
1304 } else if (dst_port->ip_tempowner) {
1305 msg_flags |= KMSG_TRACE_FLAG_DTMPOWNER;
1306 if (IIT_NULL != ip_get_imp_task(dst_port)) {
1307 dst_pid = task_pid(dst_port->ip_imp_task->iit_task);
1308 } else {
1309 dst_pid = (uint32_t)0xfffffff1;
1310 }
1311 } else if (!ip_in_a_space(dst_port)) {
1312 /* dst_port is otherwise in-transit */
1313 dst_pid = (uint32_t)0xfffffff2;
1314 } else {
1315 if (ip_in_space(dst_port, ipc_space_kernel)) {
1316 dst_pid = 0;
1317 } else {
1318 ipc_space_t dst_space;
1319 dst_space = ip_get_receiver(dst_port);
1320 if (dst_space && is_active(dst_space)) {
1321 dst_pid = task_pid(dst_space->is_task);
1322 if (task_is_daemon(dst_space->is_task)) {
1323 msg_flags |= KMSG_TRACE_FLAG_DAEMON_DST;
1324 } else if (task_is_app(dst_space->is_task)) {
1325 msg_flags |= KMSG_TRACE_FLAG_APP_DST;
1326 }
1327 } else {
1328 /* receiving task is being torn down */
1329 dst_pid = (uint32_t)0xfffffff3;
1330 }
1331 }
1332 }
1333
1334 if (dst_port->ip_messages.imq_qlimit != MACH_PORT_QLIMIT_DEFAULT) {
1335 msg_flags |= KMSG_TRACE_FLAG_DST_NDFLTQ;
1336 }
1337 if (imq_full(&dst_port->ip_messages)) {
1338 msg_flags |= KMSG_TRACE_FLAG_DSTQFULL;
1339 }
1340
1341 kotype = ip_kotype(dst_port);
1342
1343 ip_mq_unlock(dst_port);
1344
1345 switch (kotype) {
1346 case IKOT_SEMAPHORE:
1347 msg_flags |= KMSG_TRACE_FLAG_SEMA;
1348 break;
1349 case IKOT_TIMER:
1350 case IKOT_CLOCK:
1351 msg_flags |= KMSG_TRACE_FLAG_TIMER;
1352 break;
1353 case IKOT_MAIN_DEVICE:
1354 case IKOT_IOKIT_CONNECT:
1355 case IKOT_IOKIT_OBJECT:
1356 case IKOT_IOKIT_IDENT:
1357 case IKOT_UEXT_OBJECT:
1358 msg_flags |= KMSG_TRACE_FLAG_IOKIT;
1359 break;
1360 default:
1361 break;
1362 }
1363
1364 switch (MACH_MSGH_BITS_REMOTE(msg->msgh_bits)) {
1365 case MACH_MSG_TYPE_PORT_SEND_ONCE:
1366 msg_flags |= KMSG_TRACE_FLAG_DST_SONCE;
1367 break;
1368 default:
1369 break;
1370 }
1371
1372
1373 /*
1374 * Message size / content
1375 */
1376 msg_size = msg->msgh_size - sizeof(mach_msg_header_t);
1377
1378 if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
1379 mach_msg_body_t *msg_body;
1380 mach_msg_descriptor_t *kern_dsc;
1381 mach_msg_size_t dsc_count;
1382
1383 msg_flags |= KMSG_TRACE_FLAG_COMPLEX;
1384
1385 msg_body = (mach_msg_body_t *)(msg + 1);
1386 dsc_count = msg_body->msgh_descriptor_count;
1387 kern_dsc = (mach_msg_descriptor_t *)(msg_body + 1);
1388
1389 for (mach_msg_size_t i = 0; i < dsc_count; i++) {
1390 switch (kern_dsc[i].type.type) {
1391 case MACH_MSG_PORT_DESCRIPTOR:
1392 num_ports++;
1393 break;
1394 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
1395 case MACH_MSG_OOL_DESCRIPTOR: {
1396 mach_msg_ool_descriptor_t *dsc;
1397 dsc = (mach_msg_ool_descriptor_t *)&kern_dsc[i];
1398 msg_flags |= KMSG_TRACE_FLAG_OOLMEM;
1399 msg_size += dsc->size;
1400 if (dsc->size > MSG_OOL_SIZE_SMALL &&
1401 (dsc->copy == MACH_MSG_PHYSICAL_COPY) &&
1402 !dsc->deallocate) {
1403 msg_flags |= KMSG_TRACE_FLAG_PCPY;
1404 } else if (dsc->size <= MSG_OOL_SIZE_SMALL) {
1405 msg_flags |= KMSG_TRACE_FLAG_PCPY;
1406 } else {
1407 msg_flags |= KMSG_TRACE_FLAG_VCPY;
1408 }
1409 } break;
1410 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
1411 mach_msg_ool_ports_descriptor_t *dsc;
1412 dsc = (mach_msg_ool_ports_descriptor_t *)&kern_dsc[i];
1413 num_ports += dsc->count;
1414 } break;
1415 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
1416 num_ports++;
1417 msg_flags |= KMSG_TRACE_FLAG_GUARDED_DESC;
1418 break;
1419 default:
1420 break;
1421 }
1422 msg_size -= ikm_user_desc_size(kern_dsc[i].type.type, is_task_64bit);
1423 }
1424 }
1425
1426 /*
1427 * Trailer contents
1428 */
1429 trailer = (mach_msg_trailer_t *)ipc_kmsg_get_trailer(kmsg, false);
1430 if (trailer->msgh_trailer_size <= sizeof(mach_msg_security_trailer_t)) {
1431 mach_msg_security_trailer_t *strailer;
1432 strailer = (mach_msg_security_trailer_t *)trailer;
1433 /*
1434 * verify the sender PID: replies from the kernel often look
1435 * like self-talk because the sending port is not reset.
1436 */
1437 if (memcmp(&strailer->msgh_sender,
1438 &KERNEL_SECURITY_TOKEN,
1439 sizeof(KERNEL_SECURITY_TOKEN)) == 0) {
1440 send_pid = 0;
1441 msg_flags &= ~(KMSG_TRACE_FLAG_APP_SRC | KMSG_TRACE_FLAG_DAEMON_SRC);
1442 }
1443 }
1444
1445 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END,
1446 (uintptr_t)send_pid,
1447 (uintptr_t)dst_pid,
1448 (uintptr_t)msg_size,
1449 (uintptr_t)(
1450 ((msg_flags & KMSG_TRACE_FLAGS_MASK) << KMSG_TRACE_FLAGS_SHIFT) |
1451 ((num_ports & KMSG_TRACE_PORTS_MASK) << KMSG_TRACE_PORTS_SHIFT)
1452 )
1453 );
1454 }
1455 #endif
1456
1457 /* zone for cached ipc_kmsg_t structures */
1458 ZONE_DEFINE(ipc_kmsg_zone, "ipc kmsgs", IKM_SAVED_KMSG_SIZE,
1459 ZC_CACHING | ZC_ZFREE_CLEARMEM);
1460 static TUNABLE(bool, enforce_strict_reply, "ipc_strict_reply", false);
1461
1462 /*
1463 * Forward declarations
1464 */
1465
1466 static void ipc_kmsg_clean(
1467 ipc_kmsg_t kmsg);
1468
1469 static void
1470 ipc_kmsg_link_reply_context_locked(
1471 ipc_port_t reply_port,
1472 ipc_port_t voucher_port);
1473
1474 static kern_return_t
1475 ipc_kmsg_validate_reply_port_locked(
1476 ipc_port_t reply_port,
1477 mach_msg_option_t options);
1478
1479 static mach_msg_return_t
1480 ipc_kmsg_validate_reply_context_locked(
1481 mach_msg_option_t option,
1482 ipc_port_t dest_port,
1483 ipc_voucher_t voucher,
1484 mach_port_name_t voucher_name);
1485
1486 /* we can't include the BSD <sys/persona.h> header here... */
1487 #ifndef PERSONA_ID_NONE
1488 #define PERSONA_ID_NONE ((uint32_t)-1)
1489 #endif
1490
1491 static inline void *
ikm_inline_data(ipc_kmsg_t kmsg)1492 ikm_inline_data(
1493 ipc_kmsg_t kmsg)
1494 {
1495 return (void *)(kmsg + 1);
1496 }
1497
1498 /* Whether header, body, content and trailer occupy contiguous memory space */
1499 static inline bool
ikm_is_linear(ipc_kmsg_t kmsg)1500 ikm_is_linear(ipc_kmsg_t kmsg)
1501 {
1502 return kmsg->ikm_type == IKM_TYPE_ALL_INLINED ||
1503 kmsg->ikm_type == IKM_TYPE_KDATA_OOL;
1504 }
1505
1506 static inline bool
ikm_header_inlined(ipc_kmsg_t kmsg)1507 ikm_header_inlined(ipc_kmsg_t kmsg)
1508 {
1509 /* ikm_type must not be reordered */
1510 static_assert(IKM_TYPE_UDATA_OOL == 1);
1511 static_assert(IKM_TYPE_ALL_INLINED == 0);
1512 return kmsg->ikm_type <= IKM_TYPE_UDATA_OOL;
1513 }
1514
1515 #if (DEVELOPMENT || DEBUG)
1516 static vm_offset_t
ikm_kdata_end(ipc_kmsg_t kmsg)1517 ikm_kdata_end(ipc_kmsg_t kmsg)
1518 {
1519 if (ikm_header_inlined(kmsg)) {
1520 /* round up to total kmsg buffer size */
1521 return (vm_offset_t)kmsg + IKM_SAVED_KMSG_SIZE;
1522 } else if (ikm_is_linear(kmsg)) {
1523 /* round up to total kmsg buffer size */
1524 ipc_kmsg_vector_t *vec = ikm_inline_data(kmsg);
1525 return (vm_offset_t)vec->kmsgv_data + vec->kmsgv_size;
1526 } else {
1527 assert(kmsg->ikm_type == IKM_TYPE_ALL_OOL);
1528 ipc_kmsg_vector_t *vec = ikm_inline_data(kmsg);
1529 return (vm_offset_t)vec->kmsgv_data + sizeof(mach_msg_base_t) +
1530 vec->kmsgv_size * KERNEL_DESC_SIZE;
1531 }
1532 }
1533 static vm_offset_t
ikm_udata_end(ipc_kmsg_t kmsg)1534 ikm_udata_end(ipc_kmsg_t kmsg)
1535 {
1536 assert(kmsg->ikm_type != IKM_TYPE_ALL_INLINED);
1537 assert(kmsg->ikm_udata != NULL);
1538
1539 return (vm_offset_t)kmsg->ikm_udata + kmsg->ikm_udata_size;
1540 }
1541 #endif
1542
1543 /*
1544 * Returns message header address.
1545 *
1546 * /!\ WARNING /!\
1547 * Need to shift the return value after call to ipc_kmsg_convert_header_to_user().
1548 */
1549 inline mach_msg_header_t *
ikm_header(ipc_kmsg_t kmsg)1550 ikm_header(
1551 ipc_kmsg_t kmsg)
1552 {
1553 return ikm_header_inlined(kmsg) ? (mach_msg_header_t *)ikm_inline_data(kmsg) :
1554 (mach_msg_header_t *)(((ipc_kmsg_vector_t *)ikm_inline_data(kmsg))->kmsgv_data);
1555 }
1556
1557 static inline mach_msg_aux_header_t *
ikm_aux_header(ipc_kmsg_t kmsg)1558 ikm_aux_header(
1559 ipc_kmsg_t kmsg)
1560 {
1561 if (!kmsg->ikm_aux_size) {
1562 return NULL;
1563 }
1564
1565 assert(kmsg->ikm_aux_size >= sizeof(mach_msg_aux_header_t));
1566
1567 if (kmsg->ikm_type == IKM_TYPE_ALL_INLINED) {
1568 return (mach_msg_aux_header_t *)((vm_offset_t)kmsg + IKM_SAVED_KMSG_SIZE -
1569 kmsg->ikm_aux_size);
1570 } else {
1571 assert(kmsg->ikm_type != IKM_TYPE_KDATA_OOL);
1572 return (mach_msg_aux_header_t *)((vm_offset_t)kmsg->ikm_udata +
1573 kmsg->ikm_udata_size - kmsg->ikm_aux_size);
1574 }
1575 }
1576
1577 /* Return real size of kmsg aux data */
1578 inline mach_msg_size_t
ipc_kmsg_aux_data_size(ipc_kmsg_t kmsg)1579 ipc_kmsg_aux_data_size(
1580 ipc_kmsg_t kmsg)
1581 {
1582 mach_msg_aux_header_t *aux;
1583
1584 aux = ikm_aux_header(kmsg);
1585 if (aux == NULL) {
1586 return 0;
1587 }
1588
1589 #if (DEVELOPMENT || DEBUG)
1590 if (kmsg->ikm_type == IKM_TYPE_ALL_INLINED) {
1591 assert((vm_offset_t)aux + aux->msgdh_size <= (vm_offset_t)kmsg + IKM_SAVED_KMSG_SIZE);
1592 } else {
1593 assert((vm_offset_t)aux + aux->msgdh_size <= ikm_udata_end(kmsg));
1594 }
1595
1596 assert3u(aux->msgdh_size, <=, kmsg->ikm_aux_size);
1597 assert3u(aux->msgdh_size, >=, sizeof(mach_msg_aux_header_t));
1598 #endif
1599
1600 return aux->msgdh_size;
1601 }
1602
1603 void
ipc_kmsg_set_aux_data_header(ipc_kmsg_t kmsg,mach_msg_aux_header_t * new_hdr)1604 ipc_kmsg_set_aux_data_header(
1605 ipc_kmsg_t kmsg,
1606 mach_msg_aux_header_t *new_hdr)
1607 {
1608 mach_msg_aux_header_t *cur_hdr;
1609
1610 assert3u(new_hdr->msgdh_size, >=, sizeof(mach_msg_aux_header_t));
1611
1612 cur_hdr = ikm_aux_header(kmsg);
1613 if (cur_hdr == NULL) {
1614 return;
1615 }
1616
1617 /*
1618 * New header size must not exceed the space allocated for aux.
1619 */
1620 assert3u(kmsg->ikm_aux_size, >=, new_hdr->msgdh_size);
1621 assert3u(kmsg->ikm_aux_size, >=, sizeof(mach_msg_aux_header_t));
1622
1623 *cur_hdr = *new_hdr;
1624 }
1625
1626 /*
1627 * Routine: ipc_kmsg_alloc
1628 * Purpose:
1629 * Allocate a kernel message structure. If the
1630 * message is scalar and all the data resides inline, that is best.
1631 * Otherwise, allocate out of line buffers to fit the message and
1632 * the optional auxiliary data.
1633 *
1634 * Conditions:
1635 * Nothing locked.
1636 *
1637 * kmsg_size doesn't take the trailer or descriptor
1638 * inflation into account, but already accounts for the mach
1639 * message header expansion.
1640 */
1641 ipc_kmsg_t
ipc_kmsg_alloc(mach_msg_size_t kmsg_size,mach_msg_size_t aux_size,mach_msg_size_t user_descs,ipc_kmsg_alloc_flags_t flags)1642 ipc_kmsg_alloc(
1643 mach_msg_size_t kmsg_size,
1644 mach_msg_size_t aux_size,
1645 mach_msg_size_t user_descs,
1646 ipc_kmsg_alloc_flags_t flags)
1647 {
1648 mach_msg_size_t max_kmsg_size, max_delta, max_kdata_size,
1649 min_kdata_size, max_udata_size, max_kmsg_and_aux_size;
1650 ipc_kmsg_t kmsg;
1651
1652 void *msg_data = NULL, *user_data = NULL;
1653 zalloc_flags_t alloc_flags = Z_WAITOK;
1654 ipc_kmsg_type_t kmsg_type;
1655 ipc_kmsg_vector_t *vec;
1656
1657 /*
1658 * In kernel descriptors, are of the same size (KERNEL_DESC_SIZE),
1659 * but in userspace, depending on 64-bitness, descriptors might be
1660 * smaller.
1661 *
1662 * When handling a userspace message however, we know how many
1663 * descriptors have been declared, and we pad for the maximum expansion.
1664 *
1665 * During descriptor expansion, message header stays at the same place
1666 * while everything after it gets shifted to higher address.
1667 */
1668 if (flags & IPC_KMSG_ALLOC_KERNEL) {
1669 assert(user_descs == 0);
1670 max_delta = 0;
1671 } else if (os_mul_overflow(user_descs, USER_DESC_MAX_DELTA, &max_delta)) {
1672 return IKM_NULL;
1673 }
1674
1675 if (os_add3_overflow(kmsg_size, MAX_TRAILER_SIZE, max_delta, &max_kmsg_size)) {
1676 return IKM_NULL;
1677 }
1678 if (os_add_overflow(max_kmsg_size, aux_size, &max_kmsg_and_aux_size)) {
1679 return IKM_NULL;
1680 }
1681
1682
1683 if (flags & IPC_KMSG_ALLOC_ZERO) {
1684 alloc_flags |= Z_ZERO;
1685 }
1686 if (flags & IPC_KMSG_ALLOC_NOFAIL) {
1687 alloc_flags |= Z_NOFAIL;
1688 }
1689 if (flags & IPC_KMSG_ALLOC_KERNEL) {
1690 flags |= IPC_KMSG_ALLOC_LINEAR;
1691 }
1692
1693 /* First, determine the layout of the kmsg to allocate */
1694 if (max_kmsg_and_aux_size <= IKM_SAVED_MSG_SIZE) {
1695 kmsg_type = IKM_TYPE_ALL_INLINED;
1696 max_udata_size = 0;
1697 max_kdata_size = 0;
1698 } else if (flags & IPC_KMSG_ALLOC_SAVED) {
1699 panic("size too large for the fast kmsg zone (%d)", kmsg_size);
1700 } else if (flags & IPC_KMSG_ALLOC_LINEAR) {
1701 kmsg_type = IKM_TYPE_KDATA_OOL;
1702 /*
1703 * Caller sets MACH64_SEND_KOBJECT_CALL or MACH64_SEND_ANY, or that
1704 * the call originates from kernel, or it's a mach_msg() call.
1705 * In any case, message does not carry aux data.
1706 * We have validated mach_msg2() call options in mach_msg2_trap().
1707 */
1708 if (aux_size != 0) {
1709 panic("non-zero aux size for kmsg type IKM_TYPE_KDATA_OOL.");
1710 }
1711 max_udata_size = aux_size;
1712 max_kdata_size = max_kmsg_size;
1713 } else {
1714 /*
1715 * If message can be splitted from the middle, IOW does not need to
1716 * occupy contiguous memory space, sequester (header + descriptors)
1717 * from (content + trailer + aux) for memory security.
1718 */
1719 assert(max_kmsg_and_aux_size > IKM_SAVED_MSG_SIZE);
1720
1721 /*
1722 * max_kdata_size: Maximum combined size of header plus (optional) descriptors.
1723 * This is _base_ size + descriptor count * kernel descriptor size.
1724 */
1725 if (os_mul_and_add_overflow(user_descs, KERNEL_DESC_SIZE,
1726 sizeof(mach_msg_base_t), &max_kdata_size)) {
1727 return IKM_NULL;
1728 }
1729
1730 /*
1731 * min_kdata_size: Minimum combined size of header plus (optional) descriptors.
1732 * This is _header_ size + descriptor count * minimal descriptor size.
1733 */
1734 if (os_mul_and_add_overflow(user_descs, MACH_MSG_DESC_MIN_SIZE,
1735 sizeof(mach_msg_header_t), &min_kdata_size)) {
1736 return IKM_NULL;
1737 }
1738
1739 /*
1740 * max_udata_size: Maximum combined size of message content, trailer and aux.
1741 * This is total kmsg and aux size (already accounts for max trailer size) minus
1742 * _minimum_ (header + descs) size.
1743 */
1744 if (os_sub_overflow(max_kmsg_and_aux_size, min_kdata_size, &max_udata_size)) {
1745 return IKM_NULL;
1746 }
1747
1748 if (max_kdata_size <= IKM_SAVED_MSG_SIZE) {
1749 max_kdata_size = 0; /* no need to allocate kdata */
1750 kmsg_type = IKM_TYPE_UDATA_OOL;
1751 } else {
1752 kmsg_type = IKM_TYPE_ALL_OOL;
1753 }
1754 }
1755
1756 /* Then, allocate memory for both udata and kdata if needed, as well as kmsg */
1757 if (max_udata_size > 0) {
1758 user_data = kalloc_data(max_udata_size, alloc_flags);
1759 if (user_data == NULL) {
1760 return IKM_NULL;
1761 }
1762 }
1763
1764 if (max_kdata_size > 0) {
1765 if (kmsg_type == IKM_TYPE_ALL_OOL) {
1766 msg_data = kalloc_type(mach_msg_base_t, mach_msg_descriptor_t, user_descs, alloc_flags);
1767 } else {
1768 assert(kmsg_type == IKM_TYPE_KDATA_OOL);
1769 msg_data = kalloc_data(max_kdata_size, alloc_flags);
1770 }
1771
1772 if (__improbable(msg_data == NULL)) {
1773 kfree_data(user_data, max_udata_size);
1774 return IKM_NULL;
1775 }
1776 }
1777
1778 kmsg = zalloc_flags(ipc_kmsg_zone, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1779 kmsg->ikm_type = kmsg_type;
1780 kmsg->ikm_aux_size = aux_size;
1781
1782 /* Finally, set up pointers properly */
1783 if (user_data) {
1784 assert(kmsg_type != IKM_TYPE_ALL_INLINED);
1785 kmsg->ikm_udata = user_data;
1786 kmsg->ikm_udata_size = max_udata_size; /* buffer size */
1787 }
1788 if (msg_data) {
1789 assert(kmsg_type == IKM_TYPE_ALL_OOL || kmsg_type == IKM_TYPE_KDATA_OOL);
1790 vec = (ipc_kmsg_vector_t *)ikm_inline_data(kmsg);
1791 vec->kmsgv_data = msg_data;
1792 vec->kmsgv_size = (kmsg_type == IKM_TYPE_ALL_OOL) ?
1793 user_descs : /* save descriptor count on kmsgv_size */
1794 max_kdata_size; /* buffer size */
1795 }
1796
1797 /* inline kmsg space at least can fit a vector */
1798 static_assert(IKM_SAVED_MSG_SIZE > sizeof(ipc_kmsg_vector_t));
1799
1800 return kmsg;
1801 }
1802
1803 /* re-export for IOKit's c++ */
1804 extern ipc_kmsg_t ipc_kmsg_alloc_uext_reply(mach_msg_size_t);
1805
1806 ipc_kmsg_t
ipc_kmsg_alloc_uext_reply(mach_msg_size_t size)1807 ipc_kmsg_alloc_uext_reply(
1808 mach_msg_size_t size)
1809 {
1810 return ipc_kmsg_alloc(size, 0, 0,
1811 IPC_KMSG_ALLOC_KERNEL | IPC_KMSG_ALLOC_ZERO | IPC_KMSG_ALLOC_NOFAIL);
1812 }
1813
1814
1815 /*
1816 * Routine: ipc_kmsg_free
1817 * Purpose:
1818 * Free a kernel message (and udata) buffer. If the kmg is preallocated
1819 * to a port, just "put it back (marked unused)." We have to
1820 * do this with the port locked. The port may have its hold
1821 * on our message released. In that case, we have to just
1822 * revert the message to a traditional one and free it normally.
1823 * Conditions:
1824 * Nothing locked.
1825 */
1826 void
ipc_kmsg_free(ipc_kmsg_t kmsg)1827 ipc_kmsg_free(
1828 ipc_kmsg_t kmsg)
1829 {
1830 mach_msg_size_t msg_buf_size = 0, udata_buf_size = 0, dsc_count = 0;
1831 void *msg_buf = NULL, *udata_buf = NULL;
1832 ipc_kmsg_vector_t *vec = NULL;
1833 ipc_port_t inuse_port = IP_NULL;
1834 mach_msg_header_t *hdr;
1835
1836 assert(!IP_VALID(ipc_kmsg_get_voucher_port(kmsg)));
1837
1838 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_FREE) | DBG_FUNC_NONE,
1839 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
1840 0, 0, 0, 0);
1841
1842 switch (kmsg->ikm_type) {
1843 case IKM_TYPE_ALL_INLINED:
1844 case IKM_TYPE_UDATA_OOL:
1845 msg_buf = ikm_inline_data(kmsg);
1846 msg_buf_size = IKM_SAVED_MSG_SIZE;
1847 break;
1848 case IKM_TYPE_KDATA_OOL:
1849 vec = ikm_inline_data(kmsg);
1850 msg_buf = vec->kmsgv_data;
1851 msg_buf_size = vec->kmsgv_size;
1852 break;
1853 case IKM_TYPE_ALL_OOL:
1854 vec = ikm_inline_data(kmsg);
1855 msg_buf = vec->kmsgv_data;
1856 dsc_count = vec->kmsgv_size;
1857 msg_buf_size = sizeof(mach_msg_base_t) + dsc_count * KERNEL_DESC_SIZE;
1858 break;
1859 default:
1860 panic("strange kmsg type");
1861 }
1862
1863 hdr = ikm_header(kmsg);
1864 if ((void *)hdr < msg_buf ||
1865 (void *)hdr >= (void *)((uintptr_t)msg_buf + msg_buf_size)) {
1866 panic("ipc_kmsg_free: invalid kmsg (%p) header", kmsg);
1867 }
1868
1869 if (kmsg->ikm_type != IKM_TYPE_ALL_INLINED) {
1870 udata_buf = kmsg->ikm_udata;
1871 udata_buf_size = kmsg->ikm_udata_size;
1872 }
1873
1874 switch (kmsg->ikm_type) {
1875 case IKM_TYPE_ALL_INLINED:
1876 /*
1877 * Check to see if the message is bound to the port.
1878 * If so, mark it not in use.
1879 */
1880 inuse_port = ikm_prealloc_inuse_port(kmsg);
1881 if (inuse_port != IP_NULL) {
1882 ip_mq_lock(inuse_port);
1883 ikm_prealloc_clear_inuse(kmsg);
1884 assert(inuse_port->ip_premsg == kmsg);
1885 assert(IP_PREALLOC(inuse_port));
1886 ip_mq_unlock(inuse_port);
1887 ip_release(inuse_port); /* May be last reference */
1888 return;
1889 }
1890 /* all data inlined, nothing to do */
1891 break;
1892 case IKM_TYPE_UDATA_OOL:
1893 assert(udata_buf != NULL);
1894 kfree_data(udata_buf, udata_buf_size);
1895 /* kdata is inlined, udata freed */
1896 break;
1897 case IKM_TYPE_KDATA_OOL:
1898 kfree_data(msg_buf, msg_buf_size);
1899 assert(udata_buf == NULL);
1900 assert(udata_buf_size == 0);
1901 /* kdata freed, no udata */
1902 break;
1903 case IKM_TYPE_ALL_OOL:
1904 kfree_type(mach_msg_base_t, mach_msg_descriptor_t, dsc_count, msg_buf);
1905 /* kdata freed */
1906 assert(udata_buf != NULL);
1907 kfree_data(udata_buf, udata_buf_size);
1908 /* udata freed */
1909 break;
1910 default:
1911 panic("strange kmsg type");
1912 }
1913
1914 zfree(ipc_kmsg_zone, kmsg);
1915 /* kmsg struct freed */
1916 }
1917
1918
1919 /*
1920 * Routine: ipc_kmsg_enqueue_qos
1921 * Purpose:
1922 * Enqueue a kmsg, propagating qos
1923 * overrides towards the head of the queue.
1924 *
1925 * Returns:
1926 * whether the head of the queue had
1927 * it's override-qos adjusted because
1928 * of this insertion.
1929 */
1930
1931 bool
ipc_kmsg_enqueue_qos(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg)1932 ipc_kmsg_enqueue_qos(
1933 ipc_kmsg_queue_t queue,
1934 ipc_kmsg_t kmsg)
1935 {
1936 mach_msg_qos_t qos_ovr = kmsg->ikm_qos_override;
1937 ipc_kmsg_t prev;
1938
1939 if (ipc_kmsg_enqueue(queue, kmsg)) {
1940 return true;
1941 }
1942
1943 /* apply QoS overrides towards the head */
1944 prev = ipc_kmsg_queue_element(kmsg->ikm_link.prev);
1945 while (prev != kmsg) {
1946 if (qos_ovr <= prev->ikm_qos_override) {
1947 return false;
1948 }
1949 prev->ikm_qos_override = qos_ovr;
1950 prev = ipc_kmsg_queue_element(prev->ikm_link.prev);
1951 }
1952
1953 return true;
1954 }
1955
1956 /*
1957 * Routine: ipc_kmsg_override_qos
1958 * Purpose:
1959 * Update the override for a given kmsg already
1960 * enqueued, propagating qos override adjustments
1961 * towards the head of the queue.
1962 *
1963 * Returns:
1964 * whether the head of the queue had
1965 * it's override-qos adjusted because
1966 * of this insertion.
1967 */
1968
1969 bool
ipc_kmsg_override_qos(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg,mach_msg_qos_t qos_ovr)1970 ipc_kmsg_override_qos(
1971 ipc_kmsg_queue_t queue,
1972 ipc_kmsg_t kmsg,
1973 mach_msg_qos_t qos_ovr)
1974 {
1975 ipc_kmsg_t first = ipc_kmsg_queue_first(queue);
1976 ipc_kmsg_t cur = kmsg;
1977
1978 /* apply QoS overrides towards the head */
1979 while (qos_ovr > cur->ikm_qos_override) {
1980 cur->ikm_qos_override = qos_ovr;
1981 if (cur == first) {
1982 return true;
1983 }
1984 cur = ipc_kmsg_queue_element(cur->ikm_link.prev);
1985 }
1986
1987 return false;
1988 }
1989
1990 /*
1991 * Routine: ipc_kmsg_destroy
1992 * Purpose:
1993 * Destroys a kernel message. Releases all rights,
1994 * references, and memory held by the message.
1995 * Frees the message.
1996 * Conditions:
1997 * No locks held.
1998 */
1999
2000 void
ipc_kmsg_destroy(ipc_kmsg_t kmsg,ipc_kmsg_destroy_flags_t flags)2001 ipc_kmsg_destroy(
2002 ipc_kmsg_t kmsg,
2003 ipc_kmsg_destroy_flags_t flags)
2004 {
2005 /* sign the msg if it has not been signed */
2006 boolean_t sign_msg = (flags & IPC_KMSG_DESTROY_NOT_SIGNED);
2007 mach_msg_header_t *hdr = ikm_header(kmsg);
2008
2009 if (flags & IPC_KMSG_DESTROY_SKIP_REMOTE) {
2010 hdr->msgh_remote_port = MACH_PORT_NULL;
2011 /* re-sign the msg since content changed */
2012 sign_msg = true;
2013 }
2014
2015 if (flags & IPC_KMSG_DESTROY_SKIP_LOCAL) {
2016 hdr->msgh_local_port = MACH_PORT_NULL;
2017 /* re-sign the msg since content changed */
2018 sign_msg = true;
2019 }
2020
2021 if (sign_msg) {
2022 ikm_sign(kmsg);
2023 }
2024
2025 /*
2026 * Destroying a message can cause more messages to be destroyed.
2027 * Curtail recursion by putting messages on the deferred
2028 * destruction queue. If this was the first message on the
2029 * queue, this instance must process the full queue.
2030 */
2031 if (ipc_kmsg_delayed_destroy(kmsg)) {
2032 ipc_kmsg_reap_delayed();
2033 }
2034 }
2035
2036 /*
2037 * Routine: ipc_kmsg_delayed_destroy
2038 * Purpose:
2039 * Enqueues a kernel message for deferred destruction.
2040 * Returns:
2041 * Boolean indicator that the caller is responsible to reap
2042 * deferred messages.
2043 */
2044
2045 bool
ipc_kmsg_delayed_destroy(ipc_kmsg_t kmsg)2046 ipc_kmsg_delayed_destroy(
2047 ipc_kmsg_t kmsg)
2048 {
2049 return ipc_kmsg_enqueue(¤t_thread()->ith_messages, kmsg);
2050 }
2051
2052 /*
2053 * Routine: ipc_kmsg_delayed_destroy_queue
2054 * Purpose:
2055 * Enqueues a queue of kernel messages for deferred destruction.
2056 * Returns:
2057 * Boolean indicator that the caller is responsible to reap
2058 * deferred messages.
2059 */
2060
2061 bool
ipc_kmsg_delayed_destroy_queue(ipc_kmsg_queue_t queue)2062 ipc_kmsg_delayed_destroy_queue(
2063 ipc_kmsg_queue_t queue)
2064 {
2065 return circle_queue_concat_tail(¤t_thread()->ith_messages, queue);
2066 }
2067
2068 /*
2069 * Routine: ipc_kmsg_reap_delayed
2070 * Purpose:
2071 * Destroys messages from the per-thread
2072 * deferred reaping queue.
2073 * Conditions:
2074 * No locks held. kmsgs on queue must be signed.
2075 */
2076
2077 void
ipc_kmsg_reap_delayed(void)2078 ipc_kmsg_reap_delayed(void)
2079 {
2080 ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
2081 ipc_kmsg_t kmsg;
2082
2083 /*
2084 * must leave kmsg in queue while cleaning it to assure
2085 * no nested calls recurse into here.
2086 */
2087 while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
2088 /*
2089 * Kmsgs queued for delayed destruction either come from
2090 * ipc_kmsg_destroy() or ipc_kmsg_delayed_destroy_queue(),
2091 * where we handover all kmsgs enqueued on port to destruction
2092 * queue in O(1). In either case, all kmsgs must have been
2093 * signed.
2094 *
2095 * For each unreceived msg, validate its signature before freeing.
2096 */
2097 ikm_validate_sig(kmsg);
2098
2099 ipc_kmsg_clean(kmsg);
2100 ipc_kmsg_rmqueue(queue, kmsg);
2101 ipc_kmsg_free(kmsg);
2102 }
2103 }
2104
2105 /*
2106 * Routine: ipc_kmsg_clean_body
2107 * Purpose:
2108 * Cleans the body of a kernel message.
2109 * Releases all rights, references, and memory.
2110 *
2111 * Conditions:
2112 * No locks held.
2113 */
2114 static void
ipc_kmsg_clean_body(__unused ipc_kmsg_t kmsg,mach_msg_type_number_t number,mach_msg_descriptor_t * saddr)2115 ipc_kmsg_clean_body(
2116 __unused ipc_kmsg_t kmsg,
2117 mach_msg_type_number_t number,
2118 mach_msg_descriptor_t *saddr)
2119 {
2120 mach_msg_type_number_t i;
2121
2122 if (number == 0) {
2123 return;
2124 }
2125
2126 for (i = 0; i < number; i++, saddr++) {
2127 switch (saddr->type.type) {
2128 case MACH_MSG_PORT_DESCRIPTOR: {
2129 mach_msg_port_descriptor_t *dsc;
2130
2131 dsc = &saddr->port;
2132
2133 /*
2134 * Destroy port rights carried in the message
2135 */
2136 if (!IP_VALID(dsc->name)) {
2137 continue;
2138 }
2139 ipc_object_destroy(ip_to_object(dsc->name), dsc->disposition);
2140 break;
2141 }
2142 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
2143 case MACH_MSG_OOL_DESCRIPTOR: {
2144 mach_msg_ool_descriptor_t *dsc;
2145
2146 dsc = (mach_msg_ool_descriptor_t *)&saddr->out_of_line;
2147
2148 /*
2149 * Destroy memory carried in the message
2150 */
2151 if (dsc->size == 0) {
2152 assert(dsc->address == (void *) 0);
2153 } else {
2154 vm_map_copy_discard((vm_map_copy_t) dsc->address);
2155 }
2156 break;
2157 }
2158 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
2159 ipc_object_t *objects;
2160 mach_msg_type_number_t j;
2161 mach_msg_ool_ports_descriptor_t *dsc;
2162
2163 dsc = (mach_msg_ool_ports_descriptor_t *)&saddr->ool_ports;
2164 objects = (ipc_object_t *) dsc->address;
2165
2166 if (dsc->count == 0) {
2167 break;
2168 }
2169
2170 assert(objects != (ipc_object_t *) 0);
2171
2172 /* destroy port rights carried in the message */
2173
2174 for (j = 0; j < dsc->count; j++) {
2175 ipc_object_t object = objects[j];
2176
2177 if (!IO_VALID(object)) {
2178 continue;
2179 }
2180
2181 ipc_object_destroy(object, dsc->disposition);
2182 }
2183
2184 /* destroy memory carried in the message */
2185
2186 assert(dsc->count != 0);
2187
2188 kfree_type(mach_port_t, dsc->count, dsc->address);
2189 break;
2190 }
2191 case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
2192 mach_msg_guarded_port_descriptor_t *dsc = (typeof(dsc)) & saddr->guarded_port;
2193
2194 /*
2195 * Destroy port rights carried in the message
2196 */
2197 if (!IP_VALID(dsc->name)) {
2198 continue;
2199 }
2200 ipc_object_destroy(ip_to_object(dsc->name), dsc->disposition);
2201 break;
2202 }
2203 default:
2204 panic("invalid descriptor type: (%p: %d)",
2205 saddr, saddr->type.type);
2206 }
2207 }
2208 }
2209
2210 /*
2211 * Routine: ipc_kmsg_clean_partial
2212 * Purpose:
2213 * Cleans a partially-acquired kernel message.
2214 * number is the index of the type descriptor
2215 * in the body of the message that contained the error.
2216 * If dolast, the memory and port rights in this last
2217 * type spec are also cleaned. In that case, number
2218 * specifies the number of port rights to clean.
2219 * Conditions:
2220 * Nothing locked.
2221 */
2222
2223 static void
ipc_kmsg_clean_partial(ipc_kmsg_t kmsg,mach_msg_type_number_t number,mach_msg_descriptor_t * desc,vm_offset_t paddr,vm_size_t length)2224 ipc_kmsg_clean_partial(
2225 ipc_kmsg_t kmsg,
2226 mach_msg_type_number_t number,
2227 mach_msg_descriptor_t *desc,
2228 vm_offset_t paddr,
2229 vm_size_t length)
2230 {
2231 ipc_object_t object;
2232 mach_msg_header_t *hdr = ikm_header(kmsg);
2233 mach_msg_bits_t mbits = hdr->msgh_bits;
2234
2235 /* deal with importance chain while we still have dest and voucher references */
2236 ipc_importance_clean(kmsg);
2237
2238 object = ip_to_object(hdr->msgh_remote_port);
2239 assert(IO_VALID(object));
2240 ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
2241
2242 object = ip_to_object(hdr->msgh_local_port);
2243 if (IO_VALID(object)) {
2244 ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
2245 }
2246
2247 object = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
2248 if (IO_VALID(object)) {
2249 assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
2250 ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
2251 ipc_kmsg_clear_voucher_port(kmsg);
2252 }
2253
2254 if (paddr) {
2255 kmem_free(ipc_kernel_copy_map, paddr, length);
2256 }
2257
2258 ipc_kmsg_clean_body(kmsg, number, desc);
2259 }
2260
2261 /*
2262 * Routine: ipc_kmsg_clean
2263 * Purpose:
2264 * Cleans a kernel message. Releases all rights,
2265 * references, and memory held by the message.
2266 * Conditions:
2267 * No locks held.
2268 */
2269
2270 static void
ipc_kmsg_clean(ipc_kmsg_t kmsg)2271 ipc_kmsg_clean(
2272 ipc_kmsg_t kmsg)
2273 {
2274 ipc_object_t object;
2275 mach_msg_bits_t mbits;
2276 mach_msg_header_t *hdr;
2277
2278 /* deal with importance chain while we still have dest and voucher references */
2279 ipc_importance_clean(kmsg);
2280
2281 hdr = ikm_header(kmsg);
2282 mbits = hdr->msgh_bits;
2283 object = ip_to_object(hdr->msgh_remote_port);
2284 if (IO_VALID(object)) {
2285 ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
2286 }
2287
2288 object = ip_to_object(hdr->msgh_local_port);
2289 if (IO_VALID(object)) {
2290 ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
2291 }
2292
2293 object = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
2294 if (IO_VALID(object)) {
2295 assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
2296 ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
2297 ipc_kmsg_clear_voucher_port(kmsg);
2298 }
2299
2300 if (mbits & MACH_MSGH_BITS_COMPLEX) {
2301 mach_msg_body_t *body;
2302
2303 body = (mach_msg_body_t *) (hdr + 1);
2304 ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
2305 (mach_msg_descriptor_t *)(body + 1));
2306 }
2307 }
2308
2309 /*
2310 * Routine: ipc_kmsg_set_prealloc
2311 * Purpose:
2312 * Assign a kmsg as a preallocated message buffer to a port.
2313 * Conditions:
2314 * port locked.
2315 */
2316 void
ipc_kmsg_set_prealloc(ipc_kmsg_t kmsg,ipc_port_t port)2317 ipc_kmsg_set_prealloc(
2318 ipc_kmsg_t kmsg,
2319 ipc_port_t port)
2320 {
2321 assert(kmsg->ikm_prealloc == IP_NULL);
2322 assert(kmsg->ikm_type == IKM_TYPE_ALL_INLINED);
2323 kmsg->ikm_prealloc = IP_NULL;
2324
2325 IP_SET_PREALLOC(port, kmsg);
2326 }
2327
2328 /*
2329 * Routine: ipc_kmsg_too_large
2330 * Purpose:
2331 * Return true if kmsg is too large to be received:
2332 *
2333 * If MACH64_RCV_LINEAR_VECTOR:
2334 * - combined message buffer is not large enough
2335 * to fit both the message (plus trailer) and
2336 * auxiliary data.
2337 * Otherwise:
2338 * - message buffer is not large enough
2339 * - auxiliary buffer is not large enough:
2340 * (1) kmsg is a vector with aux, but user expects
2341 * a scalar kmsg (ith_max_asize is 0)
2342 * (2) kmsg is a vector with aux, but user aux
2343 * buffer is not large enough.
2344 */
2345 bool
ipc_kmsg_too_large(mach_msg_size_t msg_size,mach_msg_size_t aux_size,mach_msg_option64_t option64,mach_msg_size_t max_msg_size,mach_msg_size_t max_aux_size,thread_t receiver)2346 ipc_kmsg_too_large(
2347 mach_msg_size_t msg_size,
2348 mach_msg_size_t aux_size,
2349 mach_msg_option64_t option64,
2350 mach_msg_size_t max_msg_size,
2351 mach_msg_size_t max_aux_size,
2352 thread_t receiver)
2353 {
2354 mach_msg_size_t tsize = REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(receiver),
2355 receiver->ith_option);
2356
2357 if (max_aux_size != 0) {
2358 assert(option64 & MACH64_MSG_VECTOR);
2359 }
2360
2361 if (option64 & MACH64_RCV_LINEAR_VECTOR) {
2362 assert(receiver->ith_max_asize == 0);
2363 assert(receiver->ith_aux_addr == 0);
2364 assert(option64 & MACH64_MSG_VECTOR);
2365
2366 if (max_msg_size < msg_size + tsize + aux_size) {
2367 return true;
2368 }
2369 } else {
2370 if (max_msg_size < msg_size + tsize) {
2371 return true;
2372 }
2373
2374 /*
2375 * only return too large if MACH64_MSG_VECTOR.
2376 *
2377 * silently drop aux data when receiver is not expecting it for compat
2378 * reasons.
2379 */
2380 if ((option64 & MACH64_MSG_VECTOR) && max_aux_size < aux_size) {
2381 return true;
2382 }
2383 }
2384
2385 return false;
2386 }
2387
2388 /*
2389 * Routine: ipc_kmsg_get_body_and_aux_from_user
2390 * Purpose:
2391 * Copies in user message (and aux) to allocated kernel message buffer.
2392 * Conditions:
2393 * msg_addr and msg_size must be valid. aux_addr and aux_size can
2394 * be NULL if kmsg is not vectorized, or vector kmsg does not carry
2395 * auxiliary data.
2396 *
2397 * msg up to sizeof(mach_msg_user_header_t) has been previously copied in,
2398 * and number of descriptors has been made known.
2399 *
2400 * kmsg_size already accounts for message header expansion.
2401 *
2402 * if aux_size is not 0, mach_msg_validate_data_vectors() guarantees that
2403 * aux_size must be larger than mach_msg_aux_header_t.
2404 */
2405 static mach_msg_return_t
ipc_kmsg_get_body_and_aux_from_user(ipc_kmsg_t kmsg,mach_vm_address_t msg_addr,mach_msg_size_t kmsg_size,mach_vm_address_t aux_addr,mach_msg_size_t aux_size,mach_msg_size_t desc_count,mach_msg_user_header_t user_header)2406 ipc_kmsg_get_body_and_aux_from_user(
2407 ipc_kmsg_t kmsg,
2408 mach_vm_address_t msg_addr,
2409 mach_msg_size_t kmsg_size,
2410 mach_vm_address_t aux_addr, /* Nullable */
2411 mach_msg_size_t aux_size, /* Nullable */
2412 mach_msg_size_t desc_count,
2413 mach_msg_user_header_t user_header)
2414 {
2415 mach_msg_header_t *hdr = ikm_header(kmsg);
2416 hdr->msgh_size = kmsg_size;
2417 hdr->msgh_bits = user_header.msgh_bits;
2418 hdr->msgh_remote_port = CAST_MACH_NAME_TO_PORT(user_header.msgh_remote_port);
2419 hdr->msgh_local_port = CAST_MACH_NAME_TO_PORT(user_header.msgh_local_port);
2420 hdr->msgh_voucher_port = user_header.msgh_voucher_port;
2421 hdr->msgh_id = user_header.msgh_id;
2422
2423 if (user_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
2424 mach_msg_base_t *kbase = (mach_msg_base_t *)hdr;
2425
2426 assert(kmsg_size >= sizeof(mach_msg_base_t));
2427 kbase->body.msgh_descriptor_count = desc_count;
2428
2429 /* copy in the rest of the message, after user_base */
2430 if (kmsg_size > sizeof(mach_msg_base_t)) {
2431 /*
2432 * if kmsg is linear, just copyin the remaining msg after base
2433 * and we are done. Otherwise, first copyin until the end of descriptors
2434 * or the message, whichever comes first.
2435 */
2436 mach_msg_size_t copyin_size = kmsg_size - sizeof(mach_msg_base_t);
2437 if (!ikm_is_linear(kmsg) && (desc_count * KERNEL_DESC_SIZE < copyin_size)) {
2438 copyin_size = desc_count * KERNEL_DESC_SIZE;
2439 }
2440
2441 assert((vm_offset_t)hdr + sizeof(mach_msg_base_t) +
2442 copyin_size <= ikm_kdata_end(kmsg));
2443
2444 if (copyinmsg(msg_addr + sizeof(mach_msg_user_base_t),
2445 (char *)hdr + sizeof(mach_msg_base_t),
2446 copyin_size)) {
2447 return MACH_SEND_INVALID_DATA;
2448 }
2449
2450 /*
2451 * next, pre-validate the descriptors user claims to have by checking
2452 * their size and type, instead of doing it at body copyin time.
2453 */
2454 mach_msg_return_t mr = ikm_check_descriptors(kmsg, current_map(), copyin_size);
2455 if (mr != MACH_MSG_SUCCESS) {
2456 return mr;
2457 }
2458
2459 /*
2460 * for non-linear kmsg, since we have copied in all data that can
2461 * possibly be a descriptor and pre-validated them, we can now measure
2462 * the actual descriptor size and copyin the remaining user data
2463 * following the descriptors, if there is any.
2464 */
2465 if (!ikm_is_linear(kmsg)) {
2466 mach_msg_size_t dsc_size = ikm_total_desc_size(kmsg, current_map(), 0, 0, true);
2467 assert(desc_count * KERNEL_DESC_SIZE >= dsc_size);
2468
2469 /* if there is user data after descriptors, copy it into data heap */
2470 if (kmsg_size > sizeof(mach_msg_base_t) + dsc_size) {
2471 copyin_size = kmsg_size - sizeof(mach_msg_base_t) - dsc_size;
2472
2473 assert(kmsg->ikm_udata != NULL);
2474 assert((vm_offset_t)kmsg->ikm_udata + copyin_size <= ikm_udata_end(kmsg));
2475 if (copyinmsg(msg_addr + sizeof(mach_msg_user_base_t) + dsc_size,
2476 (char *)kmsg->ikm_udata,
2477 copyin_size)) {
2478 return MACH_SEND_INVALID_DATA;
2479 }
2480 }
2481
2482 /* finally, nil out the extra user data we copied into kdata */
2483 if (desc_count * KERNEL_DESC_SIZE > dsc_size) {
2484 bzero((void *)((vm_offset_t)hdr + sizeof(mach_msg_base_t) + dsc_size),
2485 desc_count * KERNEL_DESC_SIZE - dsc_size);
2486 }
2487 }
2488 }
2489 } else {
2490 assert(desc_count == 0);
2491 /* copy in the rest of the message, after user_header */
2492 if (kmsg_size > sizeof(mach_msg_header_t)) {
2493 char *msg_content = ikm_is_linear(kmsg) ?
2494 (char *)hdr + sizeof(mach_msg_header_t) :
2495 (char *)kmsg->ikm_udata;
2496
2497 if (ikm_is_linear(kmsg)) {
2498 assert((vm_offset_t)hdr + kmsg_size <= ikm_kdata_end(kmsg));
2499 } else {
2500 assert((vm_offset_t)kmsg->ikm_udata + kmsg_size - sizeof(mach_msg_header_t) <= ikm_udata_end(kmsg));
2501 }
2502
2503 if (copyinmsg(msg_addr + sizeof(mach_msg_user_header_t), msg_content,
2504 kmsg_size - sizeof(mach_msg_header_t))) {
2505 return MACH_SEND_INVALID_DATA;
2506 }
2507 }
2508 }
2509
2510 if (aux_size > 0) {
2511 assert(aux_addr != 0);
2512 mach_msg_aux_header_t *aux_header = ikm_aux_header(kmsg);
2513
2514 assert(kmsg->ikm_aux_size == aux_size);
2515 assert(aux_header != NULL);
2516
2517 /* initialize aux data header */
2518 aux_header->msgdh_size = aux_size;
2519 aux_header->msgdh_reserved = 0;
2520
2521 /* copyin aux data after the header */
2522 assert(aux_size >= sizeof(mach_msg_aux_header_t));
2523 if (aux_size > sizeof(mach_msg_aux_header_t)) {
2524 if (kmsg->ikm_type != IKM_TYPE_ALL_INLINED) {
2525 assert((vm_offset_t)aux_header + aux_size <= ikm_udata_end(kmsg));
2526 } else {
2527 assert((vm_offset_t)aux_header + aux_size <= ikm_kdata_end(kmsg));
2528 }
2529 if (copyinmsg(aux_addr + sizeof(mach_msg_aux_header_t),
2530 (char *)aux_header + sizeof(mach_msg_aux_header_t),
2531 aux_size - sizeof(mach_msg_aux_header_t))) {
2532 return MACH_SEND_INVALID_DATA;
2533 }
2534 }
2535 }
2536
2537 return MACH_MSG_SUCCESS;
2538 }
2539
2540 /*
2541 * Routine: ipc_kmsg_get_from_user
2542 * Purpose:
2543 * Allocates a scalar or vector kernel message buffer.
2544 * Copies user message (and optional aux data) to the message buffer.
2545 * Conditions:
2546 * user_msg_size must have been bound checked. aux_{addr, size} are
2547 * 0 if not MACH64_MSG_VECTOR.
2548 * Returns:
2549 * Produces a kmsg reference on success.
2550 *
2551 * MACH_MSG_SUCCESS Acquired a message buffer.
2552 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
2553 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
2554 * MACH_SEND_TOO_LARGE Message too large to ever be sent.
2555 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
2556 * MACH_SEND_INVALID_DATA Couldn't copy message data.
2557 */
2558 mach_msg_return_t
ipc_kmsg_get_from_user(mach_vm_address_t msg_addr,mach_msg_size_t user_msg_size,mach_vm_address_t aux_addr,mach_msg_size_t aux_size,mach_msg_user_header_t user_header,mach_msg_size_t desc_count,mach_msg_option64_t option64,ipc_kmsg_t * kmsgp)2559 ipc_kmsg_get_from_user(
2560 mach_vm_address_t msg_addr,
2561 mach_msg_size_t user_msg_size,
2562 mach_vm_address_t aux_addr,
2563 mach_msg_size_t aux_size,
2564 mach_msg_user_header_t user_header,
2565 mach_msg_size_t desc_count,
2566 mach_msg_option64_t option64,
2567 ipc_kmsg_t *kmsgp)
2568 {
2569 mach_msg_size_t kmsg_size = 0;
2570 ipc_kmsg_alloc_flags_t flags = IPC_KMSG_ALLOC_USER;
2571 ipc_kmsg_t kmsg;
2572 kern_return_t kr;
2573
2574 kmsg_size = user_msg_size + USER_HEADER_SIZE_DELTA;
2575
2576 if (aux_size == 0) {
2577 assert(aux_addr == 0);
2578 } else {
2579 assert(aux_size >= sizeof(mach_msg_aux_header_t));
2580 }
2581
2582 if (!(option64 & MACH64_MSG_VECTOR)) {
2583 assert(aux_addr == 0);
2584 assert(aux_size == 0);
2585 }
2586
2587 /*
2588 * If not a mach_msg2() call to a message queue, allocate a linear kmsg.
2589 *
2590 * This is equivalent to making the following cases always linear:
2591 * - mach_msg_trap() calls.
2592 * - mach_msg2_trap() to kobject ports.
2593 * - mach_msg2_trap() from old simulators.
2594 */
2595 if (!(option64 & MACH64_SEND_MQ_CALL)) {
2596 flags |= IPC_KMSG_ALLOC_LINEAR;
2597 }
2598
2599 kmsg = ipc_kmsg_alloc(kmsg_size, aux_size, desc_count, flags);
2600 /* can fail if msg size is too large */
2601 if (kmsg == IKM_NULL) {
2602 return MACH_SEND_NO_BUFFER;
2603 }
2604
2605 kr = ipc_kmsg_get_body_and_aux_from_user(kmsg, msg_addr, kmsg_size,
2606 aux_addr, aux_size, desc_count, user_header);
2607 if (kr != MACH_MSG_SUCCESS) {
2608 ipc_kmsg_free(kmsg);
2609 return kr;
2610 }
2611
2612 *kmsgp = kmsg;
2613 return MACH_MSG_SUCCESS;
2614 }
2615
2616 /*
2617 * Routine: ipc_kmsg_get_from_kernel
2618 * Purpose:
2619 * First checks for a preallocated message
2620 * reserved for kernel clients. If not found or size is too large -
2621 * allocates a new kernel message buffer.
2622 * Copies a kernel message to the message buffer.
2623 * Only resource errors are allowed.
2624 * Conditions:
2625 * Nothing locked.
2626 * Ports in header are ipc_port_t.
2627 * Returns:
2628 * MACH_MSG_SUCCESS Acquired a message buffer.
2629 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
2630 */
2631
2632 mach_msg_return_t
ipc_kmsg_get_from_kernel(mach_msg_header_t * msg,mach_msg_size_t size,ipc_kmsg_t * kmsgp)2633 ipc_kmsg_get_from_kernel(
2634 mach_msg_header_t *msg,
2635 mach_msg_size_t size, /* can be larger than prealloc space */
2636 ipc_kmsg_t *kmsgp)
2637 {
2638 ipc_kmsg_t kmsg;
2639 ipc_port_t dest_port;
2640 mach_msg_header_t *hdr;
2641
2642 assert(size >= sizeof(mach_msg_header_t));
2643 assert((size & 3) == 0);
2644
2645 dest_port = msg->msgh_remote_port;
2646
2647 /*
2648 * See if the port has a pre-allocated kmsg for kernel
2649 * clients. These are set up for those kernel clients
2650 * which cannot afford to wait.
2651 */
2652 if (IP_VALID(dest_port) && IP_PREALLOC(dest_port)) {
2653 ip_mq_lock(dest_port);
2654
2655 if (!ip_active(dest_port)) {
2656 ip_mq_unlock(dest_port);
2657 return MACH_SEND_NO_BUFFER;
2658 }
2659
2660 assert(IP_PREALLOC(dest_port));
2661 kmsg = dest_port->ip_premsg;
2662
2663 if (ikm_prealloc_inuse(kmsg)) {
2664 ip_mq_unlock(dest_port);
2665 return MACH_SEND_NO_BUFFER;
2666 }
2667
2668 assert(kmsg->ikm_type == IKM_TYPE_ALL_INLINED);
2669 assert(kmsg->ikm_aux_size == 0);
2670
2671 if (size + MAX_TRAILER_SIZE > IKM_SAVED_MSG_SIZE) {
2672 ip_mq_unlock(dest_port);
2673 return MACH_SEND_TOO_LARGE;
2674 }
2675 ikm_prealloc_set_inuse(kmsg, dest_port);
2676
2677 ip_mq_unlock(dest_port);
2678 } else {
2679 kmsg = ipc_kmsg_alloc(size, 0, 0, IPC_KMSG_ALLOC_KERNEL);
2680 if (kmsg == IKM_NULL) {
2681 return MACH_SEND_NO_BUFFER;
2682 }
2683 }
2684
2685 hdr = ikm_header(kmsg);
2686 assert(ikm_is_linear(kmsg));
2687
2688 memcpy(hdr, msg, size);
2689 hdr->msgh_size = size;
2690
2691 *kmsgp = kmsg;
2692 return MACH_MSG_SUCCESS;
2693 }
2694
2695 /*
2696 * Routine: ipc_kmsg_send
2697 * Purpose:
2698 * Send a message. The message holds a reference
2699 * for the destination port in the msgh_remote_port field.
2700 *
2701 * If unsuccessful, the caller still has possession of
2702 * the message and must do something with it. If successful,
2703 * the message is queued, given to a receiver, destroyed,
2704 * or handled directly by the kernel via mach_msg.
2705 * Conditions:
2706 * Nothing locked.
2707 * Returns:
2708 * MACH_MSG_SUCCESS The message was accepted.
2709 * MACH_SEND_TIMED_OUT Caller still has message.
2710 * MACH_SEND_INTERRUPTED Caller still has message.
2711 * MACH_SEND_INVALID_DEST Caller still has message.
2712 * MACH_SEND_INVALID_OPTIONS Caller still has message.
2713 */
2714 mach_msg_return_t
ipc_kmsg_send(ipc_kmsg_t kmsg,mach_msg_option64_t option64,mach_msg_timeout_t send_timeout)2715 ipc_kmsg_send(
2716 ipc_kmsg_t kmsg,
2717 mach_msg_option64_t option64,
2718 mach_msg_timeout_t send_timeout)
2719 {
2720 ipc_port_t port;
2721 thread_t th = current_thread();
2722 mach_msg_return_t error = MACH_MSG_SUCCESS;
2723 boolean_t kernel_reply = FALSE;
2724 mach_msg_header_t *hdr;
2725
2726 /* Check if honor qlimit flag is set on thread. */
2727 if ((th->options & TH_OPT_HONOR_QLIMIT) == TH_OPT_HONOR_QLIMIT) {
2728 /* Remove the MACH_SEND_ALWAYS flag to honor queue limit. */
2729 option64 &= (~MACH64_SEND_ALWAYS);
2730 /* Add the timeout flag since the message queue might be full. */
2731 option64 |= MACH64_SEND_TIMEOUT;
2732 th->options &= (~TH_OPT_HONOR_QLIMIT);
2733 }
2734
2735 #if IMPORTANCE_INHERITANCE
2736 bool did_importance = false;
2737 #if IMPORTANCE_TRACE
2738 mach_msg_id_t imp_msgh_id = -1;
2739 int sender_pid = -1;
2740 #endif /* IMPORTANCE_TRACE */
2741 #endif /* IMPORTANCE_INHERITANCE */
2742
2743 hdr = ikm_header(kmsg);
2744 /* don't allow the creation of a circular loop */
2745 if (hdr->msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
2746 ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_ALL);
2747 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_MSGH_BITS_CIRCULAR);
2748 return MACH_MSG_SUCCESS;
2749 }
2750
2751 ipc_voucher_send_preprocessing(kmsg);
2752
2753 port = hdr->msgh_remote_port;
2754 assert(IP_VALID(port));
2755 ip_mq_lock(port);
2756
2757 if (option64 & MACH64_MACH_MSG2) {
2758 /*
2759 * This is a _user_ message via mach_msg2_trap()。
2760 *
2761 * To curb kobject port/message queue confusion and improve control flow
2762 * integrity, mach_msg2_trap() invocations mandate the use of either
2763 * MACH64_SEND_KOBJECT_CALL or MACH64_SEND_MQ_CALL and that the flag
2764 * matches the underlying port type. (unless the call is from a simulator,
2765 * since old simulators keep using mach_msg() in all cases indiscriminatingly.)
2766 *
2767 * Since:
2768 * (1) We make sure to always pass either MACH64_SEND_MQ_CALL or
2769 * MACH64_SEND_KOBJECT_CALL bit at all sites outside simulators
2770 * (checked by mach_msg2_trap());
2771 * (2) We checked in mach_msg2_trap() that _exactly_ one of the three bits is set.
2772 *
2773 * CFI check cannot be bypassed by simply setting MACH64_SEND_ANY.
2774 */
2775 #if XNU_TARGET_OS_OSX
2776 if (option64 & MACH64_SEND_ANY) {
2777 goto cfi_passed;
2778 }
2779 #endif /* XNU_TARGET_OS_OSX */
2780
2781 if (ip_is_kobject(port)) {
2782 natural_t kotype = ip_kotype(port);
2783
2784 if (__improbable(kotype == IKOT_TIMER)) {
2785 /*
2786 * For bincompat, let's still allow user messages to timer port, but
2787 * force MACH64_SEND_MQ_CALL flag for memory segregation.
2788 */
2789 if (__improbable(!(option64 & MACH64_SEND_MQ_CALL))) {
2790 ip_mq_unlock(port);
2791 mach_port_guard_exception(0, 0, 0, kGUARD_EXC_INVALID_OPTIONS);
2792 return MACH_SEND_INVALID_OPTIONS;
2793 }
2794 } else {
2795 /* Otherwise, caller must set MACH64_SEND_KOBJECT_CALL. */
2796 if (__improbable(!(option64 & MACH64_SEND_KOBJECT_CALL))) {
2797 ip_mq_unlock(port);
2798 mach_port_guard_exception(0, 0, 0, kGUARD_EXC_INVALID_OPTIONS);
2799 return MACH_SEND_INVALID_OPTIONS;
2800 }
2801 }
2802 }
2803
2804 #if CONFIG_CSR
2805 if (csr_check(CSR_ALLOW_KERNEL_DEBUGGER) == 0) {
2806 /*
2807 * Allow MACH64_SEND_KOBJECT_CALL flag to message queues when SIP
2808 * is off (for Mach-on-Mach emulation). The other direction is still
2809 * not allowed (MIG KernelServer assumes a linear kmsg).
2810 */
2811 goto cfi_passed;
2812 }
2813 #endif /* CONFIG_CSR */
2814
2815 /* If destination is a message queue, caller must set MACH64_SEND_MQ_CALL */
2816 if (__improbable((!ip_is_kobject(port) &&
2817 !(option64 & MACH64_SEND_MQ_CALL)))) {
2818 ip_mq_unlock(port);
2819 mach_port_guard_exception(0, 0, 0, kGUARD_EXC_INVALID_OPTIONS);
2820 return MACH_SEND_INVALID_OPTIONS;
2821 }
2822 }
2823
2824 #if (XNU_TARGET_OS_OSX || CONFIG_CSR)
2825 cfi_passed:
2826 #endif /* XNU_TARGET_OS_OSX || CONFIG_CSR */
2827
2828 /*
2829 * If the destination has been guarded with a reply context, and the
2830 * sender is consuming a send-once right, then assume this is a reply
2831 * to an RPC and we need to validate that this sender is currently in
2832 * the correct context.
2833 */
2834 if (enforce_strict_reply && port->ip_reply_context != 0 &&
2835 ((option64 & MACH64_SEND_KERNEL) == 0) &&
2836 MACH_MSGH_BITS_REMOTE(hdr->msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE) {
2837 error = ipc_kmsg_validate_reply_context_locked((mach_msg_option_t)option64,
2838 port, th->ith_voucher, th->ith_voucher_name);
2839 if (error != MACH_MSG_SUCCESS) {
2840 ip_mq_unlock(port);
2841 return error;
2842 }
2843 }
2844
2845 #if IMPORTANCE_INHERITANCE
2846 retry:
2847 #endif /* IMPORTANCE_INHERITANCE */
2848 /*
2849 * Can't deliver to a dead port.
2850 * However, we can pretend it got sent
2851 * and was then immediately destroyed.
2852 */
2853 if (!ip_active(port)) {
2854 ip_mq_unlock(port);
2855 #if MACH_FLIPC
2856 if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2857 flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2858 }
2859 #endif
2860 if (did_importance) {
2861 /*
2862 * We're going to pretend we delivered this message
2863 * successfully, and just eat the kmsg. However, the
2864 * kmsg is actually visible via the importance_task!
2865 * We need to cleanup this linkage before we destroy
2866 * the message, and more importantly before we set the
2867 * msgh_remote_port to NULL. See: 34302571
2868 */
2869 ipc_importance_clean(kmsg);
2870 }
2871 ip_release(port); /* JMM - Future: release right, not just ref */
2872 ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_SKIP_REMOTE);
2873 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_INVALID_DEST);
2874 return MACH_MSG_SUCCESS;
2875 }
2876
2877 if (ip_in_space(port, ipc_space_kernel)) {
2878 require_ip_active(port);
2879 port->ip_messages.imq_seqno++;
2880 ip_mq_unlock(port);
2881
2882 counter_inc(¤t_task()->messages_sent);
2883
2884 /*
2885 * Call the server routine, and get the reply message to send.
2886 */
2887 kmsg = ipc_kobject_server(port, kmsg, (mach_msg_option_t)option64);
2888 if (kmsg == IKM_NULL) {
2889 return MACH_MSG_SUCCESS;
2890 }
2891 /* reload hdr since kmsg changed */
2892 hdr = ikm_header(kmsg);
2893
2894 /* sign the reply message */
2895 ipc_kmsg_init_trailer(kmsg, TASK_NULL);
2896 ikm_sign(kmsg);
2897
2898 /* restart the KMSG_INFO tracing for the reply message */
2899 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
2900 port = hdr->msgh_remote_port;
2901 assert(IP_VALID(port));
2902 ip_mq_lock(port);
2903 /* fall thru with reply - same options */
2904 kernel_reply = TRUE;
2905 if (!ip_active(port)) {
2906 error = MACH_SEND_INVALID_DEST;
2907 }
2908 }
2909
2910 #if IMPORTANCE_INHERITANCE
2911 /*
2912 * Need to see if this message needs importance donation and/or
2913 * propagation. That routine can drop the port lock temporarily.
2914 * If it does we'll have to revalidate the destination.
2915 */
2916 if (!did_importance) {
2917 did_importance = true;
2918 if (ipc_importance_send(kmsg, (mach_msg_option_t)option64)) {
2919 goto retry;
2920 }
2921 }
2922 #endif /* IMPORTANCE_INHERITANCE */
2923
2924 if (error != MACH_MSG_SUCCESS) {
2925 ip_mq_unlock(port);
2926 } else {
2927 /*
2928 * We have a valid message and a valid reference on the port.
2929 * call mqueue_send() on its message queue.
2930 */
2931 ipc_special_reply_port_msg_sent(port);
2932
2933 error = ipc_mqueue_send_locked(&port->ip_messages, kmsg,
2934 (mach_msg_option_t)option64, send_timeout);
2935 /* port unlocked */
2936 }
2937
2938 #if IMPORTANCE_INHERITANCE
2939 if (did_importance) {
2940 __unused int importance_cleared = 0;
2941 switch (error) {
2942 case MACH_SEND_TIMED_OUT:
2943 case MACH_SEND_NO_BUFFER:
2944 case MACH_SEND_INTERRUPTED:
2945 case MACH_SEND_INVALID_DEST:
2946 /*
2947 * We still have the kmsg and its
2948 * reference on the port. But we
2949 * have to back out the importance
2950 * boost.
2951 *
2952 * The port could have changed hands,
2953 * be inflight to another destination,
2954 * etc... But in those cases our
2955 * back-out will find the new owner
2956 * (and all the operations that
2957 * transferred the right should have
2958 * applied their own boost adjustments
2959 * to the old owner(s)).
2960 */
2961 importance_cleared = 1;
2962 ipc_importance_clean(kmsg);
2963 break;
2964
2965 case MACH_MSG_SUCCESS:
2966 default:
2967 break;
2968 }
2969 #if IMPORTANCE_TRACE
2970 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_MSG, IMP_MSG_SEND)) | DBG_FUNC_END,
2971 task_pid(current_task()), sender_pid, imp_msgh_id, importance_cleared, 0);
2972 #endif /* IMPORTANCE_TRACE */
2973 }
2974 #endif /* IMPORTANCE_INHERITANCE */
2975
2976 /*
2977 * If the port has been destroyed while we wait, treat the message
2978 * as a successful delivery (like we do for an inactive port).
2979 */
2980 if (error == MACH_SEND_INVALID_DEST) {
2981 #if MACH_FLIPC
2982 if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2983 flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2984 }
2985 #endif
2986 ip_release(port); /* JMM - Future: release right, not just ref */
2987 ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_SKIP_REMOTE);
2988 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_INVALID_DEST);
2989 return MACH_MSG_SUCCESS;
2990 }
2991
2992 if (error != MACH_MSG_SUCCESS && kernel_reply) {
2993 /*
2994 * Kernel reply messages that fail can't be allowed to
2995 * pseudo-receive on error conditions. We need to just treat
2996 * the message as a successful delivery.
2997 */
2998 #if MACH_FLIPC
2999 if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
3000 flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
3001 }
3002 #endif
3003 ip_release(port); /* JMM - Future: release right, not just ref */
3004 ipc_kmsg_destroy(kmsg, IPC_KMSG_DESTROY_SKIP_REMOTE);
3005 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, error);
3006 return MACH_MSG_SUCCESS;
3007 }
3008 return error;
3009 }
3010
3011 /*
3012 * Routine: ipc_kmsg_convert_header_to_user
3013 * Purpose:
3014 * Convert a kmsg header back to user header.
3015 */
3016 static mach_msg_user_header_t *
ipc_kmsg_convert_header_to_user(ipc_kmsg_t kmsg)3017 ipc_kmsg_convert_header_to_user(
3018 ipc_kmsg_t kmsg)
3019 {
3020 assert(current_task() != kernel_task);
3021 mach_msg_header_t *hdr = ikm_header(kmsg);
3022
3023 /* user_header is kernel header shifted in place */
3024 mach_msg_user_header_t *user_header =
3025 (mach_msg_user_header_t *)((vm_offset_t)(hdr) + USER_HEADER_SIZE_DELTA);
3026
3027 mach_msg_bits_t bits = hdr->msgh_bits;
3028 mach_msg_size_t kmsg_size = hdr->msgh_size;
3029 mach_port_name_t remote_port = CAST_MACH_PORT_TO_NAME(hdr->msgh_remote_port);
3030 mach_port_name_t local_port = CAST_MACH_PORT_TO_NAME(hdr->msgh_local_port);
3031 mach_port_name_t voucher_port = hdr->msgh_voucher_port;
3032 mach_msg_id_t id = hdr->msgh_id;
3033
3034 user_header->msgh_id = id;
3035 user_header->msgh_local_port = local_port;
3036 user_header->msgh_remote_port = remote_port;
3037 user_header->msgh_voucher_port = voucher_port;
3038 user_header->msgh_size = kmsg_size - USER_HEADER_SIZE_DELTA;
3039 user_header->msgh_bits = bits;
3040
3041 return user_header;
3042 }
3043
3044 /*
3045 * Routine: ipc_kmsg_put_vector_to_user
3046 * Purpose:
3047 * Copies a scalar or vector message buffer to a user message.
3048 * Frees the message buffer.
3049 * Conditions:
3050 * Nothing locked. kmsg is freed upon return.
3051 *
3052 * 1. If user has allocated space for aux data, mach_msg_validate_data_vectors
3053 * guarantees that rcv_aux_addr is non-zero, and max_aux_size must be at least
3054 * sizeof(mach_msg_aux_header_t). In case the kmsg is a scalar or a vector
3055 * without auxiliary data, copy out an empty aux header to rcv_aux_addr which
3056 * serves as EOF.
3057 *
3058 * 2. If kmsg is a vector without aux, copy out the message as if it's scalar
3059 *
3060 * 3. If an aux buffer is provided by user, max_aux_size must be large enough
3061 * to at least fit the minimum aux header built by msg_receive_error().
3062 *
3063 * 4. If MACH64_RCV_LINEAR_VECTOR is set, use rcv_msg_addr as the combined
3064 * buffer for message proper and aux data. rcv_aux_addr and max_aux_size
3065 * must be passed as zeros and are ignored.
3066 *
3067 * Returns:
3068 * MACH_MSG_SUCCESS Copied data out of message buffer.
3069 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
3070 */
3071 static mach_msg_return_t
ipc_kmsg_put_vector_to_user(ipc_kmsg_t kmsg,mach_msg_option64_t option64,mach_vm_address_t rcv_msg_addr,mach_msg_size_t max_msg_size,mach_vm_address_t rcv_aux_addr,mach_msg_size_t max_aux_size,mach_msg_size_t trailer_size,mach_msg_size_t * msg_sizep,mach_msg_size_t * aux_sizep)3072 ipc_kmsg_put_vector_to_user(
3073 ipc_kmsg_t kmsg, /* scalar or vector */
3074 mach_msg_option64_t option64,
3075 mach_vm_address_t rcv_msg_addr,
3076 mach_msg_size_t max_msg_size,
3077 mach_vm_address_t rcv_aux_addr, /* Nullable */
3078 mach_msg_size_t max_aux_size, /* Nullable */
3079 mach_msg_size_t trailer_size,
3080 mach_msg_size_t *msg_sizep, /* size of msg copied out */
3081 mach_msg_size_t *aux_sizep) /* size of aux copied out */
3082 {
3083 mach_msg_size_t cpout_msg_size, cpout_aux_size;
3084 mach_msg_user_header_t *user_hdr;
3085 mach_msg_return_t mr = MACH_MSG_SUCCESS;
3086
3087 DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_put_vector_to_user()");
3088
3089 assert(option64 & MACH64_MSG_VECTOR);
3090 user_hdr = ipc_kmsg_convert_header_to_user(kmsg);
3091 /* ikm_header->msgh_size is now user msg size */
3092
3093 /* msg and aux size might be updated by msg_receive_error() */
3094 cpout_msg_size = user_hdr->msgh_size + trailer_size;
3095 cpout_aux_size = ipc_kmsg_aux_data_size(kmsg);
3096
3097 /*
3098 * For ipc_kmsg_put_scalar_to_user() we try to receive up to
3099 * msg buffer size for backward-compatibility. (See below).
3100 *
3101 * For mach_msg2(), we just error out here.
3102 */
3103 if (option64 & MACH64_RCV_LINEAR_VECTOR) {
3104 if (cpout_msg_size + cpout_aux_size > max_msg_size) {
3105 mr = MACH_RCV_INVALID_DATA;
3106 cpout_msg_size = 0;
3107 cpout_aux_size = 0;
3108 goto failed;
3109 }
3110 assert(rcv_aux_addr == 0);
3111 assert(max_aux_size == 0);
3112
3113 if (option64 & MACH64_RCV_STACK) {
3114 rcv_msg_addr += max_msg_size - cpout_msg_size - cpout_aux_size;
3115 }
3116 rcv_aux_addr = rcv_msg_addr + cpout_msg_size;
3117 max_aux_size = cpout_aux_size;
3118 } else {
3119 /*
3120 * (81193887) some clients stomp their own stack due to mis-sized
3121 * combined send/receives where the receive buffer didn't account
3122 * for the trailer size.
3123 *
3124 * At the very least, avoid smashing their stack.
3125 */
3126 if (cpout_msg_size > max_msg_size) {
3127 cpout_msg_size = max_msg_size;
3128
3129 /* just copy out the partial message for compatibility */
3130 cpout_aux_size = 0;
3131 goto copyout_msg;
3132 }
3133
3134 if (cpout_aux_size > max_aux_size) {
3135 /*
3136 * mach_msg_validate_data_vectors() guarantees
3137 * that max_aux_size is at least what msg_receive_error() builds
3138 * during MACH_RCV_TOO_LARGE, if an aux buffer is provided.
3139 *
3140 * So this can only happen if caller is trying to receive a vector
3141 * kmsg with aux, but did not provide aux buffer. And we must be
3142 * coming from msg_receive_error().
3143 */
3144 assert(rcv_aux_addr == 0);
3145
3146 /* just copy out the minimal message header and trailer */
3147 cpout_aux_size = 0;
3148 goto copyout_msg;
3149 }
3150 }
3151
3152 /*
3153 * at this point, we are certain that receiver has enough space for both msg
3154 * proper and aux data.
3155 */
3156 assert(max_aux_size >= cpout_aux_size);
3157 if (option64 & MACH64_RCV_LINEAR_VECTOR) {
3158 assert(max_msg_size >= cpout_msg_size + cpout_aux_size);
3159 } else {
3160 assert(max_msg_size >= cpout_msg_size);
3161 }
3162
3163 /* receive the aux data to user space */
3164 if (cpout_aux_size) {
3165 mach_msg_aux_header_t *aux_header;
3166
3167 if ((aux_header = ikm_aux_header(kmsg)) != NULL) {
3168 /* user expecting aux data, and kmsg has it */
3169 assert(rcv_aux_addr != 0);
3170 if (copyoutmsg((const char *)aux_header, rcv_aux_addr, cpout_aux_size)) {
3171 mr = MACH_RCV_INVALID_DATA;
3172 cpout_aux_size = 0;
3173 cpout_msg_size = 0;
3174 goto failed;
3175 }
3176 /* success, copy out the msg next */
3177 goto copyout_msg;
3178 }
3179 }
3180
3181 /* we only reach here if have not copied out any aux data */
3182 if (!(option64 & MACH64_RCV_LINEAR_VECTOR) && rcv_aux_addr != 0) {
3183 /*
3184 * If user has a buffer for aux data, at least copy out an empty header
3185 * which serves as an EOF. We don't need to do so for linear vector
3186 * because it's used in kevent context and we will return cpout_aux_size
3187 * as 0 on ext[3] to signify empty aux data.
3188 *
3189 * See: filt_machportprocess().
3190 */
3191 mach_msg_aux_header_t header = {.msgdh_size = 0};
3192 cpout_aux_size = sizeof(header);
3193 assert(max_aux_size >= cpout_aux_size);
3194 if (copyoutmsg((const char *)&header, rcv_aux_addr, cpout_aux_size)) {
3195 mr = MACH_RCV_INVALID_DATA;
3196 cpout_aux_size = 0;
3197 cpout_msg_size = 0;
3198 goto failed;
3199 }
3200 }
3201
3202 copyout_msg:
3203 /* receive the message proper to user space */
3204 if (ikm_is_linear(kmsg)) {
3205 if (copyoutmsg((const char *)user_hdr, rcv_msg_addr, cpout_msg_size)) {
3206 mr = MACH_RCV_INVALID_DATA;
3207 cpout_msg_size = 0;
3208 goto failed;
3209 }
3210 } else {
3211 mach_msg_size_t kdata_size = ikm_kdata_size(kmsg, current_map(),
3212 USER_HEADER_SIZE_DELTA, true);
3213 mach_msg_size_t udata_size = ikm_content_size(kmsg, current_map(),
3214 USER_HEADER_SIZE_DELTA, true) + trailer_size;
3215
3216 mach_msg_size_t kdata_copyout_size = MIN(kdata_size, cpout_msg_size);
3217 mach_msg_size_t udata_copyout_size = MIN(udata_size, cpout_msg_size - kdata_copyout_size);
3218
3219 /* First copy out kdata */
3220 if (copyoutmsg((const char *)user_hdr, rcv_msg_addr, kdata_copyout_size)) {
3221 mr = MACH_RCV_INVALID_DATA;
3222 cpout_msg_size = 0;
3223 goto failed;
3224 }
3225
3226 /* Then copy out udata */
3227 if (copyoutmsg((const char *)kmsg->ikm_udata, rcv_msg_addr + kdata_copyout_size,
3228 udata_copyout_size)) {
3229 mr = MACH_RCV_INVALID_DATA;
3230 cpout_msg_size = 0;
3231 goto failed;
3232 }
3233 }
3234
3235 /* at this point, we have copied out the message proper */
3236 assert(cpout_msg_size > 0);
3237
3238 failed:
3239
3240 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
3241 (rcv_msg_addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS ||
3242 rcv_msg_addr + cpout_msg_size >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) ? (uintptr_t)0 : (uintptr_t)rcv_msg_addr,
3243 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
3244 1, /* this is on the receive/copyout path */
3245 0, 0);
3246
3247 ipc_kmsg_free(kmsg);
3248
3249 if (msg_sizep) {
3250 *msg_sizep = cpout_msg_size;
3251 }
3252
3253 if (aux_sizep) {
3254 *aux_sizep = cpout_aux_size;
3255 }
3256
3257 return mr;
3258 }
3259
3260 /*
3261 * Routine: ipc_kmsg_put_scalar_to_user
3262 * Purpose:
3263 * Copies a scalar message buffer to a user message.
3264 * Frees the message buffer.
3265 * Conditions:
3266 * Nothing locked. kmsg is freed upon return.
3267 *
3268 * Returns:
3269 * MACH_MSG_SUCCESS Copied data out of message buffer.
3270 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
3271 */
3272 static mach_msg_return_t
ipc_kmsg_put_scalar_to_user(ipc_kmsg_t kmsg,__unused mach_msg_option64_t option64,mach_vm_address_t rcv_addr,mach_msg_size_t rcv_size,mach_msg_size_t trailer_size,mach_msg_size_t * sizep)3273 ipc_kmsg_put_scalar_to_user(
3274 ipc_kmsg_t kmsg,
3275 __unused mach_msg_option64_t option64,
3276 mach_vm_address_t rcv_addr,
3277 mach_msg_size_t rcv_size,
3278 mach_msg_size_t trailer_size,
3279 mach_msg_size_t *sizep) /* size of msg copied out */
3280 {
3281 mach_msg_size_t copyout_size;
3282 mach_msg_user_header_t *user_hdr;
3283 mach_msg_return_t mr = MACH_MSG_SUCCESS;
3284
3285 DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_put_scalar_to_user()");
3286
3287 assert(!(option64 & MACH64_MSG_VECTOR));
3288 /* stack-based receive must be vectorized */
3289 assert(!(option64 & MACH64_RCV_STACK));
3290 /*
3291 * We will reach here in one of the following cases, kmsg size
3292 * may have been updated by msg_receive_error();
3293 *
3294 * 1. kmsg is scalar: OK to copy out as scalar
3295 * 2. kmsg is vector without aux: OK to copy out as scalar
3296 * 3. kmsg is vector with aux: silently dropping aux data
3297 */
3298 user_hdr = ipc_kmsg_convert_header_to_user(kmsg);
3299 /* ikm_header->msgh_size is now user msg size */
3300
3301 copyout_size = user_hdr->msgh_size + trailer_size;
3302
3303 /*
3304 * (81193887) some clients stomp their own stack due to mis-sized
3305 * combined send/receives where the receive buffer didn't account
3306 * for the trailer size.
3307 *
3308 * At the very least, avoid smashing their stack.
3309 */
3310 if (copyout_size > rcv_size) {
3311 copyout_size = rcv_size;
3312 }
3313
3314 if (ikm_is_linear(kmsg)) {
3315 if (copyoutmsg((const char *)user_hdr, rcv_addr, copyout_size)) {
3316 mr = MACH_RCV_INVALID_DATA;
3317 copyout_size = 0;
3318 }
3319 } else {
3320 mach_msg_size_t kdata_size = ikm_kdata_size(kmsg, current_map(),
3321 USER_HEADER_SIZE_DELTA, true);
3322 mach_msg_size_t udata_size = ikm_content_size(kmsg, current_map(),
3323 USER_HEADER_SIZE_DELTA, true) + trailer_size;
3324
3325 mach_msg_size_t kdata_copyout_size = MIN(kdata_size, copyout_size);
3326 mach_msg_size_t udata_copyout_size = MIN(udata_size, copyout_size - kdata_copyout_size);
3327
3328 /* First copy out kdata */
3329 if (copyoutmsg((const char *)user_hdr, rcv_addr, kdata_copyout_size)) {
3330 mr = MACH_RCV_INVALID_DATA;
3331 copyout_size = 0;
3332 }
3333
3334 /* Then copy out udata */
3335 if (copyoutmsg((const char *)kmsg->ikm_udata, rcv_addr + kdata_copyout_size,
3336 udata_copyout_size)) {
3337 mr = MACH_RCV_INVALID_DATA;
3338 copyout_size = 0;
3339 }
3340 }
3341
3342 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
3343 (rcv_addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS ||
3344 rcv_addr + copyout_size >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) ? (uintptr_t)0 : (uintptr_t)rcv_addr,
3345 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
3346 1, /* this is on the receive/copyout path */
3347 0, 0);
3348
3349 ipc_kmsg_free(kmsg);
3350
3351 if (sizep) {
3352 *sizep = copyout_size;
3353 }
3354 return mr;
3355 }
3356
3357 /*
3358 * Routine: ipc_kmsg_put_to_user
3359 * Purpose:
3360 * Copies a scalar or vector message buffer to a user message.
3361 * Frees the message buffer.
3362 * See comments above ipc_kmsg_put_{scalar, vector}_to_user().
3363 * Conditions:
3364 * Nothing locked. kmsg is freed upon return.
3365 *
3366 * Returns:
3367 * MACH_MSG_SUCCESS Copied data out of message buffer.
3368 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
3369 */
3370 mach_msg_return_t
ipc_kmsg_put_to_user(ipc_kmsg_t kmsg,mach_msg_option64_t option64,mach_vm_address_t rcv_msg_addr,mach_msg_size_t max_msg_size,mach_vm_address_t rcv_aux_addr,mach_msg_size_t max_aux_size,mach_msg_size_t trailer_size,mach_msg_size_t * msg_sizep,mach_msg_size_t * aux_sizep)3371 ipc_kmsg_put_to_user(
3372 ipc_kmsg_t kmsg, /* scalar or vector */
3373 mach_msg_option64_t option64,
3374 mach_vm_address_t rcv_msg_addr,
3375 mach_msg_size_t max_msg_size,
3376 mach_vm_address_t rcv_aux_addr, /* Nullable */
3377 mach_msg_size_t max_aux_size, /* Nullable */
3378 mach_msg_size_t trailer_size,
3379 mach_msg_size_t *msg_sizep, /* size of msg copied out */
3380 mach_msg_size_t *aux_sizep) /* size of aux copied out */
3381 {
3382 mach_msg_return_t mr;
3383
3384 if (option64 & MACH64_MSG_VECTOR) {
3385 mr = ipc_kmsg_put_vector_to_user(kmsg, option64, rcv_msg_addr,
3386 max_msg_size, rcv_aux_addr, max_aux_size, trailer_size,
3387 msg_sizep, aux_sizep);
3388 } else {
3389 mr = ipc_kmsg_put_scalar_to_user(kmsg, option64, rcv_msg_addr,
3390 max_msg_size, trailer_size, msg_sizep);
3391 if (mr == MACH_MSG_SUCCESS && aux_sizep != NULL) {
3392 *aux_sizep = 0;
3393 }
3394 }
3395
3396 /*
3397 * During message copyout, MACH_RCV_INVALID_DATA takes precedence
3398 * over all other errors. Other error code will be treated as
3399 * MACH_MSG_SUCCESS by mach_msg_receive_results().
3400 *
3401 * See: msg_receive_error().
3402 */
3403 assert(mr == MACH_RCV_INVALID_DATA || mr == MACH_MSG_SUCCESS);
3404 return mr;
3405 }
3406
3407 /*
3408 * Routine: ipc_kmsg_put_to_kernel
3409 * Purpose:
3410 * Copies a message buffer to a kernel message.
3411 * Frees the message buffer.
3412 * No errors allowed.
3413 * Conditions:
3414 * Nothing locked.
3415 */
3416
3417 void
ipc_kmsg_put_to_kernel(mach_msg_header_t * msg,ipc_kmsg_t kmsg,mach_msg_size_t rcv_size)3418 ipc_kmsg_put_to_kernel(
3419 mach_msg_header_t *msg,
3420 ipc_kmsg_t kmsg,
3421 mach_msg_size_t rcv_size) /* includes trailer size */
3422 {
3423 mach_msg_header_t *hdr = ikm_header(kmsg);
3424
3425 assert(kmsg->ikm_aux_size == 0);
3426 assert(rcv_size >= hdr->msgh_size);
3427
3428 if (ikm_is_linear(kmsg)) {
3429 (void)memcpy((void *)msg, (const void *)hdr, rcv_size);
3430 } else {
3431 mach_msg_size_t kdata_size = ikm_kdata_size(kmsg, current_map(), 0, false);
3432
3433 /* First memcpy kdata */
3434 assert(rcv_size >= kdata_size);
3435 (void)memcpy((void *)msg, (const void *)hdr, kdata_size);
3436
3437 /* Fill the remaining space with udata */
3438 (void)memcpy((void *)((vm_offset_t)msg + kdata_size),
3439 (const void *)kmsg->ikm_udata, rcv_size - kdata_size);
3440 }
3441
3442 ipc_kmsg_free(kmsg);
3443 }
3444
3445 static pthread_priority_compact_t
ipc_get_current_thread_priority(void)3446 ipc_get_current_thread_priority(void)
3447 {
3448 thread_t thread = current_thread();
3449 thread_qos_t qos;
3450 int relpri;
3451
3452 qos = thread_get_requested_qos(thread, &relpri);
3453 if (!qos) {
3454 qos = thread_user_promotion_qos_for_pri(thread->base_pri);
3455 relpri = 0;
3456 }
3457 return _pthread_priority_make_from_thread_qos(qos, relpri, 0);
3458 }
3459
3460 static kern_return_t
ipc_kmsg_set_qos(ipc_kmsg_t kmsg,mach_msg_option_t options,mach_msg_priority_t priority)3461 ipc_kmsg_set_qos(
3462 ipc_kmsg_t kmsg,
3463 mach_msg_option_t options,
3464 mach_msg_priority_t priority)
3465 {
3466 kern_return_t kr;
3467 mach_msg_header_t *hdr = ikm_header(kmsg);
3468 ipc_port_t special_reply_port = hdr->msgh_local_port;
3469 ipc_port_t dest_port = hdr->msgh_remote_port;
3470
3471 if ((options & MACH_SEND_OVERRIDE) &&
3472 !mach_msg_priority_is_pthread_priority(priority)) {
3473 mach_msg_qos_t qos = mach_msg_priority_qos(priority);
3474 int relpri = mach_msg_priority_relpri(priority);
3475 mach_msg_qos_t ovr = mach_msg_priority_overide_qos(priority);
3476
3477 kmsg->ikm_ppriority = _pthread_priority_make_from_thread_qos(qos, relpri, 0);
3478 kmsg->ikm_qos_override = MAX(qos, ovr);
3479 } else {
3480 #if CONFIG_VOUCHER_DEPRECATED
3481 kr = ipc_get_pthpriority_from_kmsg_voucher(kmsg, &kmsg->ikm_ppriority);
3482 #else
3483 kr = KERN_FAILURE;
3484 #endif /* CONFIG_VOUCHER_DEPRECATED */
3485 if (kr != KERN_SUCCESS) {
3486 if (options & MACH_SEND_PROPAGATE_QOS) {
3487 kmsg->ikm_ppriority = ipc_get_current_thread_priority();
3488 } else {
3489 kmsg->ikm_ppriority = MACH_MSG_PRIORITY_UNSPECIFIED;
3490 }
3491 }
3492
3493 if (options & MACH_SEND_OVERRIDE) {
3494 mach_msg_qos_t qos = _pthread_priority_thread_qos(kmsg->ikm_ppriority);
3495 mach_msg_qos_t ovr = _pthread_priority_thread_qos(priority);
3496 kmsg->ikm_qos_override = MAX(qos, ovr);
3497 } else {
3498 kmsg->ikm_qos_override = _pthread_priority_thread_qos(kmsg->ikm_ppriority);
3499 }
3500 }
3501
3502 kr = KERN_SUCCESS;
3503
3504 if (IP_VALID(special_reply_port) &&
3505 special_reply_port->ip_specialreply &&
3506 !ip_is_kobject(dest_port) &&
3507 MACH_MSGH_BITS_LOCAL(hdr->msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE) {
3508 boolean_t sync_bootstrap_checkin = !!(options & MACH_SEND_SYNC_BOOTSTRAP_CHECKIN);
3509 /*
3510 * Link the destination port to special reply port and make sure that
3511 * dest port has a send turnstile, else allocate one.
3512 */
3513 ipc_port_link_special_reply_port(special_reply_port, dest_port, sync_bootstrap_checkin);
3514 }
3515 return kr;
3516 }
3517
3518 static kern_return_t
ipc_kmsg_set_qos_kernel(ipc_kmsg_t kmsg)3519 ipc_kmsg_set_qos_kernel(
3520 ipc_kmsg_t kmsg)
3521 {
3522 ipc_port_t dest_port = ikm_header(kmsg)->msgh_remote_port;
3523 kmsg->ikm_qos_override = dest_port->ip_kernel_qos_override;
3524 kmsg->ikm_ppriority = _pthread_priority_make_from_thread_qos(kmsg->ikm_qos_override, 0, 0);
3525 return KERN_SUCCESS;
3526 }
3527
3528 /*
3529 * Routine: ipc_kmsg_link_reply_context_locked
3530 * Purpose:
3531 * Link any required context from the sending voucher
3532 * to the reply port. The ipc_kmsg_copyin_from_user function will
3533 * enforce that the sender calls mach_msg in this context.
3534 * Conditions:
3535 * reply port is locked
3536 */
3537 static void
ipc_kmsg_link_reply_context_locked(ipc_port_t reply_port,ipc_port_t voucher_port)3538 ipc_kmsg_link_reply_context_locked(
3539 ipc_port_t reply_port,
3540 ipc_port_t voucher_port)
3541 {
3542 kern_return_t __assert_only kr;
3543 uint32_t persona_id = 0;
3544 ipc_voucher_t voucher;
3545
3546 ip_mq_lock_held(reply_port);
3547
3548 if (!ip_active(reply_port)) {
3549 return;
3550 }
3551
3552 voucher = convert_port_to_voucher(voucher_port);
3553
3554 kr = bank_get_bank_ledger_thread_group_and_persona(voucher, NULL, NULL, &persona_id);
3555 assert(kr == KERN_SUCCESS);
3556 ipc_voucher_release(voucher);
3557
3558 if (persona_id == 0 || persona_id == PERSONA_ID_NONE) {
3559 /* there was no persona context to record */
3560 return;
3561 }
3562
3563 /*
3564 * Set the persona_id as the context on the reply port.
3565 * This will force the thread that replies to have adopted a voucher
3566 * with a matching persona.
3567 */
3568 reply_port->ip_reply_context = persona_id;
3569
3570 return;
3571 }
3572
3573 static kern_return_t
ipc_kmsg_validate_reply_port_locked(ipc_port_t reply_port,mach_msg_option_t options)3574 ipc_kmsg_validate_reply_port_locked(ipc_port_t reply_port, mach_msg_option_t options)
3575 {
3576 ip_mq_lock_held(reply_port);
3577
3578 if (!ip_active(reply_port)) {
3579 /*
3580 * Ideally, we would enforce that the reply receive right is
3581 * active, but asynchronous XPC cancellation destroys the
3582 * receive right, so we just have to return success here.
3583 */
3584 return KERN_SUCCESS;
3585 }
3586
3587 if (options & MACH_SEND_MSG) {
3588 /*
3589 * If the rely port is active, then it should not be
3590 * in-transit, and the receive right should be in the caller's
3591 * IPC space.
3592 */
3593 if (!ip_in_space(reply_port, current_task()->itk_space)) {
3594 return KERN_INVALID_CAPABILITY;
3595 }
3596
3597 /*
3598 * A port used as a reply port in an RPC should have exactly 1
3599 * extant send-once right which we either just made or are
3600 * moving as part of the IPC.
3601 */
3602 if (reply_port->ip_sorights != 1) {
3603 return KERN_INVALID_CAPABILITY;
3604 }
3605 /*
3606 * XPC uses an extra send-right to keep the name of the reply
3607 * right around through cancellation. That makes it harder to
3608 * enforce a particular semantic kere, so for now, we say that
3609 * you can have a maximum of 1 send right (in addition to your
3610 * send once right). In the future, it would be great to lock
3611 * this down even further.
3612 */
3613 if (reply_port->ip_srights > 1) {
3614 return KERN_INVALID_CAPABILITY;
3615 }
3616
3617 /*
3618 * The sender can also specify that the receive right should
3619 * be immovable. Note that this check only applies to
3620 * send-only operations. Combined send/receive or rcv-only
3621 * operations can specify an immovable receive right by
3622 * opt-ing into guarded descriptors (MACH_RCV_GUARDED_DESC)
3623 * and using the MACH_MSG_STRICT_REPLY options flag.
3624 */
3625 if (MACH_SEND_REPLY_IS_IMMOVABLE(options)) {
3626 if (!reply_port->ip_immovable_receive) {
3627 return KERN_INVALID_CAPABILITY;
3628 }
3629 }
3630 }
3631
3632 /*
3633 * don't enforce this yet: need a better way of indicating the
3634 * receiver wants this...
3635 */
3636 #if 0
3637 if (MACH_RCV_WITH_IMMOVABLE_REPLY(options)) {
3638 if (!reply_port->ip_immovable_receive) {
3639 return KERN_INVALID_CAPABILITY;
3640 }
3641 }
3642 #endif /* 0 */
3643
3644 return KERN_SUCCESS;
3645 }
3646
3647 /*
3648 * Routine: ipc_kmsg_validate_reply_context_locked
3649 * Purpose:
3650 * Validate that the current thread is running in the context
3651 * required by the destination port.
3652 * Conditions:
3653 * dest_port is locked
3654 * Returns:
3655 * MACH_MSG_SUCCESS on success.
3656 * On error, an EXC_GUARD exception is also raised.
3657 * This function *always* resets the port reply context.
3658 */
3659 static mach_msg_return_t
ipc_kmsg_validate_reply_context_locked(mach_msg_option_t option,ipc_port_t dest_port,ipc_voucher_t voucher,mach_port_name_t voucher_name)3660 ipc_kmsg_validate_reply_context_locked(
3661 mach_msg_option_t option,
3662 ipc_port_t dest_port,
3663 ipc_voucher_t voucher,
3664 mach_port_name_t voucher_name)
3665 {
3666 uint32_t dest_ctx = dest_port->ip_reply_context;
3667 dest_port->ip_reply_context = 0;
3668
3669 if (!ip_active(dest_port)) {
3670 return MACH_MSG_SUCCESS;
3671 }
3672
3673 if (voucher == IPC_VOUCHER_NULL || !MACH_PORT_VALID(voucher_name)) {
3674 if ((option & MACH_SEND_KERNEL) == 0) {
3675 mach_port_guard_exception(voucher_name, 0,
3676 (MPG_FLAGS_STRICT_REPLY_INVALID_VOUCHER | dest_ctx),
3677 kGUARD_EXC_STRICT_REPLY);
3678 }
3679 return MACH_SEND_INVALID_CONTEXT;
3680 }
3681
3682 kern_return_t __assert_only kr;
3683 uint32_t persona_id = 0;
3684 kr = bank_get_bank_ledger_thread_group_and_persona(voucher, NULL, NULL, &persona_id);
3685 assert(kr == KERN_SUCCESS);
3686
3687 if (dest_ctx != persona_id) {
3688 if ((option & MACH_SEND_KERNEL) == 0) {
3689 mach_port_guard_exception(voucher_name, 0,
3690 (MPG_FLAGS_STRICT_REPLY_MISMATCHED_PERSONA | ((((uint64_t)persona_id << 32) & MPG_FLAGS_STRICT_REPLY_MASK) | dest_ctx)),
3691 kGUARD_EXC_STRICT_REPLY);
3692 }
3693 return MACH_SEND_INVALID_CONTEXT;
3694 }
3695
3696 return MACH_MSG_SUCCESS;
3697 }
3698
3699 /*
3700 * Routine: ipc_kmsg_copyin_header
3701 * Purpose:
3702 * "Copy-in" port rights in the header of a message.
3703 * Operates atomically; if it doesn't succeed the
3704 * message header and the space are left untouched.
3705 * If it does succeed the remote/local port fields
3706 * contain object pointers instead of port names,
3707 * and the bits field is updated. The destination port
3708 * will be a valid port pointer.
3709 *
3710 * Conditions:
3711 * Nothing locked. May add MACH64_SEND_ALWAYS option.
3712 * Returns:
3713 * MACH_MSG_SUCCESS Successful copyin.
3714 * MACH_SEND_INVALID_HEADER
3715 * Illegal value in the message header bits.
3716 * MACH_SEND_INVALID_DEST The space is dead.
3717 * MACH_SEND_INVALID_DEST Can't copyin destination port.
3718 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
3719 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
3720 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
3721 */
3722
3723 static mach_msg_return_t
ipc_kmsg_copyin_header(ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_priority_t priority,mach_msg_option64_t * option64p)3724 ipc_kmsg_copyin_header(
3725 ipc_kmsg_t kmsg,
3726 ipc_space_t space,
3727 mach_msg_priority_t priority,
3728 mach_msg_option64_t *option64p)
3729 {
3730 mach_msg_header_t *msg = ikm_header(kmsg);
3731 mach_msg_bits_t mbits = msg->msgh_bits & MACH_MSGH_BITS_USER;
3732 mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(msg->msgh_remote_port);
3733 mach_port_name_t reply_name = CAST_MACH_PORT_TO_NAME(msg->msgh_local_port);
3734 mach_port_name_t voucher_name = MACH_PORT_NULL;
3735 kern_return_t kr;
3736
3737 mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
3738 mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
3739 mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
3740 ipc_object_t dest_port = IO_NULL;
3741 ipc_object_t reply_port = IO_NULL;
3742 ipc_port_t dest_soright = IP_NULL;
3743 ipc_port_t dport = IP_NULL;
3744 ipc_port_t reply_soright = IP_NULL;
3745 ipc_port_t voucher_soright = IP_NULL;
3746 ipc_port_t release_port = IP_NULL;
3747 ipc_port_t voucher_port = IP_NULL;
3748 ipc_port_t voucher_release_port = IP_NULL;
3749 ipc_entry_t dest_entry = IE_NULL;
3750 ipc_entry_t reply_entry = IE_NULL;
3751 ipc_entry_t voucher_entry = IE_NULL;
3752 ipc_object_copyin_flags_t dest_flags = IPC_OBJECT_COPYIN_FLAGS_ALLOW_REPLY_MAKE_SEND_ONCE | IPC_OBJECT_COPYIN_FLAGS_ALLOW_REPLY_MOVE_SEND_ONCE;
3753 ipc_object_copyin_flags_t reply_flags = IPC_OBJECT_COPYIN_FLAGS_ALLOW_REPLY_MAKE_SEND_ONCE;
3754 boolean_t reply_port_semantics_violation = FALSE;
3755
3756 int assertcnt = 0;
3757 mach_msg_option_t option32 = (mach_msg_option_t)*option64p;
3758 #if IMPORTANCE_INHERITANCE
3759 boolean_t needboost = FALSE;
3760 #endif /* IMPORTANCE_INHERITANCE */
3761
3762 if ((mbits != msg->msgh_bits) ||
3763 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type)) ||
3764 ((reply_type == 0) ?
3765 (reply_name != MACH_PORT_NULL) :
3766 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type))) {
3767 return MACH_SEND_INVALID_HEADER;
3768 }
3769
3770 if (!MACH_PORT_VALID(dest_name)) {
3771 return MACH_SEND_INVALID_DEST;
3772 }
3773
3774 is_write_lock(space);
3775 if (!is_active(space)) {
3776 is_write_unlock(space);
3777 return MACH_SEND_INVALID_DEST;
3778 }
3779 /* space locked and active */
3780
3781 /*
3782 * If there is a voucher specified, make sure the disposition is
3783 * valid and the entry actually refers to a voucher port. Don't
3784 * actually copy in until we validate destination and reply.
3785 */
3786 if (voucher_type != MACH_MSGH_BITS_ZERO) {
3787 voucher_name = msg->msgh_voucher_port;
3788
3789 if (voucher_name == MACH_PORT_DEAD ||
3790 (voucher_type != MACH_MSG_TYPE_MOVE_SEND &&
3791 voucher_type != MACH_MSG_TYPE_COPY_SEND)) {
3792 is_write_unlock(space);
3793 if ((option32 & MACH_SEND_KERNEL) == 0) {
3794 mach_port_guard_exception(voucher_name, 0, 0, kGUARD_EXC_SEND_INVALID_VOUCHER);
3795 }
3796 return MACH_SEND_INVALID_VOUCHER;
3797 }
3798
3799 if (voucher_name != MACH_PORT_NULL) {
3800 voucher_entry = ipc_entry_lookup(space, voucher_name);
3801 if (voucher_entry == IE_NULL ||
3802 (voucher_entry->ie_bits & MACH_PORT_TYPE_SEND) == 0 ||
3803 io_kotype(voucher_entry->ie_object) != IKOT_VOUCHER) {
3804 is_write_unlock(space);
3805 if ((option32 & MACH_SEND_KERNEL) == 0) {
3806 mach_port_guard_exception(voucher_name, 0, 0, kGUARD_EXC_SEND_INVALID_VOUCHER);
3807 }
3808 return MACH_SEND_INVALID_VOUCHER;
3809 }
3810 } else {
3811 voucher_type = MACH_MSG_TYPE_MOVE_SEND;
3812 }
3813 }
3814
3815 if (enforce_strict_reply && MACH_SEND_WITH_STRICT_REPLY(option32) &&
3816 (!MACH_PORT_VALID(reply_name) ||
3817 ((reply_type != MACH_MSG_TYPE_MAKE_SEND_ONCE) && (reply_type != MACH_MSG_TYPE_MOVE_SEND_ONCE))
3818 )) {
3819 /*
3820 * The caller cannot enforce a reply context with an invalid
3821 * reply port name, or a non-send_once reply disposition.
3822 */
3823 is_write_unlock(space);
3824 if ((option32 & MACH_SEND_KERNEL) == 0) {
3825 mach_port_guard_exception(reply_name, 0,
3826 (MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_DISP | reply_type),
3827 kGUARD_EXC_STRICT_REPLY);
3828 }
3829 return MACH_SEND_INVALID_REPLY;
3830 }
3831
3832 /*
3833 * Handle combinations of validating destination and reply; along
3834 * with copying in destination, reply, and voucher in an atomic way.
3835 */
3836
3837 if (dest_name == voucher_name) {
3838 /*
3839 * If the destination name is the same as the voucher name,
3840 * the voucher_entry must already be known. Either that or
3841 * the destination name is MACH_PORT_NULL (i.e. invalid).
3842 */
3843 dest_entry = voucher_entry;
3844 if (dest_entry == IE_NULL) {
3845 goto invalid_dest;
3846 }
3847
3848 /*
3849 * Make sure a future copyin of the reply port will succeed.
3850 * Once we start copying in the dest/voucher pair, we can't
3851 * back out.
3852 */
3853 if (MACH_PORT_VALID(reply_name)) {
3854 assert(reply_type != 0); /* because reply_name not null */
3855
3856 /* It is just WRONG if dest, voucher, and reply are all the same. */
3857 if (voucher_name == reply_name) {
3858 goto invalid_reply;
3859 }
3860 reply_entry = ipc_entry_lookup(space, reply_name);
3861 if (reply_entry == IE_NULL) {
3862 goto invalid_reply;
3863 }
3864 assert(dest_entry != reply_entry); /* names are not equal */
3865 if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type, dest_entry, &reply_port_semantics_violation)) {
3866 goto invalid_reply;
3867 }
3868 }
3869
3870 /*
3871 * Do the joint copyin of the dest disposition and
3872 * voucher disposition from the one entry/port. We
3873 * already validated that the voucher copyin would
3874 * succeed (above). So, any failure in combining
3875 * the copyins can be blamed on the destination.
3876 */
3877 kr = ipc_right_copyin_two(space, dest_name, dest_entry,
3878 dest_type, voucher_type, IPC_OBJECT_COPYIN_FLAGS_NONE, IPC_OBJECT_COPYIN_FLAGS_NONE,
3879 &dest_port, &dest_soright, &release_port);
3880 if (kr != KERN_SUCCESS) {
3881 assert(kr != KERN_INVALID_CAPABILITY);
3882 goto invalid_dest;
3883 }
3884 voucher_port = ip_object_to_port(dest_port);
3885
3886 /*
3887 * could not have been one of these dispositions,
3888 * validated the port was a true kernel voucher port above,
3889 * AND was successfully able to copyin both dest and voucher.
3890 */
3891 assert(dest_type != MACH_MSG_TYPE_MAKE_SEND);
3892 assert(dest_type != MACH_MSG_TYPE_MAKE_SEND_ONCE);
3893 assert(dest_type != MACH_MSG_TYPE_MOVE_SEND_ONCE);
3894
3895 /*
3896 * Perform the delayed reply right copyin (guaranteed success).
3897 */
3898 if (reply_entry != IE_NULL) {
3899 kr = ipc_right_copyin(space, reply_name, reply_entry,
3900 reply_type, IPC_OBJECT_COPYIN_FLAGS_DEADOK | reply_flags,
3901 &reply_port, &reply_soright,
3902 &release_port, &assertcnt, 0, NULL);
3903 assert(assertcnt == 0);
3904 assert(kr == KERN_SUCCESS);
3905 }
3906 } else {
3907 if (dest_name == reply_name) {
3908 /*
3909 * Destination and reply ports are the same!
3910 * This is very similar to the case where the
3911 * destination and voucher ports were the same
3912 * (except the reply port disposition is not
3913 * previously validated).
3914 */
3915 dest_entry = ipc_entry_lookup(space, dest_name);
3916 if (dest_entry == IE_NULL) {
3917 goto invalid_dest;
3918 }
3919
3920 reply_entry = dest_entry;
3921 assert(reply_type != 0); /* because name not null */
3922
3923 /*
3924 * Pre-validate that the reply right can be copied in by itself.
3925 * Fail if reply port is marked as immovable send.
3926 */
3927 if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type, dest_entry, &reply_port_semantics_violation)) {
3928 goto invalid_reply;
3929 }
3930
3931 /*
3932 * Do the joint copyin of the dest disposition and
3933 * reply disposition from the one entry/port.
3934 */
3935 kr = ipc_right_copyin_two(space, dest_name, dest_entry, dest_type, reply_type,
3936 dest_flags, reply_flags, &dest_port, &dest_soright, &release_port);
3937 if (kr == KERN_INVALID_CAPABILITY) {
3938 goto invalid_reply;
3939 } else if (kr != KERN_SUCCESS) {
3940 goto invalid_dest;
3941 }
3942 reply_port = dest_port;
3943 } else {
3944 /*
3945 * Handle destination and reply independently, as
3946 * they are independent entries (even if the entries
3947 * refer to the same port).
3948 *
3949 * This can be the tough case to make atomic.
3950 *
3951 * The difficult problem is serializing with port death.
3952 * The bad case is when dest_port dies after its copyin,
3953 * reply_port dies before its copyin, and dest_port dies before
3954 * reply_port. Then the copyins operated as if dest_port was
3955 * alive and reply_port was dead, which shouldn't have happened
3956 * because they died in the other order.
3957 *
3958 * Note that it is easy for a user task to tell if
3959 * a copyin happened before or after a port died.
3960 * If a port dies before copyin, a dead-name notification
3961 * is generated and the dead name's urefs are incremented,
3962 * and if the copyin happens first, a port-deleted
3963 * notification is generated.
3964 *
3965 * Even so, avoiding that potentially detectable race is too
3966 * expensive - and no known code cares about it. So, we just
3967 * do the expedient thing and copy them in one after the other.
3968 */
3969
3970 dest_entry = ipc_entry_lookup(space, dest_name);
3971 if (dest_entry == IE_NULL) {
3972 goto invalid_dest;
3973 }
3974 assert(dest_entry != voucher_entry);
3975
3976 /*
3977 * Make sure reply port entry is valid before dest copyin.
3978 */
3979 if (MACH_PORT_VALID(reply_name)) {
3980 if (reply_name == voucher_name) {
3981 goto invalid_reply;
3982 }
3983 reply_entry = ipc_entry_lookup(space, reply_name);
3984 if (reply_entry == IE_NULL) {
3985 goto invalid_reply;
3986 }
3987 assert(dest_entry != reply_entry); /* names are not equal */
3988 assert(reply_type != 0); /* because reply_name not null */
3989
3990 if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type, dest_entry, &reply_port_semantics_violation)) {
3991 goto invalid_reply;
3992 }
3993 }
3994
3995 /*
3996 * copyin the destination.
3997 */
3998 kr = ipc_right_copyin(space, dest_name, dest_entry, dest_type,
3999 (IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND | IPC_OBJECT_COPYIN_FLAGS_ALLOW_DEAD_SEND_ONCE | dest_flags),
4000 &dest_port, &dest_soright,
4001 &release_port, &assertcnt, 0, NULL);
4002 assert(assertcnt == 0);
4003 if (kr != KERN_SUCCESS) {
4004 goto invalid_dest;
4005 }
4006 assert(IO_VALID(dest_port));
4007 assert(!IP_VALID(release_port));
4008
4009 /*
4010 * Copyin the pre-validated reply right.
4011 * It's OK if the reply right has gone dead in the meantime.
4012 */
4013 if (MACH_PORT_VALID(reply_name)) {
4014 kr = ipc_right_copyin(space, reply_name, reply_entry,
4015 reply_type, IPC_OBJECT_COPYIN_FLAGS_DEADOK | reply_flags,
4016 &reply_port, &reply_soright,
4017 &release_port, &assertcnt, 0, NULL);
4018 assert(assertcnt == 0);
4019 assert(kr == KERN_SUCCESS);
4020 } else {
4021 /* convert invalid name to equivalent ipc_object type */
4022 reply_port = ip_to_object(CAST_MACH_NAME_TO_PORT(reply_name));
4023 }
4024 }
4025
4026 /*
4027 * Finally can copyin the voucher right now that dest and reply
4028 * are fully copied in (guaranteed success).
4029 */
4030 if (IE_NULL != voucher_entry) {
4031 kr = ipc_right_copyin(space, voucher_name, voucher_entry,
4032 voucher_type, IPC_OBJECT_COPYIN_FLAGS_NONE,
4033 (ipc_object_t *)&voucher_port,
4034 &voucher_soright,
4035 &voucher_release_port,
4036 &assertcnt, 0, NULL);
4037 assert(assertcnt == 0);
4038 assert(KERN_SUCCESS == kr);
4039 assert(IP_VALID(voucher_port));
4040 require_ip_active(voucher_port);
4041 }
4042 }
4043
4044 dest_type = ipc_object_copyin_type(dest_type);
4045 reply_type = ipc_object_copyin_type(reply_type);
4046
4047 dport = ip_object_to_port(dest_port);
4048 /*
4049 * If the dest port died, or is a kobject AND its receive right belongs to kernel,
4050 * allow copyin of immovable send rights in the message body (port descriptor) to
4051 * succeed since those send rights are simply "moved" or "copied" into kernel.
4052 *
4053 * See: ipc_object_copyin().
4054 */
4055
4056 ip_mq_lock(dport);
4057
4058 if (!ip_active(dport) || (ip_is_kobject(dport) &&
4059 ip_in_space(dport, ipc_space_kernel))) {
4060 assert(ip_kotype(dport) != IKOT_TIMER);
4061 kmsg->ikm_flags |= IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND;
4062 }
4063
4064 /*
4065 * JMM - Without rdar://problem/6275821, this is the last place we can
4066 * re-arm the send-possible notifications. It may trigger unexpectedly
4067 * early (send may NOT have failed), but better than missing. We assure
4068 * we won't miss by forcing MACH_SEND_ALWAYS if we got past arming.
4069 */
4070 if (((option32 & MACH_SEND_NOTIFY) != 0) &&
4071 dest_type != MACH_MSG_TYPE_PORT_SEND_ONCE &&
4072 dest_entry != IE_NULL && dest_entry->ie_request != IE_REQ_NONE) {
4073 /* dport still locked from above */
4074 if (ip_active(dport) && !ip_in_space(dport, ipc_space_kernel)) {
4075 /* dport could be in-transit, or in an ipc space */
4076 if (ip_full(dport)) {
4077 #if IMPORTANCE_INHERITANCE
4078 needboost = ipc_port_request_sparm(dport, dest_name,
4079 dest_entry->ie_request,
4080 option32,
4081 priority);
4082 if (needboost == FALSE) {
4083 ip_mq_unlock(dport);
4084 }
4085 #else
4086 ipc_port_request_sparm(dport, dest_name,
4087 dest_entry->ie_request,
4088 option32,
4089 priority);
4090 ip_mq_unlock(dport);
4091 #endif /* IMPORTANCE_INHERITANCE */
4092 } else {
4093 *option64p |= MACH64_SEND_ALWAYS;
4094 ip_mq_unlock(dport);
4095 }
4096 } else {
4097 ip_mq_unlock(dport);
4098 }
4099 } else {
4100 ip_mq_unlock(dport);
4101 }
4102 /* dport is unlocked, unless needboost == TRUE */
4103
4104 is_write_unlock(space);
4105
4106 #if IMPORTANCE_INHERITANCE
4107 /*
4108 * If our request is the first boosting send-possible
4109 * notification this cycle, push the boost down the
4110 * destination port.
4111 */
4112 if (needboost == TRUE) {
4113 /* dport still locked from above */
4114 if (ipc_port_importance_delta(dport, IPID_OPTION_SENDPOSSIBLE, 1) == FALSE) {
4115 ip_mq_unlock(dport);
4116 }
4117 }
4118 #endif /* IMPORTANCE_INHERITANCE */
4119
4120 /* dport is unlocked */
4121
4122 if (dest_soright != IP_NULL) {
4123 ipc_notify_port_deleted(dest_soright, dest_name);
4124 }
4125 if (reply_soright != IP_NULL) {
4126 ipc_notify_port_deleted(reply_soright, reply_name);
4127 }
4128 if (voucher_soright != IP_NULL) {
4129 ipc_notify_port_deleted(voucher_soright, voucher_name);
4130 }
4131
4132 /*
4133 * No room to store voucher port in in-kernel msg header,
4134 * so we store it back in the kmsg itself. Store original voucher
4135 * type there as well, but set the bits to the post-copyin type.
4136 */
4137 if (IP_VALID(voucher_port)) {
4138 ipc_kmsg_set_voucher_port(kmsg, voucher_port, voucher_type);
4139 voucher_type = MACH_MSG_TYPE_MOVE_SEND;
4140 }
4141
4142 msg->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, voucher_type, mbits);
4143 msg->msgh_remote_port = ip_object_to_port(dest_port);
4144 msg->msgh_local_port = ip_object_to_port(reply_port);
4145
4146 /*
4147 * capture the qos value(s) for the kmsg qos,
4148 * and apply any override before we enqueue the kmsg.
4149 */
4150 ipc_kmsg_set_qos(kmsg, option32, priority);
4151
4152 if (release_port != IP_NULL) {
4153 ip_release(release_port);
4154 }
4155
4156 if (voucher_release_port != IP_NULL) {
4157 ip_release(voucher_release_port);
4158 }
4159
4160 if (enforce_strict_reply && MACH_SEND_WITH_STRICT_REPLY(option32) &&
4161 IP_VALID(msg->msgh_local_port)) {
4162 /*
4163 * We've already validated that the reply disposition is a
4164 * [make/move] send-once. Ideally, we should enforce that the
4165 * reply port is also not dead, but XPC asynchronous
4166 * cancellation can make the reply port dead before we
4167 * actually make it to the mach_msg send.
4168 *
4169 * Here, we ensure that if we have a non-dead reply port, then
4170 * the reply port's receive right should not be in-transit,
4171 * and should live in the caller's IPC space.
4172 */
4173 ipc_port_t rport = msg->msgh_local_port;
4174 ip_mq_lock(rport);
4175 kr = ipc_kmsg_validate_reply_port_locked(rport, option32);
4176 ip_mq_unlock(rport);
4177 if (kr != KERN_SUCCESS) {
4178 /*
4179 * no descriptors have been copied in yet, but the
4180 * full header has been copied in: clean it up
4181 */
4182 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
4183 if ((option32 & MACH_SEND_KERNEL) == 0) {
4184 mach_port_guard_exception(reply_name, 0,
4185 (MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_PORT | kr),
4186 kGUARD_EXC_STRICT_REPLY);
4187 }
4188 return MACH_SEND_INVALID_REPLY;
4189 }
4190 }
4191
4192 if (reply_port_semantics_violation) {
4193 /* Currently rate limiting it to sucess paths only. */
4194 stash_reply_port_semantics_violations_telemetry();
4195 }
4196 return MACH_MSG_SUCCESS;
4197
4198 invalid_reply:
4199 is_write_unlock(space);
4200
4201 if (release_port != IP_NULL) {
4202 ip_release(release_port);
4203 }
4204
4205 assert(voucher_port == IP_NULL);
4206 assert(voucher_soright == IP_NULL);
4207
4208 if ((option32 & MACH_SEND_KERNEL) == 0) {
4209 mach_port_guard_exception(reply_name, 0, 0, kGUARD_EXC_SEND_INVALID_REPLY);
4210 }
4211 return MACH_SEND_INVALID_REPLY;
4212
4213 invalid_dest:
4214 is_write_unlock(space);
4215
4216 if (release_port != IP_NULL) {
4217 ip_release(release_port);
4218 }
4219
4220 if (reply_soright != IP_NULL) {
4221 ipc_notify_port_deleted(reply_soright, reply_name);
4222 }
4223
4224 assert(voucher_port == IP_NULL);
4225 assert(voucher_soright == IP_NULL);
4226
4227 return MACH_SEND_INVALID_DEST;
4228 }
4229
4230 static mach_msg_descriptor_t *
ipc_kmsg_copyin_port_descriptor(mach_msg_port_descriptor_t * dsc,mach_msg_user_port_descriptor_t * user_dsc_in,ipc_space_t space,ipc_object_t dest,ipc_kmsg_t kmsg,mach_msg_option_t options,mach_msg_return_t * mr)4231 ipc_kmsg_copyin_port_descriptor(
4232 mach_msg_port_descriptor_t *dsc,
4233 mach_msg_user_port_descriptor_t *user_dsc_in,
4234 ipc_space_t space,
4235 ipc_object_t dest,
4236 ipc_kmsg_t kmsg,
4237 mach_msg_option_t options,
4238 mach_msg_return_t *mr)
4239 {
4240 mach_msg_user_port_descriptor_t user_dsc = *user_dsc_in;
4241 mach_msg_type_name_t user_disp;
4242 mach_msg_type_name_t result_disp;
4243 mach_port_name_t name;
4244 ipc_object_t object;
4245
4246 user_disp = user_dsc.disposition;
4247 result_disp = ipc_object_copyin_type(user_disp);
4248
4249 name = (mach_port_name_t)user_dsc.name;
4250 if (MACH_PORT_VALID(name)) {
4251 kern_return_t kr = ipc_object_copyin(space, name, user_disp, &object, 0, NULL, kmsg->ikm_flags);
4252 if (kr != KERN_SUCCESS) {
4253 if (((options & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
4254 mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
4255 }
4256 *mr = MACH_SEND_INVALID_RIGHT;
4257 return NULL;
4258 }
4259
4260 if ((result_disp == MACH_MSG_TYPE_PORT_RECEIVE) &&
4261 ipc_port_check_circularity(ip_object_to_port(object),
4262 ip_object_to_port(dest))) {
4263 ikm_header(kmsg)->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4264 }
4265 dsc->name = ip_object_to_port(object);
4266 } else {
4267 dsc->name = CAST_MACH_NAME_TO_PORT(name);
4268 }
4269 dsc->disposition = result_disp;
4270 dsc->type = MACH_MSG_PORT_DESCRIPTOR;
4271
4272 dsc->pad_end = 0; // debug, unnecessary
4273
4274 return (mach_msg_descriptor_t *)(user_dsc_in + 1);
4275 }
4276
4277 static mach_msg_descriptor_t *
ipc_kmsg_copyin_ool_descriptor(mach_msg_ool_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,mach_vm_address_t * paddr,vm_map_copy_t * copy,vm_size_t * space_needed,vm_map_t map,mach_msg_return_t * mr)4278 ipc_kmsg_copyin_ool_descriptor(
4279 mach_msg_ool_descriptor_t *dsc,
4280 mach_msg_descriptor_t *user_dsc,
4281 int is_64bit,
4282 mach_vm_address_t *paddr,
4283 vm_map_copy_t *copy,
4284 vm_size_t *space_needed,
4285 vm_map_t map,
4286 mach_msg_return_t *mr)
4287 {
4288 vm_size_t length;
4289 boolean_t dealloc;
4290 mach_msg_copy_options_t copy_options;
4291 mach_vm_offset_t addr;
4292 mach_msg_descriptor_type_t dsc_type;
4293
4294 if (is_64bit) {
4295 mach_msg_ool_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4296
4297 addr = (mach_vm_offset_t) user_ool_dsc->address;
4298 length = user_ool_dsc->size;
4299 dealloc = user_ool_dsc->deallocate;
4300 copy_options = user_ool_dsc->copy;
4301 dsc_type = user_ool_dsc->type;
4302
4303 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
4304 } else {
4305 mach_msg_ool_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4306
4307 addr = CAST_USER_ADDR_T(user_ool_dsc->address);
4308 dealloc = user_ool_dsc->deallocate;
4309 copy_options = user_ool_dsc->copy;
4310 dsc_type = user_ool_dsc->type;
4311 length = user_ool_dsc->size;
4312
4313 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
4314 }
4315
4316 dsc->size = (mach_msg_size_t)length;
4317 dsc->deallocate = dealloc;
4318 dsc->copy = copy_options;
4319 dsc->type = dsc_type;
4320
4321 if (length == 0) {
4322 dsc->address = NULL;
4323 } else if (length > MSG_OOL_SIZE_SMALL &&
4324 (copy_options == MACH_MSG_PHYSICAL_COPY) && !dealloc) {
4325 /*
4326 * If the request is a physical copy and the source
4327 * is not being deallocated, then allocate space
4328 * in the kernel's pageable ipc copy map and copy
4329 * the data in. The semantics guarantee that the
4330 * data will have been physically copied before
4331 * the send operation terminates. Thus if the data
4332 * is not being deallocated, we must be prepared
4333 * to page if the region is sufficiently large.
4334 */
4335 if (copyin(addr, (char *)*paddr, length)) {
4336 *mr = MACH_SEND_INVALID_MEMORY;
4337 return NULL;
4338 }
4339
4340 /*
4341 * The kernel ipc copy map is marked no_zero_fill.
4342 * If the transfer is not a page multiple, we need
4343 * to zero fill the balance.
4344 */
4345 if (!page_aligned(length)) {
4346 (void) memset((void *) (*paddr + length), 0,
4347 round_page(length) - length);
4348 }
4349 if (vm_map_copyin(ipc_kernel_copy_map, (vm_map_address_t)*paddr,
4350 (vm_map_size_t)length, TRUE, copy) != KERN_SUCCESS) {
4351 *mr = MACH_MSG_VM_KERNEL;
4352 return NULL;
4353 }
4354 dsc->address = (void *)*copy;
4355 *paddr += round_page(length);
4356 *space_needed -= round_page(length);
4357 } else {
4358 /*
4359 * Make a vm_map_copy_t of the of the data. If the
4360 * data is small, this will do an optimized physical
4361 * copy. Otherwise, it will do a virtual copy.
4362 *
4363 * NOTE: A virtual copy is OK if the original is being
4364 * deallocted, even if a physical copy was requested.
4365 */
4366 kern_return_t kr = vm_map_copyin(map, addr,
4367 (vm_map_size_t)length, dealloc, copy);
4368 if (kr != KERN_SUCCESS) {
4369 *mr = (kr == KERN_RESOURCE_SHORTAGE) ?
4370 MACH_MSG_VM_KERNEL :
4371 MACH_SEND_INVALID_MEMORY;
4372 return NULL;
4373 }
4374 dsc->address = (void *)*copy;
4375 }
4376
4377 return user_dsc;
4378 }
4379
4380 static mach_msg_descriptor_t *
ipc_kmsg_copyin_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,vm_map_t map,ipc_space_t space,ipc_object_t dest,ipc_kmsg_t kmsg,mach_msg_option_t options,mach_msg_return_t * mr)4381 ipc_kmsg_copyin_ool_ports_descriptor(
4382 mach_msg_ool_ports_descriptor_t *dsc,
4383 mach_msg_descriptor_t *user_dsc,
4384 int is_64bit,
4385 vm_map_t map,
4386 ipc_space_t space,
4387 ipc_object_t dest,
4388 ipc_kmsg_t kmsg,
4389 mach_msg_option_t options,
4390 mach_msg_return_t *mr)
4391 {
4392 void *data;
4393 ipc_object_t *objects;
4394 unsigned int i;
4395 mach_vm_offset_t addr;
4396 mach_msg_type_name_t user_disp;
4397 mach_msg_type_name_t result_disp;
4398 mach_msg_type_number_t count;
4399 mach_msg_copy_options_t copy_option;
4400 boolean_t deallocate;
4401 mach_msg_descriptor_type_t type;
4402 vm_size_t ports_length, names_length;
4403
4404 if (is_64bit) {
4405 mach_msg_ool_ports_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4406
4407 addr = (mach_vm_offset_t)user_ool_dsc->address;
4408 count = user_ool_dsc->count;
4409 deallocate = user_ool_dsc->deallocate;
4410 copy_option = user_ool_dsc->copy;
4411 user_disp = user_ool_dsc->disposition;
4412 type = user_ool_dsc->type;
4413
4414 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
4415 } else {
4416 mach_msg_ool_ports_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4417
4418 addr = CAST_USER_ADDR_T(user_ool_dsc->address);
4419 count = user_ool_dsc->count;
4420 deallocate = user_ool_dsc->deallocate;
4421 copy_option = user_ool_dsc->copy;
4422 user_disp = user_ool_dsc->disposition;
4423 type = user_ool_dsc->type;
4424
4425 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
4426 }
4427
4428 dsc->deallocate = deallocate;
4429 dsc->copy = copy_option;
4430 dsc->type = type;
4431 dsc->count = count;
4432 dsc->address = NULL; /* for now */
4433
4434 result_disp = ipc_object_copyin_type(user_disp);
4435 dsc->disposition = result_disp;
4436
4437 /* We always do a 'physical copy', but you have to specify something valid */
4438 if (copy_option != MACH_MSG_PHYSICAL_COPY &&
4439 copy_option != MACH_MSG_VIRTUAL_COPY) {
4440 *mr = MACH_SEND_INVALID_TYPE;
4441 return NULL;
4442 }
4443
4444 /* calculate length of data in bytes, rounding up */
4445
4446 if (os_mul_overflow(count, sizeof(mach_port_t), &ports_length)) {
4447 *mr = MACH_SEND_TOO_LARGE;
4448 return NULL;
4449 }
4450
4451 if (os_mul_overflow(count, sizeof(mach_port_name_t), &names_length)) {
4452 *mr = MACH_SEND_TOO_LARGE;
4453 return NULL;
4454 }
4455
4456 if (ports_length == 0) {
4457 return user_dsc;
4458 }
4459
4460 data = kalloc_type(mach_port_t, count, Z_WAITOK);
4461
4462 if (data == NULL) {
4463 *mr = MACH_SEND_NO_BUFFER;
4464 return NULL;
4465 }
4466
4467 #ifdef __LP64__
4468 mach_port_name_t *names = &((mach_port_name_t *)data)[count];
4469 #else
4470 mach_port_name_t *names = ((mach_port_name_t *)data);
4471 #endif
4472
4473 if (copyinmap(map, addr, names, names_length) != KERN_SUCCESS) {
4474 kfree_type(mach_port_t, count, data);
4475 *mr = MACH_SEND_INVALID_MEMORY;
4476 return NULL;
4477 }
4478
4479 if (deallocate) {
4480 (void) mach_vm_deallocate(map, addr, (mach_vm_size_t)names_length);
4481 }
4482
4483 objects = (ipc_object_t *) data;
4484 dsc->address = data;
4485
4486 for (i = 0; i < count; i++) {
4487 mach_port_name_t name = names[i];
4488 ipc_object_t object;
4489
4490 if (!MACH_PORT_VALID(name)) {
4491 objects[i] = ip_to_object(CAST_MACH_NAME_TO_PORT(name));
4492 continue;
4493 }
4494
4495 kern_return_t kr = ipc_object_copyin(space, name, user_disp, &object, 0, NULL, kmsg->ikm_flags);
4496
4497 if (kr != KERN_SUCCESS) {
4498 unsigned int j;
4499
4500 for (j = 0; j < i; j++) {
4501 object = objects[j];
4502 if (IPC_OBJECT_VALID(object)) {
4503 ipc_object_destroy(object, result_disp);
4504 }
4505 }
4506 kfree_type(mach_port_t, count, data);
4507 dsc->address = NULL;
4508 if (((options & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
4509 mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
4510 }
4511 *mr = MACH_SEND_INVALID_RIGHT;
4512 return NULL;
4513 }
4514
4515 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4516 ipc_port_check_circularity(ip_object_to_port(object),
4517 ip_object_to_port(dest))) {
4518 ikm_header(kmsg)->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4519 }
4520
4521 objects[i] = object;
4522 }
4523
4524 return user_dsc;
4525 }
4526
4527 static mach_msg_descriptor_t *
ipc_kmsg_copyin_guarded_port_descriptor(mach_msg_guarded_port_descriptor_t * dsc,mach_msg_descriptor_t * user_addr,int is_64bit,ipc_space_t space,ipc_object_t dest,ipc_kmsg_t kmsg,mach_msg_option_t options,mach_msg_return_t * mr)4528 ipc_kmsg_copyin_guarded_port_descriptor(
4529 mach_msg_guarded_port_descriptor_t *dsc,
4530 mach_msg_descriptor_t *user_addr,
4531 int is_64bit,
4532 ipc_space_t space,
4533 ipc_object_t dest,
4534 ipc_kmsg_t kmsg,
4535 mach_msg_option_t options,
4536 mach_msg_return_t *mr)
4537 {
4538 mach_msg_descriptor_t *user_dsc;
4539 mach_msg_type_name_t disp;
4540 mach_msg_type_name_t result_disp;
4541 mach_port_name_t name;
4542 mach_msg_guard_flags_t guard_flags;
4543 ipc_object_t object;
4544 mach_port_context_t context;
4545
4546 if (!is_64bit) {
4547 mach_msg_guarded_port_descriptor32_t *user_gp_dsc = (typeof(user_gp_dsc))user_addr;
4548 name = user_gp_dsc->name;
4549 guard_flags = user_gp_dsc->flags;
4550 disp = user_gp_dsc->disposition;
4551 context = user_gp_dsc->context;
4552 user_dsc = (mach_msg_descriptor_t *)(user_gp_dsc + 1);
4553 } else {
4554 mach_msg_guarded_port_descriptor64_t *user_gp_dsc = (typeof(user_gp_dsc))user_addr;
4555 name = user_gp_dsc->name;
4556 guard_flags = user_gp_dsc->flags;
4557 disp = user_gp_dsc->disposition;
4558 context = user_gp_dsc->context;
4559 user_dsc = (mach_msg_descriptor_t *)(user_gp_dsc + 1);
4560 }
4561
4562 guard_flags &= MACH_MSG_GUARD_FLAGS_MASK;
4563 result_disp = ipc_object_copyin_type(disp);
4564
4565 if (MACH_PORT_VALID(name)) {
4566 kern_return_t kr = ipc_object_copyin(space, name, disp, &object, context, &guard_flags, kmsg->ikm_flags);
4567 if (kr != KERN_SUCCESS) {
4568 if (((options & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
4569 mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
4570 }
4571 *mr = MACH_SEND_INVALID_RIGHT;
4572 return NULL;
4573 }
4574
4575 if ((result_disp == MACH_MSG_TYPE_PORT_RECEIVE) &&
4576 ipc_port_check_circularity(ip_object_to_port(object),
4577 ip_object_to_port(dest))) {
4578 ikm_header(kmsg)->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4579 }
4580 dsc->name = ip_object_to_port(object);
4581 } else {
4582 dsc->name = CAST_MACH_NAME_TO_PORT(name);
4583 }
4584 dsc->flags = guard_flags;
4585 dsc->disposition = result_disp;
4586 dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
4587
4588 #if __LP64__
4589 dsc->pad_end = 0; // debug, unnecessary
4590 #endif
4591
4592 return user_dsc;
4593 }
4594
4595
4596 /*
4597 * Routine: ipc_kmsg_copyin_body
4598 * Purpose:
4599 * "Copy-in" port rights and out-of-line memory
4600 * in the message body.
4601 *
4602 * In all failure cases, the message is left holding
4603 * no rights or memory. However, the message buffer
4604 * is not deallocated. If successful, the message
4605 * contains a valid destination port.
4606 * Conditions:
4607 * Nothing locked.
4608 * Returns:
4609 * MACH_MSG_SUCCESS Successful copyin.
4610 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
4611 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
4612 * MACH_SEND_INVALID_TYPE Bad type specification.
4613 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
4614 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
4615 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
4616 * MACH_SEND_NO_GRANT_DEST Dest port doesn't accept ports in body
4617 */
4618
4619 static mach_msg_return_t
ipc_kmsg_copyin_body(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_option_t options)4620 ipc_kmsg_copyin_body(
4621 ipc_kmsg_t kmsg,
4622 ipc_space_t space,
4623 vm_map_t map,
4624 mach_msg_option_t options)
4625 {
4626 ipc_object_t dest;
4627 mach_msg_body_t *body;
4628 mach_msg_descriptor_t *daddr;
4629 mach_msg_descriptor_t *user_addr, *kern_addr;
4630 mach_msg_type_number_t dsc_count;
4631 boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
4632 boolean_t contains_port_desc = FALSE;
4633 vm_size_t space_needed = 0;
4634 mach_vm_address_t paddr = 0;
4635 __assert_only vm_offset_t end;
4636 vm_map_copy_t copy = VM_MAP_COPY_NULL;
4637 mach_msg_return_t mr = MACH_MSG_SUCCESS;
4638 mach_msg_header_t *hdr = ikm_header(kmsg);
4639
4640 ipc_port_t remote_port = hdr->msgh_remote_port;
4641
4642 vm_size_t descriptor_size = 0;
4643
4644 mach_msg_type_number_t total_ool_port_count = 0;
4645 mach_msg_guard_flags_t guard_flags = 0;
4646 mach_port_context_t context;
4647 mach_msg_type_name_t disp;
4648
4649 /*
4650 * Determine if the target is a kernel port.
4651 */
4652 dest = ip_to_object(remote_port);
4653 body = (mach_msg_body_t *) (hdr + 1);
4654 daddr = (mach_msg_descriptor_t *) (body + 1);
4655
4656 dsc_count = body->msgh_descriptor_count;
4657 if (dsc_count == 0) {
4658 return MACH_MSG_SUCCESS;
4659 }
4660
4661 assert(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX);
4662 end = (vm_offset_t)hdr + sizeof(mach_msg_base_t) +
4663 dsc_count * KERNEL_DESC_SIZE;
4664
4665 /*
4666 * Make an initial pass to determine kernal VM space requirements for
4667 * physical copies and possible contraction of the descriptors from
4668 * processes with pointers larger than the kernel's.
4669 */
4670 for (mach_msg_type_number_t i = 0; i < dsc_count; i++) {
4671 mach_msg_size_t dsize;
4672 mach_msg_size_t size;
4673 mach_msg_type_number_t ool_port_count = 0;
4674
4675 dsize = ikm_user_desc_size(daddr->type.type, is_task_64bit);
4676 /* descriptor size check has been hoisted to ikm_check_descriptors() */
4677 assert((vm_offset_t)daddr + dsize <= end);
4678
4679 switch (daddr->type.type) {
4680 case MACH_MSG_OOL_DESCRIPTOR:
4681 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
4682 size = (is_task_64bit) ?
4683 ((mach_msg_ool_descriptor64_t *)daddr)->size :
4684 daddr->out_of_line.size;
4685
4686 if (daddr->out_of_line.copy != MACH_MSG_PHYSICAL_COPY &&
4687 daddr->out_of_line.copy != MACH_MSG_VIRTUAL_COPY) {
4688 /*
4689 * Invalid copy option
4690 */
4691 mr = MACH_SEND_INVALID_TYPE;
4692 goto clean_message;
4693 }
4694
4695 if (size > MSG_OOL_SIZE_SMALL &&
4696 (daddr->out_of_line.copy == MACH_MSG_PHYSICAL_COPY) &&
4697 !(daddr->out_of_line.deallocate)) {
4698 /*
4699 * Out-of-line memory descriptor, accumulate kernel
4700 * memory requirements
4701 */
4702 if (space_needed + round_page(size) <= space_needed) {
4703 /* Overflow dectected */
4704 mr = MACH_MSG_VM_KERNEL;
4705 goto clean_message;
4706 }
4707
4708 space_needed += round_page(size);
4709 if (space_needed > ipc_kmsg_max_vm_space) {
4710 /* Per message kernel memory limit exceeded */
4711 mr = MACH_MSG_VM_KERNEL;
4712 goto clean_message;
4713 }
4714 }
4715 break;
4716 case MACH_MSG_PORT_DESCRIPTOR:
4717 if (os_add_overflow(total_ool_port_count, 1, &total_ool_port_count)) {
4718 /* Overflow detected */
4719 mr = MACH_SEND_TOO_LARGE;
4720 goto clean_message;
4721 }
4722 contains_port_desc = TRUE;
4723 break;
4724 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
4725 ool_port_count = (is_task_64bit) ?
4726 ((mach_msg_ool_ports_descriptor64_t *)daddr)->count :
4727 daddr->ool_ports.count;
4728
4729 if (os_add_overflow(total_ool_port_count, ool_port_count, &total_ool_port_count)) {
4730 /* Overflow detected */
4731 mr = MACH_SEND_TOO_LARGE;
4732 goto clean_message;
4733 }
4734
4735 if (ool_port_count > (ipc_kmsg_max_vm_space / sizeof(mach_port_t))) {
4736 /* Per message kernel memory limit exceeded */
4737 mr = MACH_SEND_TOO_LARGE;
4738 goto clean_message;
4739 }
4740 contains_port_desc = TRUE;
4741 break;
4742 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
4743 guard_flags = (is_task_64bit) ?
4744 ((mach_msg_guarded_port_descriptor64_t *)daddr)->flags :
4745 ((mach_msg_guarded_port_descriptor32_t *)daddr)->flags;
4746 context = (is_task_64bit) ?
4747 ((mach_msg_guarded_port_descriptor64_t *)daddr)->context :
4748 ((mach_msg_guarded_port_descriptor32_t *)daddr)->context;
4749 disp = (is_task_64bit) ?
4750 ((mach_msg_guarded_port_descriptor64_t *)daddr)->disposition :
4751 ((mach_msg_guarded_port_descriptor32_t *)daddr)->disposition;
4752
4753 /* Only MACH_MSG_TYPE_MOVE_RECEIVE is supported for now */
4754 if (!guard_flags || ((guard_flags & ~MACH_MSG_GUARD_FLAGS_MASK) != 0) ||
4755 ((guard_flags & MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND) && (context != 0)) ||
4756 (disp != MACH_MSG_TYPE_MOVE_RECEIVE)) {
4757 /*
4758 * Invalid guard flags, context or disposition
4759 */
4760 mr = MACH_SEND_INVALID_TYPE;
4761 goto clean_message;
4762 }
4763 if (os_add_overflow(total_ool_port_count, 1, &total_ool_port_count)) {
4764 /* Overflow detected */
4765 mr = MACH_SEND_TOO_LARGE;
4766 goto clean_message;
4767 }
4768 contains_port_desc = TRUE;
4769 break;
4770 default:
4771 /* descriptor type check has been hoisted to ikm_check_descriptors() */
4772 panic("invalid descriptor type");
4773 }
4774
4775 descriptor_size += dsize;
4776 daddr = (typeof(daddr))((vm_offset_t)daddr + dsize);
4777 }
4778
4779 /* Sending more than 16383 rights in one message seems crazy */
4780 if (total_ool_port_count >= (MACH_PORT_UREFS_MAX / 4)) {
4781 mr = MACH_SEND_TOO_LARGE;
4782 goto clean_message;
4783 }
4784
4785 /*
4786 * Check if dest is a no-grant port; Since this bit is set only on
4787 * port construction and cannot be unset later, we can peek at the
4788 * bit without paying the cost of locking the port.
4789 */
4790 if (contains_port_desc && remote_port->ip_no_grant) {
4791 mr = MACH_SEND_NO_GRANT_DEST;
4792 goto clean_message;
4793 }
4794
4795 /*
4796 * Allocate space in the pageable kernel ipc copy map for all the
4797 * ool data that is to be physically copied. Map is marked wait for
4798 * space.
4799 */
4800 if (space_needed) {
4801 if (mach_vm_allocate_kernel(ipc_kernel_copy_map, &paddr, space_needed,
4802 VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IPC) != KERN_SUCCESS) {
4803 mr = MACH_MSG_VM_KERNEL;
4804 goto clean_message;
4805 }
4806 }
4807
4808 /* kern_addr = just after base as it was copied in */
4809 kern_addr = (mach_msg_descriptor_t *)((vm_offset_t)hdr +
4810 sizeof(mach_msg_base_t));
4811
4812 /*
4813 * Shift memory after mach_msg_base_t to make room for dsc_count * 16bytes
4814 * of descriptors on 64 bit kernels
4815 */
4816 vm_offset_t dsc_adjust = KERNEL_DESC_SIZE * dsc_count - descriptor_size;
4817
4818 if (descriptor_size != KERNEL_DESC_SIZE * dsc_count) {
4819 if (ikm_is_linear(kmsg)) {
4820 memmove((char *)(((vm_offset_t)hdr) + sizeof(mach_msg_base_t) + dsc_adjust),
4821 (void *)((vm_offset_t)hdr + sizeof(mach_msg_base_t)),
4822 hdr->msgh_size - sizeof(mach_msg_base_t));
4823 } else {
4824 /* just memmove the descriptors following the header */
4825 memmove((char *)(((vm_offset_t)hdr) + sizeof(mach_msg_base_t) + dsc_adjust),
4826 (void *)((vm_offset_t)hdr + sizeof(mach_msg_base_t)),
4827 ikm_total_desc_size(kmsg, current_map(), 0, 0, true));
4828 }
4829
4830 /* Update the message size for the larger in-kernel representation */
4831 hdr->msgh_size += (mach_msg_size_t)dsc_adjust;
4832 }
4833
4834
4835 /* user_addr = just after base after it has been (conditionally) moved */
4836 user_addr = (mach_msg_descriptor_t *)((vm_offset_t)hdr +
4837 sizeof(mach_msg_base_t) + dsc_adjust);
4838
4839 /*
4840 * Receive right of a libxpc connection port is moved as a part of kmsg's body
4841 * 1. from a client to a service during connection etsablishment.
4842 * 2. back to the client on service's death or port deallocation.
4843 *
4844 * Any other attempt to move this receive right is not allowed.
4845 */
4846 kmsg->ikm_flags |= IPC_OBJECT_COPYIN_FLAGS_ALLOW_CONN_IMMOVABLE_RECEIVE;
4847
4848 /* handle the OOL regions and port descriptors. */
4849 for (mach_msg_type_number_t copied_in_dscs = 0;
4850 copied_in_dscs < dsc_count; copied_in_dscs++) {
4851 switch (user_addr->type.type) {
4852 case MACH_MSG_PORT_DESCRIPTOR:
4853 user_addr = ipc_kmsg_copyin_port_descriptor((mach_msg_port_descriptor_t *)kern_addr,
4854 (mach_msg_user_port_descriptor_t *)user_addr, space, dest, kmsg, options, &mr);
4855 kern_addr++;
4856 break;
4857 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
4858 case MACH_MSG_OOL_DESCRIPTOR:
4859 user_addr = ipc_kmsg_copyin_ool_descriptor((mach_msg_ool_descriptor_t *)kern_addr,
4860 user_addr, is_task_64bit, &paddr, ©, &space_needed, map, &mr);
4861 kern_addr++;
4862 break;
4863 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
4864 user_addr = ipc_kmsg_copyin_ool_ports_descriptor((mach_msg_ool_ports_descriptor_t *)kern_addr,
4865 user_addr, is_task_64bit, map, space, dest, kmsg, options, &mr);
4866 kern_addr++;
4867 break;
4868 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
4869 user_addr = ipc_kmsg_copyin_guarded_port_descriptor((mach_msg_guarded_port_descriptor_t *)kern_addr,
4870 user_addr, is_task_64bit, space, dest, kmsg, options, &mr);
4871 kern_addr++;
4872 break;
4873 default:
4874 panic("invalid descriptor type %d", user_addr->type.type);
4875 }
4876
4877 if (MACH_MSG_SUCCESS != mr) {
4878 /* clean from start of message descriptors to copied_in_dscs */
4879 ipc_kmsg_clean_partial(kmsg, copied_in_dscs,
4880 (mach_msg_descriptor_t *)((mach_msg_base_t *)hdr + 1),
4881 paddr, space_needed);
4882 goto out;
4883 }
4884 } /* End of loop */
4885
4886 out:
4887 return mr;
4888
4889 clean_message:
4890 /* no descriptors have been copied in yet */
4891 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
4892 return mr;
4893 }
4894
4895 #define MACH_BOOTSTRAP_PORT_MSG_ID_MASK ((1ul << 24) - 1)
4896
4897 /*
4898 * Routine: ipc_kmsg_copyin_from_user
4899 * Purpose:
4900 * "Copy-in" port rights and out-of-line memory
4901 * in the message.
4902 *
4903 * In all failure cases, the message is left holding
4904 * no rights or memory. However, the message buffer
4905 * is not deallocated. If successful, the message
4906 * contains a valid destination port.
4907 * Conditions:
4908 * Nothing locked.
4909 * Returns:
4910 * MACH_MSG_SUCCESS Successful copyin.
4911 * MACH_SEND_INVALID_HEADER Illegal value in the message header bits.
4912 * MACH_SEND_INVALID_DEST Can't copyin destination port.
4913 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
4914 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
4915 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
4916 * MACH_SEND_INVALID_TYPE Bad type specification.
4917 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
4918 */
4919
4920 mach_msg_return_t
ipc_kmsg_copyin_from_user(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_priority_t priority,mach_msg_option64_t * option64p,bool filter_nonfatal)4921 ipc_kmsg_copyin_from_user(
4922 ipc_kmsg_t kmsg,
4923 ipc_space_t space,
4924 vm_map_t map,
4925 mach_msg_priority_t priority,
4926 mach_msg_option64_t *option64p,
4927 bool filter_nonfatal)
4928 {
4929 mach_msg_return_t mr;
4930 mach_msg_header_t *hdr = ikm_header(kmsg);
4931 mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(hdr->msgh_remote_port);
4932
4933 hdr->msgh_bits &= MACH_MSGH_BITS_USER;
4934
4935 mr = ipc_kmsg_copyin_header(kmsg, space, priority, option64p);
4936 /* copyin_header may add MACH64_SEND_ALWAYS option */
4937
4938 if (mr != MACH_MSG_SUCCESS) {
4939 return mr;
4940 }
4941
4942 /* Get the message filter policy if the task and port support filtering */
4943 mach_msg_filter_id fid = 0;
4944 mach_port_t remote_port = hdr->msgh_remote_port;
4945 mach_msg_id_t msg_id = hdr->msgh_id;
4946 void * sblabel = NULL;
4947
4948 if (mach_msg_filter_at_least(MACH_MSG_FILTER_CALLBACKS_VERSION_1) &&
4949 task_get_filter_msg_flag(current_task()) &&
4950 ip_enforce_msg_filtering(remote_port)) {
4951 ip_mq_lock(remote_port);
4952 if (ip_active(remote_port)) {
4953 if (remote_port->ip_service_port) {
4954 ipc_service_port_label_t label = remote_port->ip_splabel;
4955 sblabel = label->ispl_sblabel;
4956 if (label && ipc_service_port_label_is_bootstrap_port(label)) {
4957 /*
4958 * Mask the top byte for messages sent to launchd's bootstrap port.
4959 * Filter any messages with domain 0 (as they correspond to MIG
4960 * based messages)
4961 */
4962 unsigned msg_protocol = msg_id & ~MACH_BOOTSTRAP_PORT_MSG_ID_MASK;
4963 if (!msg_protocol) {
4964 ip_mq_unlock(remote_port);
4965 goto filtered_msg;
4966 }
4967 msg_id = msg_id & MACH_BOOTSTRAP_PORT_MSG_ID_MASK;
4968 }
4969 } else {
4970 assert(!ip_is_kolabeled(remote_port));
4971 /* Connection ports can also have send-side message filters */
4972 sblabel = remote_port->ip_splabel;
4973 }
4974 if (sblabel) {
4975 mach_msg_filter_retain_sblabel_callback(sblabel);
4976 }
4977 }
4978 ip_mq_unlock(remote_port);
4979
4980 if (sblabel && !mach_msg_fetch_filter_policy(sblabel, msg_id, &fid)) {
4981 goto filtered_msg;
4982 }
4983 }
4984
4985 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_SEND) | DBG_FUNC_NONE,
4986 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
4987 (uintptr_t)hdr->msgh_bits,
4988 (uintptr_t)hdr->msgh_id,
4989 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg))),
4990 0);
4991
4992 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_copyin_from_user header:\n%.8x\n%.8x\n%p\n%p\n%p\n%.8x\n",
4993 hdr->msgh_size,
4994 hdr->msgh_bits,
4995 hdr->msgh_remote_port,
4996 hdr->msgh_local_port,
4997 ipc_kmsg_get_voucher_port(kmsg),
4998 hdr->msgh_id);
4999
5000 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
5001 mr = ipc_kmsg_copyin_body(kmsg, space, map, (mach_msg_option_t)*option64p);
5002 }
5003
5004 /* Sign the message contents */
5005 if (mr == MACH_MSG_SUCCESS) {
5006 ipc_kmsg_init_trailer(kmsg, current_task());
5007 ikm_sign(kmsg);
5008 }
5009
5010 return mr;
5011
5012 filtered_msg:
5013 if (!filter_nonfatal) {
5014 mach_port_guard_exception(dest_name, 0, 0, kGUARD_EXC_MSG_FILTERED);
5015 }
5016 /* no descriptors have been copied in yet */
5017 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
5018 return MACH_SEND_MSG_FILTERED;
5019 }
5020
5021 /*
5022 * Routine: ipc_kmsg_copyin_from_kernel
5023 * Purpose:
5024 * "Copy-in" port rights and out-of-line memory
5025 * in a message sent from the kernel.
5026 *
5027 * Because the message comes from the kernel,
5028 * the implementation assumes there are no errors
5029 * or peculiarities in the message.
5030 * Conditions:
5031 * Nothing locked.
5032 */
5033
5034 mach_msg_return_t
ipc_kmsg_copyin_from_kernel(ipc_kmsg_t kmsg)5035 ipc_kmsg_copyin_from_kernel(
5036 ipc_kmsg_t kmsg)
5037 {
5038 mach_msg_header_t *hdr = ikm_header(kmsg);
5039 mach_msg_bits_t bits = hdr->msgh_bits;
5040 mach_msg_type_name_t rname = MACH_MSGH_BITS_REMOTE(bits);
5041 mach_msg_type_name_t lname = MACH_MSGH_BITS_LOCAL(bits);
5042 mach_msg_type_name_t vname = MACH_MSGH_BITS_VOUCHER(bits);
5043 ipc_object_t remote = ip_to_object(hdr->msgh_remote_port);
5044 ipc_object_t local = ip_to_object(hdr->msgh_local_port);
5045 ipc_object_t voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
5046 ipc_port_t dest = hdr->msgh_remote_port;
5047
5048 /* translate the destination and reply ports */
5049 if (!IO_VALID(remote)) {
5050 return MACH_SEND_INVALID_DEST;
5051 }
5052
5053 ipc_object_copyin_from_kernel(remote, rname);
5054 if (IO_VALID(local)) {
5055 ipc_object_copyin_from_kernel(local, lname);
5056 }
5057
5058 if (IO_VALID(voucher)) {
5059 ipc_object_copyin_from_kernel(voucher, vname);
5060 }
5061
5062 /*
5063 * The common case is a complex message with no reply port,
5064 * because that is what the memory_object interface uses.
5065 */
5066
5067 if (bits == (MACH_MSGH_BITS_COMPLEX |
5068 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0))) {
5069 bits = (MACH_MSGH_BITS_COMPLEX |
5070 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0));
5071
5072 hdr->msgh_bits = bits;
5073 } else {
5074 bits = (MACH_MSGH_BITS_OTHER(bits) |
5075 MACH_MSGH_BITS_SET_PORTS(ipc_object_copyin_type(rname),
5076 ipc_object_copyin_type(lname), ipc_object_copyin_type(vname)));
5077
5078 hdr->msgh_bits = bits;
5079 }
5080
5081 ipc_kmsg_set_qos_kernel(kmsg);
5082
5083 if (bits & MACH_MSGH_BITS_COMPLEX) {
5084 /*
5085 * Check if the remote port accepts ports in the body.
5086 */
5087 if (dest->ip_no_grant) {
5088 mach_msg_descriptor_t *saddr;
5089 mach_msg_body_t *body;
5090 mach_msg_type_number_t i, count;
5091
5092 body = (mach_msg_body_t *) (hdr + 1);
5093 saddr = (mach_msg_descriptor_t *) (body + 1);
5094 count = body->msgh_descriptor_count;
5095
5096 for (i = 0; i < count; i++, saddr++) {
5097 switch (saddr->type.type) {
5098 case MACH_MSG_PORT_DESCRIPTOR:
5099 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
5100 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
5101 /* no descriptors have been copied in yet */
5102 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
5103 return MACH_SEND_NO_GRANT_DEST;
5104 }
5105 }
5106 }
5107
5108 mach_msg_descriptor_t *saddr;
5109 mach_msg_body_t *body;
5110 mach_msg_type_number_t i, count;
5111
5112 body = (mach_msg_body_t *) (hdr + 1);
5113 saddr = (mach_msg_descriptor_t *) (body + 1);
5114 count = body->msgh_descriptor_count;
5115
5116 for (i = 0; i < count; i++, saddr++) {
5117 switch (saddr->type.type) {
5118 case MACH_MSG_PORT_DESCRIPTOR: {
5119 mach_msg_type_name_t name;
5120 ipc_object_t object;
5121 mach_msg_port_descriptor_t *dsc;
5122
5123 dsc = &saddr->port;
5124
5125 /* this is really the type SEND, SEND_ONCE, etc. */
5126 name = dsc->disposition;
5127 object = ip_to_object(dsc->name);
5128 dsc->disposition = ipc_object_copyin_type(name);
5129
5130 if (!IO_VALID(object)) {
5131 break;
5132 }
5133
5134 ipc_object_copyin_from_kernel(object, name);
5135
5136 /* CDY avoid circularity when the destination is also */
5137 /* the kernel. This check should be changed into an */
5138 /* assert when the new kobject model is in place since*/
5139 /* ports will not be used in kernel to kernel chats */
5140
5141 /* do not lock remote port, use raw pointer comparison */
5142 if (!ip_in_space_noauth(ip_object_to_port(remote), ipc_space_kernel)) {
5143 /* remote port could be dead, in-transit or in an ipc space */
5144 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
5145 ipc_port_check_circularity(ip_object_to_port(object),
5146 ip_object_to_port(remote))) {
5147 hdr->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
5148 }
5149 }
5150 break;
5151 }
5152 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
5153 case MACH_MSG_OOL_DESCRIPTOR: {
5154 /*
5155 * The sender should supply ready-made memory, i.e.
5156 * a vm_map_copy_t, so we don't need to do anything.
5157 */
5158 break;
5159 }
5160 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
5161 ipc_object_t *objects;
5162 unsigned int j;
5163 mach_msg_type_name_t name;
5164 mach_msg_ool_ports_descriptor_t *dsc;
5165
5166 dsc = (mach_msg_ool_ports_descriptor_t *)&saddr->ool_ports;
5167
5168 /* this is really the type SEND, SEND_ONCE, etc. */
5169 name = dsc->disposition;
5170 dsc->disposition = ipc_object_copyin_type(name);
5171
5172 objects = (ipc_object_t *) dsc->address;
5173
5174 for (j = 0; j < dsc->count; j++) {
5175 ipc_object_t object = objects[j];
5176
5177 if (!IO_VALID(object)) {
5178 continue;
5179 }
5180
5181 ipc_object_copyin_from_kernel(object, name);
5182
5183 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
5184 ipc_port_check_circularity(ip_object_to_port(object),
5185 ip_object_to_port(remote))) {
5186 hdr->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
5187 }
5188 }
5189 break;
5190 }
5191 case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
5192 mach_msg_guarded_port_descriptor_t *dsc = (typeof(dsc)) & saddr->guarded_port;
5193 mach_msg_type_name_t disp = dsc->disposition;
5194 ipc_object_t object = ip_to_object(dsc->name);
5195 dsc->disposition = ipc_object_copyin_type(disp);
5196 assert(dsc->flags == 0);
5197
5198 if (!IO_VALID(object)) {
5199 break;
5200 }
5201
5202 ipc_object_copyin_from_kernel(object, disp);
5203 /*
5204 * avoid circularity when the destination is also
5205 * the kernel. This check should be changed into an
5206 * assert when the new kobject model is in place since
5207 * ports will not be used in kernel to kernel chats
5208 */
5209
5210 /* do not lock remote port, use raw pointer comparison */
5211 if (!ip_in_space_noauth(ip_object_to_port(remote), ipc_space_kernel)) {
5212 /* remote port could be dead, in-transit or in an ipc space */
5213 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
5214 ipc_port_check_circularity(ip_object_to_port(object),
5215 ip_object_to_port(remote))) {
5216 hdr->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
5217 }
5218 }
5219 break;
5220 }
5221 default: {
5222 #if MACH_ASSERT
5223 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
5224 #endif /* MACH_ASSERT */
5225 }
5226 }
5227 }
5228 }
5229
5230 /* Add trailer and signature to the message */
5231 ipc_kmsg_init_trailer(kmsg, TASK_NULL);
5232 ikm_sign(kmsg);
5233
5234 return MACH_MSG_SUCCESS;
5235 }
5236
5237 /*
5238 * Routine: ipc_kmsg_copyout_header
5239 * Purpose:
5240 * "Copy-out" port rights in the header of a message.
5241 * Operates atomically; if it doesn't succeed the
5242 * message header and the space are left untouched.
5243 * If it does succeed the remote/local port fields
5244 * contain port names instead of object pointers,
5245 * and the bits field is updated.
5246 * Conditions:
5247 * Nothing locked.
5248 * Returns:
5249 * MACH_MSG_SUCCESS Copied out port rights.
5250 * MACH_RCV_INVALID_NOTIFY
5251 * Notify is non-null and doesn't name a receive right.
5252 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
5253 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
5254 * The space is dead.
5255 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
5256 * No room in space for another name.
5257 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
5258 * Couldn't allocate memory for the reply port.
5259 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
5260 * Couldn't allocate memory for the dead-name request.
5261 */
5262
5263 static mach_msg_return_t
ipc_kmsg_copyout_header(ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_option_t option)5264 ipc_kmsg_copyout_header(
5265 ipc_kmsg_t kmsg,
5266 ipc_space_t space,
5267 mach_msg_option_t option)
5268 {
5269 mach_msg_header_t *msg = ikm_header(kmsg);
5270 mach_msg_bits_t mbits = msg->msgh_bits;
5271 ipc_port_t dest = msg->msgh_remote_port;
5272
5273 assert(IP_VALID(dest));
5274
5275 /*
5276 * While we still hold a reference on the received-from port,
5277 * process all send-possible notfications we received along with
5278 * the message.
5279 */
5280 ipc_port_spnotify(dest);
5281
5282 {
5283 mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
5284 mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
5285 mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
5286 ipc_port_t reply = msg->msgh_local_port;
5287 ipc_port_t release_reply_port = IP_NULL;
5288 mach_port_name_t dest_name, reply_name;
5289
5290 ipc_port_t voucher = ipc_kmsg_get_voucher_port(kmsg);
5291 uintptr_t voucher_addr = 0;
5292 ipc_port_t release_voucher_port = IP_NULL;
5293 mach_port_name_t voucher_name;
5294
5295 uint32_t entries_held = 0;
5296 boolean_t need_write_lock = FALSE;
5297 ipc_object_copyout_flags_t reply_copyout_options = IPC_OBJECT_COPYOUT_FLAGS_NONE;
5298 kern_return_t kr;
5299
5300 /*
5301 * Reserve any potentially needed entries in the target space.
5302 * We'll free any unused before unlocking the space.
5303 */
5304 if (IP_VALID(reply)) {
5305 entries_held++;
5306 need_write_lock = TRUE;
5307 }
5308 if (IP_VALID(voucher)) {
5309 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5310
5311 if ((option & MACH_RCV_VOUCHER) != 0) {
5312 entries_held++;
5313 }
5314 need_write_lock = TRUE;
5315 voucher_addr = unsafe_convert_port_to_voucher(voucher);
5316 }
5317
5318 if (need_write_lock) {
5319 handle_reply_again:
5320 is_write_lock(space);
5321
5322 while (entries_held) {
5323 if (!is_active(space)) {
5324 is_write_unlock(space);
5325 return MACH_RCV_HEADER_ERROR |
5326 MACH_MSG_IPC_SPACE;
5327 }
5328
5329 kr = ipc_entries_hold(space, entries_held);
5330 if (KERN_SUCCESS == kr) {
5331 break;
5332 }
5333
5334 kr = ipc_entry_grow_table(space, ITS_SIZE_NONE);
5335 if (KERN_SUCCESS != kr) {
5336 return MACH_RCV_HEADER_ERROR |
5337 MACH_MSG_IPC_SPACE;
5338 }
5339 /* space was unlocked and relocked - retry */
5340 }
5341
5342 /* Handle reply port. */
5343 if (IP_VALID(reply)) {
5344 ipc_port_t reply_subst = IP_NULL;
5345 ipc_entry_t entry;
5346
5347 ip_mq_lock(reply);
5348
5349 /* Is the reply port still active and allowed to be copied out? */
5350 if (!ip_active(reply) ||
5351 !ip_label_check(space, reply, reply_type,
5352 &reply_copyout_options, &reply_subst)) {
5353 /* clear the context value */
5354 reply->ip_reply_context = 0;
5355 ip_mq_unlock(reply);
5356
5357 assert(reply_subst == IP_NULL);
5358 release_reply_port = reply;
5359 reply = IP_DEAD;
5360 reply_name = MACH_PORT_DEAD;
5361 goto done_with_reply;
5362 }
5363
5364 /* is the kolabel requesting a substitution */
5365 if (reply_subst != IP_NULL) {
5366 /*
5367 * port is unlocked, its right consumed
5368 * space is unlocked
5369 */
5370 assert(reply_type == MACH_MSG_TYPE_PORT_SEND);
5371 msg->msgh_local_port = reply = reply_subst;
5372 goto handle_reply_again;
5373 }
5374
5375
5376 /* Is there already an entry we can use? */
5377 if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
5378 ipc_right_reverse(space, ip_to_object(reply), &reply_name, &entry)) {
5379 assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
5380 } else {
5381 /* claim a held entry for the reply port */
5382 assert(entries_held > 0);
5383 entries_held--;
5384 ipc_entry_claim(space, ip_to_object(reply),
5385 &reply_name, &entry);
5386 }
5387
5388 /* space and reply port are locked and active */
5389 ip_reference(reply); /* hold onto the reply port */
5390
5391 /*
5392 * If the receiver would like to enforce strict reply
5393 * semantics, and the message looks like it expects a reply,
5394 * and contains a voucher, then link the context in the
5395 * voucher with the reply port so that the next message sent
5396 * to the reply port must come from a thread that has a
5397 * matching context (voucher).
5398 */
5399 if (enforce_strict_reply && MACH_RCV_WITH_STRICT_REPLY(option) && IP_VALID(voucher)) {
5400 if (ipc_kmsg_validate_reply_port_locked(reply, option) != KERN_SUCCESS) {
5401 /* if the receiver isn't happy with the reply port: fail the receive. */
5402 assert(!ip_is_pinned(reply));
5403 ipc_entry_dealloc(space, ip_to_object(reply),
5404 reply_name, entry);
5405 ip_mq_unlock(reply);
5406 is_write_unlock(space);
5407 ip_release(reply);
5408 return MACH_RCV_INVALID_REPLY;
5409 }
5410 ipc_kmsg_link_reply_context_locked(reply, voucher);
5411 } else {
5412 /*
5413 * if the receive did not choose to participate
5414 * in the strict reply/RPC, then don't enforce
5415 * anything (as this could lead to booby-trapped
5416 * messages that kill the server).
5417 */
5418 reply->ip_reply_context = 0;
5419 }
5420
5421 kr = ipc_right_copyout(space, reply_name, entry,
5422 reply_type, IPC_OBJECT_COPYOUT_FLAGS_NONE, NULL, NULL,
5423 ip_to_object(reply));
5424 assert(kr == KERN_SUCCESS);
5425 /* reply port is unlocked */
5426 } else {
5427 reply_name = CAST_MACH_PORT_TO_NAME(reply);
5428 }
5429
5430 done_with_reply:
5431
5432 /* Handle voucher port. */
5433 if (voucher_type != MACH_MSGH_BITS_ZERO) {
5434 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5435
5436 if (!IP_VALID(voucher)) {
5437 if ((option & MACH_RCV_VOUCHER) == 0) {
5438 voucher_type = MACH_MSGH_BITS_ZERO;
5439 }
5440 voucher_name = MACH_PORT_NULL;
5441 goto done_with_voucher;
5442 }
5443
5444 #if CONFIG_PREADOPT_TG
5445 struct knote *kn = current_thread()->ith_knote;
5446 if (kn == ITH_KNOTE_NULL || kn == ITH_KNOTE_PSEUDO) {
5447 /*
5448 * We are not in this path of voucher copyout because of
5449 * kevent - we cannot expect a voucher preadopt happening on
5450 * this thread for this message later on
5451 */
5452 KDBG_DEBUG(MACHDBG_CODE(DBG_MACH_THREAD_GROUP, MACH_THREAD_GROUP_PREADOPT_NA),
5453 thread_tid(current_thread()), 0, 0, 0);
5454 }
5455 #endif
5456
5457 /* clear voucher from its hiding place back in the kmsg */
5458 ipc_kmsg_clear_voucher_port(kmsg);
5459
5460 if ((option & MACH_RCV_VOUCHER) != 0) {
5461 ipc_entry_t entry;
5462
5463 ip_mq_lock(voucher);
5464
5465 if (ipc_right_reverse(space, ip_to_object(voucher),
5466 &voucher_name, &entry)) {
5467 assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
5468 } else {
5469 assert(entries_held > 0);
5470 entries_held--;
5471 ipc_entry_claim(space, ip_to_object(voucher), &voucher_name, &entry);
5472 }
5473 /* space is locked and active */
5474
5475 assert(ip_kotype(voucher) == IKOT_VOUCHER);
5476 kr = ipc_right_copyout(space, voucher_name, entry,
5477 MACH_MSG_TYPE_MOVE_SEND, IPC_OBJECT_COPYOUT_FLAGS_NONE,
5478 NULL, NULL, ip_to_object(voucher));
5479 /* voucher port is unlocked */
5480 } else {
5481 voucher_type = MACH_MSGH_BITS_ZERO;
5482 release_voucher_port = voucher;
5483 voucher_name = MACH_PORT_NULL;
5484 }
5485 } else {
5486 voucher_name = msg->msgh_voucher_port;
5487 }
5488
5489 done_with_voucher:
5490
5491 ip_mq_lock(dest);
5492 is_write_unlock(space);
5493 } else {
5494 /*
5495 * No reply or voucher port! This is an easy case.
5496 *
5497 * We only need to check that the space is still
5498 * active once we locked the destination:
5499 *
5500 * - if the space holds a receive right for `dest`,
5501 * then holding the port lock means we can't fail
5502 * to notice if the space went dead because
5503 * the is_write_unlock() will pair with
5504 * os_atomic_barrier_before_lock_acquire() + ip_mq_lock().
5505 *
5506 * - if this space doesn't hold a receive right
5507 * for `dest`, then `dest->ip_receiver` points
5508 * elsewhere, and ipc_object_copyout_dest() will
5509 * handle this situation, and failing to notice
5510 * that the space was dead is accetable.
5511 */
5512
5513 os_atomic_barrier_before_lock_acquire();
5514 ip_mq_lock(dest);
5515 if (!is_active(space)) {
5516 ip_mq_unlock(dest);
5517 return MACH_RCV_HEADER_ERROR | MACH_MSG_IPC_SPACE;
5518 }
5519
5520 reply_name = CAST_MACH_PORT_TO_NAME(reply);
5521
5522 if (voucher_type != MACH_MSGH_BITS_ZERO) {
5523 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5524 if ((option & MACH_RCV_VOUCHER) == 0) {
5525 voucher_type = MACH_MSGH_BITS_ZERO;
5526 }
5527 voucher_name = MACH_PORT_NULL;
5528 } else {
5529 voucher_name = msg->msgh_voucher_port;
5530 }
5531 }
5532
5533 /*
5534 * At this point, the space is unlocked and the destination
5535 * port is locked.
5536 * reply_name is taken care of; we still need dest_name.
5537 * We still hold a ref for reply (if it is valid).
5538 *
5539 * If the space holds receive rights for the destination,
5540 * we return its name for the right. Otherwise the task
5541 * managed to destroy or give away the receive right between
5542 * receiving the message and this copyout. If the destination
5543 * is dead, return MACH_PORT_DEAD, and if the receive right
5544 * exists somewhere else (another space, in transit)
5545 * return MACH_PORT_NULL.
5546 *
5547 * Making this copyout operation atomic with the previous
5548 * copyout of the reply port is a bit tricky. If there was
5549 * no real reply port (it wasn't IP_VALID) then this isn't
5550 * an issue. If the reply port was dead at copyout time,
5551 * then we are OK, because if dest is dead we serialize
5552 * after the death of both ports and if dest is alive
5553 * we serialize after reply died but before dest's (later) death.
5554 * So assume reply was alive when we copied it out. If dest
5555 * is alive, then we are OK because we serialize before
5556 * the ports' deaths. So assume dest is dead when we look at it.
5557 * If reply dies/died after dest, then we are OK because
5558 * we serialize after dest died but before reply dies.
5559 * So the hard case is when reply is alive at copyout,
5560 * dest is dead at copyout, and reply died before dest died.
5561 * In this case pretend that dest is still alive, so
5562 * we serialize while both ports are alive.
5563 *
5564 * Because the space lock is held across the copyout of reply
5565 * and locking dest, the receive right for dest can't move
5566 * in or out of the space while the copyouts happen, so
5567 * that isn't an atomicity problem. In the last hard case
5568 * above, this implies that when dest is dead that the
5569 * space couldn't have had receive rights for dest at
5570 * the time reply was copied-out, so when we pretend
5571 * that dest is still alive, we can return MACH_PORT_NULL.
5572 *
5573 * If dest == reply, then we have to make it look like
5574 * either both copyouts happened before the port died,
5575 * or both happened after the port died. This special
5576 * case works naturally if the timestamp comparison
5577 * is done correctly.
5578 */
5579
5580 if (ip_active(dest)) {
5581 ipc_object_copyout_dest(space, ip_to_object(dest),
5582 dest_type, &dest_name);
5583 /* dest is unlocked */
5584 } else {
5585 ipc_port_timestamp_t timestamp;
5586
5587 timestamp = ip_get_death_time(dest);
5588 ip_mq_unlock(dest);
5589 ip_release(dest);
5590
5591 if (IP_VALID(reply)) {
5592 ip_mq_lock(reply);
5593 if (ip_active(reply) ||
5594 IP_TIMESTAMP_ORDER(timestamp,
5595 ip_get_death_time(reply))) {
5596 dest_name = MACH_PORT_DEAD;
5597 } else {
5598 dest_name = MACH_PORT_NULL;
5599 }
5600 ip_mq_unlock(reply);
5601 } else {
5602 dest_name = MACH_PORT_DEAD;
5603 }
5604 }
5605
5606 if (IP_VALID(reply)) {
5607 ip_release(reply);
5608 }
5609
5610 if (IP_VALID(release_reply_port)) {
5611 if (reply_type == MACH_MSG_TYPE_PORT_SEND_ONCE) {
5612 ipc_port_release_sonce(release_reply_port);
5613 } else {
5614 ipc_port_release_send(release_reply_port);
5615 }
5616 }
5617
5618 if ((option & MACH_RCV_VOUCHER) != 0) {
5619 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV) | DBG_FUNC_NONE,
5620 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
5621 (uintptr_t)msg->msgh_bits,
5622 (uintptr_t)msg->msgh_id,
5623 VM_KERNEL_ADDRPERM(voucher_addr), 0);
5624 } else {
5625 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV_VOUCHER_REFUSED) | DBG_FUNC_NONE,
5626 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
5627 (uintptr_t)msg->msgh_bits,
5628 (uintptr_t)msg->msgh_id,
5629 VM_KERNEL_ADDRPERM(voucher_addr), 0);
5630 }
5631
5632 if (IP_VALID(release_voucher_port)) {
5633 ipc_port_release_send(release_voucher_port);
5634 }
5635
5636 msg->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
5637 voucher_type, mbits);
5638 msg->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
5639 msg->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
5640 msg->msgh_voucher_port = voucher_name;
5641 }
5642
5643 return MACH_MSG_SUCCESS;
5644 }
5645
5646 /*
5647 * Routine: ipc_kmsg_copyout_object
5648 * Purpose:
5649 * Copy-out a port right. Always returns a name,
5650 * even for unsuccessful return codes. Always
5651 * consumes the supplied object.
5652 * Conditions:
5653 * Nothing locked.
5654 * Returns:
5655 * MACH_MSG_SUCCESS The space acquired the right
5656 * (name is valid) or the object is dead (MACH_PORT_DEAD).
5657 * MACH_MSG_IPC_SPACE No room in space for the right,
5658 * or the space is dead. (Name is MACH_PORT_NULL.)
5659 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
5660 * (Name is MACH_PORT_NULL.)
5661 */
5662 static mach_msg_return_t
ipc_kmsg_copyout_object(ipc_space_t space,ipc_object_t object,mach_msg_type_name_t msgt_name,mach_port_context_t * context,mach_msg_guard_flags_t * guard_flags,mach_port_name_t * namep)5663 ipc_kmsg_copyout_object(
5664 ipc_space_t space,
5665 ipc_object_t object,
5666 mach_msg_type_name_t msgt_name,
5667 mach_port_context_t *context,
5668 mach_msg_guard_flags_t *guard_flags,
5669 mach_port_name_t *namep)
5670 {
5671 kern_return_t kr;
5672
5673 if (!IO_VALID(object)) {
5674 *namep = CAST_MACH_PORT_TO_NAME(object);
5675 return MACH_MSG_SUCCESS;
5676 }
5677
5678 kr = ipc_object_copyout(space, object, msgt_name, IPC_OBJECT_COPYOUT_FLAGS_NONE,
5679 context, guard_flags, namep);
5680 if (kr != KERN_SUCCESS) {
5681 if (kr == KERN_INVALID_CAPABILITY) {
5682 *namep = MACH_PORT_DEAD;
5683 } else {
5684 *namep = MACH_PORT_NULL;
5685
5686 if (kr == KERN_RESOURCE_SHORTAGE) {
5687 return MACH_MSG_IPC_KERNEL;
5688 } else {
5689 return MACH_MSG_IPC_SPACE;
5690 }
5691 }
5692 }
5693
5694 return MACH_MSG_SUCCESS;
5695 }
5696
5697 /*
5698 * Routine: ipc_kmsg_copyout_reply_object
5699 * Purpose:
5700 * Kernel swallows the send-once right associated with reply port.
5701 * Always returns a name, even for unsuccessful return codes.
5702 * Returns
5703 * MACH_MSG_SUCCESS Returns name of receive right for reply port.
5704 * Name is valid if the space acquired the right and msgt_name would be changed from MOVE_SO to MAKE_SO.
5705 * Name is MACH_PORT_DEAD if the object is dead.
5706 * Name is MACH_PORT_NULL if its entry could not be found in task's ipc space.
5707 * MACH_MSG_IPC_SPACE
5708 * The space is dead. (Name is MACH_PORT_NULL.)
5709 * Conditions:
5710 * Nothing locked.
5711 */
5712 static mach_msg_return_t
ipc_kmsg_copyout_reply_object(ipc_space_t space,ipc_object_t object,mach_msg_type_name_t * msgt_name,mach_port_name_t * namep)5713 ipc_kmsg_copyout_reply_object(
5714 ipc_space_t space,
5715 ipc_object_t object,
5716 mach_msg_type_name_t *msgt_name,
5717 mach_port_name_t *namep)
5718 {
5719 ipc_port_t port;
5720 ipc_entry_t entry;
5721 kern_return_t kr;
5722
5723 if (!IO_VALID(object)) {
5724 *namep = CAST_MACH_PORT_TO_NAME(object);
5725 return MACH_MSG_SUCCESS;
5726 }
5727
5728 port = ip_object_to_port(object);
5729
5730 assert(ip_is_reply_port(port));
5731 assert(*msgt_name == MACH_MSG_TYPE_PORT_SEND_ONCE);
5732
5733 is_write_lock(space);
5734
5735 if (!is_active(space)) {
5736 ipc_port_release_sonce(port);
5737 is_write_unlock(space);
5738 *namep = MACH_PORT_NULL;
5739 return MACH_MSG_IPC_SPACE;
5740 }
5741
5742 io_lock(object);
5743
5744 if (!io_active(object)) {
5745 *namep = MACH_PORT_DEAD;
5746 kr = MACH_MSG_SUCCESS;
5747 goto out;
5748 }
5749
5750 /* space is locked and active. object is locked and active. */
5751 if (!ipc_right_reverse(space, object, namep, &entry)) {
5752 *namep = MACH_PORT_NULL;
5753 kr = MACH_MSG_SUCCESS;
5754 goto out;
5755 }
5756
5757 assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
5758
5759 *msgt_name = MACH_MSG_TYPE_MAKE_SEND_ONCE;
5760 ipc_port_release_sonce_and_unlock(port);
5761 /* object is unlocked. */
5762
5763 is_write_unlock(space);
5764
5765 return MACH_MSG_SUCCESS;
5766
5767 out:
5768
5769 /* space and object are locked. */
5770 ipc_port_release_sonce_and_unlock(port);
5771
5772 is_write_unlock(space);
5773
5774 return kr;
5775 }
5776
5777 static mach_msg_descriptor_t *
ipc_kmsg_copyout_port_descriptor(mach_msg_descriptor_t * dsc,mach_msg_descriptor_t * dest_dsc,ipc_space_t space,kern_return_t * mr)5778 ipc_kmsg_copyout_port_descriptor(
5779 mach_msg_descriptor_t *dsc,
5780 mach_msg_descriptor_t *dest_dsc,
5781 ipc_space_t space,
5782 kern_return_t *mr)
5783 {
5784 mach_msg_user_port_descriptor_t *user_dsc;
5785 mach_port_t port;
5786 mach_port_name_t name;
5787 mach_msg_type_name_t disp;
5788
5789 /* Copyout port right carried in the message */
5790 port = dsc->port.name;
5791 disp = dsc->port.disposition;
5792 *mr |= ipc_kmsg_copyout_object(space,
5793 ip_to_object(port), disp, NULL, NULL, &name);
5794
5795 // point to the start of this port descriptor
5796 user_dsc = ((mach_msg_user_port_descriptor_t *)dest_dsc - 1);
5797 bzero((void *)user_dsc, sizeof(*user_dsc));
5798 user_dsc->name = CAST_MACH_PORT_TO_NAME(name);
5799 user_dsc->disposition = disp;
5800 user_dsc->type = MACH_MSG_PORT_DESCRIPTOR;
5801
5802 return (mach_msg_descriptor_t *)user_dsc;
5803 }
5804
5805 static mach_msg_descriptor_t *
ipc_kmsg_copyout_ool_descriptor(mach_msg_ool_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,vm_map_t map,mach_msg_return_t * mr)5806 ipc_kmsg_copyout_ool_descriptor(
5807 mach_msg_ool_descriptor_t *dsc,
5808 mach_msg_descriptor_t *user_dsc,
5809 int is_64bit,
5810 vm_map_t map,
5811 mach_msg_return_t *mr)
5812 {
5813 vm_map_copy_t copy;
5814 vm_map_address_t rcv_addr;
5815 mach_msg_copy_options_t copy_options;
5816 vm_map_size_t size;
5817 mach_msg_descriptor_type_t dsc_type;
5818 boolean_t misaligned = FALSE;
5819
5820 copy = (vm_map_copy_t)dsc->address;
5821 size = (vm_map_size_t)dsc->size;
5822 copy_options = dsc->copy;
5823 assert(copy_options != MACH_MSG_KALLOC_COPY_T);
5824 dsc_type = dsc->type;
5825
5826 if (copy != VM_MAP_COPY_NULL) {
5827 kern_return_t kr;
5828
5829 rcv_addr = 0;
5830 if (vm_map_copy_validate_size(map, copy, &size) == FALSE) {
5831 panic("Inconsistent OOL/copyout size on %p: expected %d, got %lld @%p",
5832 dsc, dsc->size, (unsigned long long)copy->size, copy);
5833 }
5834
5835 if ((copy->type == VM_MAP_COPY_ENTRY_LIST) &&
5836 (trunc_page(copy->offset) != copy->offset ||
5837 round_page(dsc->size) != dsc->size)) {
5838 misaligned = TRUE;
5839 }
5840
5841 if (misaligned) {
5842 mach_vm_offset_t rounded_addr;
5843 vm_map_size_t rounded_size;
5844 vm_map_offset_t effective_page_mask, effective_page_size;
5845
5846 effective_page_mask = VM_MAP_PAGE_MASK(map);
5847 effective_page_size = effective_page_mask + 1;
5848
5849 rounded_size = vm_map_round_page(copy->offset + size, effective_page_mask) - vm_map_trunc_page(copy->offset, effective_page_mask);
5850
5851 kr = mach_vm_allocate_kernel(map, &rounded_addr,
5852 rounded_size, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IPC);
5853
5854 if (kr == KERN_SUCCESS) {
5855 /*
5856 * vm_map_copy_overwrite does a full copy
5857 * if size is too small to optimize.
5858 * So we tried skipping the offset adjustment
5859 * if we fail the 'size' test.
5860 *
5861 * if (size >= VM_MAP_COPY_OVERWRITE_OPTIMIZATION_THRESHOLD_PAGES * effective_page_size) {
5862 *
5863 * This resulted in leaked memory especially on the
5864 * older watches (16k user - 4k kernel) because we
5865 * would do a physical copy into the start of this
5866 * rounded range but could leak part of it
5867 * on deallocation if the 'size' being deallocated
5868 * does not cover the full range. So instead we do
5869 * the misalignment adjustment always so that on
5870 * deallocation we will remove the full range.
5871 */
5872 if ((rounded_addr & effective_page_mask) !=
5873 (copy->offset & effective_page_mask)) {
5874 /*
5875 * Need similar mis-alignment of source and destination...
5876 */
5877 rounded_addr += (copy->offset & effective_page_mask);
5878
5879 assert((rounded_addr & effective_page_mask) == (copy->offset & effective_page_mask));
5880 }
5881 rcv_addr = rounded_addr;
5882
5883 kr = vm_map_copy_overwrite(map, rcv_addr, copy, size, FALSE);
5884 }
5885 } else {
5886 kr = vm_map_copyout_size(map, &rcv_addr, copy, size);
5887 }
5888 if (kr != KERN_SUCCESS) {
5889 if (kr == KERN_RESOURCE_SHORTAGE) {
5890 *mr |= MACH_MSG_VM_KERNEL;
5891 } else {
5892 *mr |= MACH_MSG_VM_SPACE;
5893 }
5894 vm_map_copy_discard(copy);
5895 rcv_addr = 0;
5896 size = 0;
5897 }
5898 } else {
5899 rcv_addr = 0;
5900 size = 0;
5901 }
5902
5903 /*
5904 * Now update the descriptor as the user would see it.
5905 * This may require expanding the descriptor to the user
5906 * visible size. There is already space allocated for
5907 * this in what naddr points to.
5908 */
5909 if (is_64bit) {
5910 mach_msg_ool_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
5911 user_ool_dsc--;
5912 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
5913
5914 user_ool_dsc->address = rcv_addr;
5915 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
5916 TRUE : FALSE;
5917 user_ool_dsc->copy = copy_options;
5918 user_ool_dsc->type = dsc_type;
5919 user_ool_dsc->size = (mach_msg_size_t)size;
5920
5921 user_dsc = (typeof(user_dsc))user_ool_dsc;
5922 } else {
5923 mach_msg_ool_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
5924 user_ool_dsc--;
5925 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
5926
5927 user_ool_dsc->address = CAST_DOWN_EXPLICIT(uint32_t, rcv_addr);
5928 user_ool_dsc->size = (mach_msg_size_t)size;
5929 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
5930 TRUE : FALSE;
5931 user_ool_dsc->copy = copy_options;
5932 user_ool_dsc->type = dsc_type;
5933
5934 user_dsc = (typeof(user_dsc))user_ool_dsc;
5935 }
5936 return user_dsc;
5937 }
5938
5939 static mach_msg_descriptor_t *
ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,vm_map_t map,ipc_space_t space,ipc_kmsg_t kmsg,mach_msg_return_t * mr)5940 ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t *dsc,
5941 mach_msg_descriptor_t *user_dsc,
5942 int is_64bit,
5943 vm_map_t map,
5944 ipc_space_t space,
5945 ipc_kmsg_t kmsg,
5946 mach_msg_return_t *mr)
5947 {
5948 mach_vm_offset_t rcv_addr = 0;
5949 mach_msg_type_name_t disp;
5950 mach_msg_type_number_t count, i;
5951 vm_size_t ports_length, names_length;
5952 mach_msg_copy_options_t copy_options = MACH_MSG_VIRTUAL_COPY;
5953
5954 count = dsc->count;
5955 disp = dsc->disposition;
5956 ports_length = count * sizeof(mach_port_t);
5957 names_length = count * sizeof(mach_port_name_t);
5958
5959 if (ports_length != 0 && dsc->address != 0) {
5960 if (copy_options == MACH_MSG_VIRTUAL_COPY) {
5961 /*
5962 * Dynamically allocate the region
5963 */
5964 vm_tag_t tag;
5965 if (vm_kernel_map_is_kernel(map)) {
5966 tag = VM_KERN_MEMORY_IPC;
5967 } else {
5968 tag = VM_MEMORY_MACH_MSG;
5969 }
5970
5971 kern_return_t kr;
5972 if ((kr = mach_vm_allocate_kernel(map, &rcv_addr,
5973 (mach_vm_size_t)names_length,
5974 VM_FLAGS_ANYWHERE, tag)) != KERN_SUCCESS) {
5975 ipc_kmsg_clean_body(kmsg, 1, (mach_msg_descriptor_t *)dsc);
5976 rcv_addr = 0;
5977
5978 if (kr == KERN_RESOURCE_SHORTAGE) {
5979 *mr |= MACH_MSG_VM_KERNEL;
5980 } else {
5981 *mr |= MACH_MSG_VM_SPACE;
5982 }
5983 }
5984 }
5985
5986 /*
5987 * Handle the port rights and copy out the names
5988 * for those rights out to user-space.
5989 */
5990 if (rcv_addr != 0) {
5991 ipc_object_t *objects = (ipc_object_t *) dsc->address;
5992 mach_port_name_t *names = (mach_port_name_t *) dsc->address;
5993
5994 /* copyout port rights carried in the message */
5995
5996 for (i = 0; i < count; i++) {
5997 ipc_object_t object = objects[i];
5998
5999 *mr |= ipc_kmsg_copyout_object(space, object,
6000 disp, NULL, NULL, &names[i]);
6001 }
6002
6003 /* copyout to memory allocated above */
6004 void *data = dsc->address;
6005 if (copyoutmap(map, data, rcv_addr, names_length) != KERN_SUCCESS) {
6006 *mr |= MACH_MSG_VM_SPACE;
6007 }
6008 kfree_type(mach_port_t, count, data);
6009 }
6010 } else {
6011 rcv_addr = 0;
6012 }
6013
6014 /*
6015 * Now update the descriptor based on the information
6016 * calculated above.
6017 */
6018 if (is_64bit) {
6019 mach_msg_ool_ports_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
6020 user_ool_dsc--;
6021 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
6022
6023 user_ool_dsc->address = rcv_addr;
6024 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
6025 TRUE : FALSE;
6026 user_ool_dsc->copy = copy_options;
6027 user_ool_dsc->disposition = disp;
6028 user_ool_dsc->type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
6029 user_ool_dsc->count = count;
6030
6031 user_dsc = (typeof(user_dsc))user_ool_dsc;
6032 } else {
6033 mach_msg_ool_ports_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
6034 user_ool_dsc--;
6035 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
6036
6037 user_ool_dsc->address = CAST_DOWN_EXPLICIT(uint32_t, rcv_addr);
6038 user_ool_dsc->count = count;
6039 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
6040 TRUE : FALSE;
6041 user_ool_dsc->copy = copy_options;
6042 user_ool_dsc->disposition = disp;
6043 user_ool_dsc->type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
6044
6045 user_dsc = (typeof(user_dsc))user_ool_dsc;
6046 }
6047 return user_dsc;
6048 }
6049
6050 static mach_msg_descriptor_t *
ipc_kmsg_copyout_guarded_port_descriptor(mach_msg_guarded_port_descriptor_t * dsc,mach_msg_descriptor_t * dest_dsc,int is_64bit,__unused ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_option_t option,kern_return_t * mr)6051 ipc_kmsg_copyout_guarded_port_descriptor(
6052 mach_msg_guarded_port_descriptor_t *dsc,
6053 mach_msg_descriptor_t *dest_dsc,
6054 int is_64bit,
6055 __unused ipc_kmsg_t kmsg,
6056 ipc_space_t space,
6057 mach_msg_option_t option,
6058 kern_return_t *mr)
6059 {
6060 mach_port_t port;
6061 mach_port_name_t name = MACH_PORT_NULL;
6062 mach_msg_type_name_t disp;
6063 mach_msg_guard_flags_t guard_flags;
6064 mach_port_context_t context;
6065
6066 /* Copyout port right carried in the message */
6067 port = dsc->name;
6068 disp = dsc->disposition;
6069 guard_flags = dsc->flags;
6070 context = 0;
6071
6072 /* Currently kernel_task doesnt support receiving guarded port descriptors */
6073 struct knote *kn = current_thread()->ith_knote;
6074 if ((kn != ITH_KNOTE_PSEUDO) && ((option & MACH_RCV_GUARDED_DESC) == 0)) {
6075 #if DEVELOPMENT || DEBUG
6076 /*
6077 * Simulated crash needed for debugging, notifies the receiver to opt into receiving
6078 * guarded descriptors.
6079 */
6080 mach_port_guard_exception(current_thread()->ith_receiver_name,
6081 0, 0, kGUARD_EXC_RCV_GUARDED_DESC);
6082 #endif
6083 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_DESTROY_GUARDED_DESC), current_thread()->ith_receiver_name,
6084 VM_KERNEL_ADDRPERM(port), disp, guard_flags);
6085 ipc_object_destroy(ip_to_object(port), disp);
6086 mach_msg_user_port_descriptor_t *user_dsc = (typeof(user_dsc))dest_dsc;
6087 user_dsc--; // point to the start of this port descriptor
6088 bzero((void *)user_dsc, sizeof(*user_dsc));
6089 user_dsc->name = name;
6090 user_dsc->disposition = disp;
6091 user_dsc->type = MACH_MSG_PORT_DESCRIPTOR;
6092 dest_dsc = (typeof(dest_dsc))user_dsc;
6093 } else {
6094 *mr |= ipc_kmsg_copyout_object(space,
6095 ip_to_object(port), disp, &context, &guard_flags, &name);
6096
6097 if (!is_64bit) {
6098 mach_msg_guarded_port_descriptor32_t *user_dsc = (typeof(user_dsc))dest_dsc;
6099 user_dsc--; // point to the start of this port descriptor
6100 bzero((void *)user_dsc, sizeof(*user_dsc));
6101 user_dsc->name = name;
6102 user_dsc->flags = guard_flags;
6103 user_dsc->disposition = disp;
6104 user_dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
6105 user_dsc->context = CAST_DOWN_EXPLICIT(uint32_t, context);
6106 dest_dsc = (typeof(dest_dsc))user_dsc;
6107 } else {
6108 mach_msg_guarded_port_descriptor64_t *user_dsc = (typeof(user_dsc))dest_dsc;
6109 user_dsc--; // point to the start of this port descriptor
6110 bzero((void *)user_dsc, sizeof(*user_dsc));
6111 user_dsc->name = name;
6112 user_dsc->flags = guard_flags;
6113 user_dsc->disposition = disp;
6114 user_dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
6115 user_dsc->context = context;
6116 dest_dsc = (typeof(dest_dsc))user_dsc;
6117 }
6118 }
6119
6120 return (mach_msg_descriptor_t *)dest_dsc;
6121 }
6122
6123
6124 /*
6125 * Routine: ipc_kmsg_copyout_body
6126 * Purpose:
6127 * "Copy-out" port rights and out-of-line memory
6128 * in the body of a message.
6129 *
6130 * The error codes are a combination of special bits.
6131 * The copyout proceeds despite errors.
6132 * Conditions:
6133 * Nothing locked.
6134 * Returns:
6135 * MACH_MSG_SUCCESS Successful copyout.
6136 * MACH_MSG_IPC_SPACE No room for port right in name space.
6137 * MACH_MSG_VM_SPACE No room for memory in address space.
6138 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
6139 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
6140 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
6141 */
6142
6143 static mach_msg_return_t
ipc_kmsg_copyout_body(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_option_t option)6144 ipc_kmsg_copyout_body(
6145 ipc_kmsg_t kmsg,
6146 ipc_space_t space,
6147 vm_map_t map,
6148 mach_msg_option_t option)
6149 {
6150 mach_msg_body_t *body;
6151 mach_msg_descriptor_t *kern_dsc, *user_dsc;
6152 mach_msg_type_number_t dsc_count;
6153 mach_msg_return_t mr = MACH_MSG_SUCCESS;
6154 boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
6155 mach_msg_header_t *hdr = ikm_header(kmsg);
6156
6157 body = (mach_msg_body_t *) (hdr + 1);
6158 dsc_count = body->msgh_descriptor_count;
6159 kern_dsc = (mach_msg_descriptor_t *) (body + 1);
6160 /* Point user_dsc just after the end of all the descriptors */
6161 user_dsc = &kern_dsc[dsc_count];
6162
6163 assert(current_task() != kernel_task);
6164
6165 /* Now process the descriptors - in reverse order */
6166 for (mach_msg_type_number_t i = dsc_count; i-- > 0;) {
6167 switch (kern_dsc[i].type.type) {
6168 case MACH_MSG_PORT_DESCRIPTOR:
6169 user_dsc = ipc_kmsg_copyout_port_descriptor(&kern_dsc[i],
6170 user_dsc, space, &mr);
6171 break;
6172 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
6173 case MACH_MSG_OOL_DESCRIPTOR:
6174 user_dsc = ipc_kmsg_copyout_ool_descriptor(
6175 (mach_msg_ool_descriptor_t *)&kern_dsc[i],
6176 user_dsc, is_task_64bit, map, &mr);
6177 break;
6178 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
6179 user_dsc = ipc_kmsg_copyout_ool_ports_descriptor(
6180 (mach_msg_ool_ports_descriptor_t *)&kern_dsc[i],
6181 user_dsc, is_task_64bit, map, space, kmsg, &mr);
6182 break;
6183 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
6184 user_dsc = ipc_kmsg_copyout_guarded_port_descriptor(
6185 (mach_msg_guarded_port_descriptor_t *)&kern_dsc[i],
6186 user_dsc, is_task_64bit, kmsg, space, option, &mr);
6187 break;
6188 default:
6189 panic("untyped IPC copyout body: invalid message descriptor");
6190 }
6191 }
6192
6193 assert((vm_offset_t)kern_dsc == (vm_offset_t)hdr + sizeof(mach_msg_base_t));
6194
6195 if (user_dsc != kern_dsc) {
6196 vm_offset_t dsc_adjust = (vm_offset_t)user_dsc - (vm_offset_t)kern_dsc;
6197 /* update the message size for the smaller user representation */
6198 hdr->msgh_size -= (mach_msg_size_t)dsc_adjust;
6199
6200 if (ikm_is_linear(kmsg)) {
6201 /* trailer has been initialized during send - memmove it too. */
6202 memmove((char *)kern_dsc,
6203 user_dsc, hdr->msgh_size - sizeof(mach_msg_base_t) + MAX_TRAILER_SIZE);
6204 } else {
6205 /* just memmove the descriptors following the header */
6206 memmove((char *)kern_dsc,
6207 user_dsc, ikm_total_desc_size(kmsg, current_map(), dsc_adjust, 0, true));
6208 }
6209 }
6210
6211 return mr;
6212 }
6213
6214 /*
6215 * Routine: ipc_kmsg_copyout_size
6216 * Purpose:
6217 * Compute the size of the message as copied out to the given
6218 * map. If the destination map's pointers are a different size
6219 * than the kernel's, we have to allow for expansion/
6220 * contraction of the descriptors as appropriate.
6221 * Conditions:
6222 * Nothing locked.
6223 * Returns:
6224 * size of the message as it would be received.
6225 */
6226
6227 mach_msg_size_t
ipc_kmsg_copyout_size(ipc_kmsg_t kmsg,vm_map_t map)6228 ipc_kmsg_copyout_size(
6229 ipc_kmsg_t kmsg,
6230 vm_map_t map)
6231 {
6232 mach_msg_size_t send_size;
6233 mach_msg_header_t *hdr;
6234
6235 hdr = ikm_header(kmsg);
6236 send_size = hdr->msgh_size - USER_HEADER_SIZE_DELTA;
6237
6238 boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
6239
6240 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
6241 mach_msg_body_t *body;
6242 mach_msg_descriptor_t *saddr, *eaddr;
6243
6244 body = (mach_msg_body_t *) (hdr + 1);
6245 saddr = (mach_msg_descriptor_t *) (body + 1);
6246 eaddr = saddr + body->msgh_descriptor_count;
6247
6248 send_size -= KERNEL_DESC_SIZE * body->msgh_descriptor_count;
6249 for (; saddr < eaddr; saddr++) {
6250 send_size += ikm_user_desc_size(saddr->type.type, is_task_64bit);
6251 }
6252 }
6253 return send_size;
6254 }
6255
6256 /*
6257 * Routine: ipc_kmsg_copyout
6258 * Purpose:
6259 * "Copy-out" port rights and out-of-line memory
6260 * in the message.
6261 * Conditions:
6262 * Nothing locked.
6263 * Returns:
6264 * MACH_MSG_SUCCESS Copied out all rights and memory.
6265 * MACH_RCV_HEADER_ERROR + special bits
6266 * Rights and memory in the message are intact.
6267 * MACH_RCV_BODY_ERROR + special bits
6268 * The message header was successfully copied out.
6269 * As much of the body was handled as possible.
6270 */
6271
6272 mach_msg_return_t
ipc_kmsg_copyout(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map,mach_msg_option_t option)6273 ipc_kmsg_copyout(
6274 ipc_kmsg_t kmsg,
6275 ipc_space_t space,
6276 vm_map_t map,
6277 mach_msg_option_t option)
6278 {
6279 mach_msg_return_t mr;
6280
6281 ikm_validate_sig(kmsg);
6282
6283 mr = ipc_kmsg_copyout_header(kmsg, space, option);
6284 if (mr != MACH_MSG_SUCCESS) {
6285 return mr;
6286 }
6287
6288 if (ikm_header(kmsg)->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
6289 mr = ipc_kmsg_copyout_body(kmsg, space, map, option);
6290
6291 if (mr != MACH_MSG_SUCCESS) {
6292 mr |= MACH_RCV_BODY_ERROR;
6293 }
6294 }
6295
6296 return mr;
6297 }
6298
6299 /*
6300 * Routine: ipc_kmsg_copyout_pseudo
6301 * Purpose:
6302 * Does a pseudo-copyout of the message.
6303 * This is like a regular copyout, except
6304 * that the ports in the header are handled
6305 * as if they are in the body. They aren't reversed.
6306 *
6307 * The error codes are a combination of special bits.
6308 * The copyout proceeds despite errors.
6309 * Conditions:
6310 * Nothing locked.
6311 * Returns:
6312 * MACH_MSG_SUCCESS Successful copyout.
6313 * MACH_MSG_IPC_SPACE No room for port right in name space.
6314 * MACH_MSG_VM_SPACE No room for memory in address space.
6315 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
6316 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
6317 */
6318
6319 mach_msg_return_t
ipc_kmsg_copyout_pseudo(ipc_kmsg_t kmsg,ipc_space_t space,vm_map_t map)6320 ipc_kmsg_copyout_pseudo(
6321 ipc_kmsg_t kmsg,
6322 ipc_space_t space,
6323 vm_map_t map)
6324 {
6325 mach_msg_header_t *hdr = ikm_header(kmsg);
6326 mach_msg_bits_t mbits = hdr->msgh_bits;
6327 ipc_object_t dest = ip_to_object(hdr->msgh_remote_port);
6328 ipc_object_t reply = ip_to_object(hdr->msgh_local_port);
6329 ipc_object_t voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
6330 mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
6331 mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
6332 mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
6333 mach_port_name_t voucher_name = hdr->msgh_voucher_port;
6334 mach_port_name_t dest_name, reply_name;
6335 mach_msg_return_t mr;
6336
6337 /* Set ith_knote to ITH_KNOTE_PSEUDO */
6338 current_thread()->ith_knote = ITH_KNOTE_PSEUDO;
6339
6340 ikm_validate_sig(kmsg);
6341
6342 assert(IO_VALID(dest));
6343
6344 #if 0
6345 /*
6346 * If we did this here, it looks like we wouldn't need the undo logic
6347 * at the end of ipc_kmsg_send() in the error cases. Not sure which
6348 * would be more elegant to keep.
6349 */
6350 ipc_importance_clean(kmsg);
6351 #else
6352 /* just assert it is already clean */
6353 ipc_importance_assert_clean(kmsg);
6354 #endif
6355
6356 mr = ipc_kmsg_copyout_object(space, dest, dest_type, NULL, NULL, &dest_name);
6357
6358 if (!IO_VALID(reply)) {
6359 reply_name = CAST_MACH_PORT_TO_NAME(reply);
6360 } else if (ip_is_reply_port(ip_object_to_port(reply))) {
6361 mach_msg_return_t reply_mr;
6362 reply_mr = ipc_kmsg_copyout_reply_object(space, reply, &reply_type, &reply_name);
6363 mr = mr | reply_mr;
6364 if (reply_mr == MACH_MSG_SUCCESS) {
6365 mbits = MACH_MSGH_BITS_SET(dest_type, reply_type, voucher_type, MACH_MSGH_BITS_OTHER(mbits));
6366 }
6367 } else {
6368 mr = mr | ipc_kmsg_copyout_object(space, reply, reply_type, NULL, NULL, &reply_name);
6369 }
6370
6371 hdr->msgh_bits = mbits & MACH_MSGH_BITS_USER;
6372 hdr->msgh_remote_port = CAST_MACH_NAME_TO_PORT(dest_name);
6373 hdr->msgh_local_port = CAST_MACH_NAME_TO_PORT(reply_name);
6374
6375 /* restore the voucher:
6376 * If it was copied in via move-send, have to put back a voucher send right.
6377 *
6378 * If it was copied in via copy-send, the header still contains the old voucher name.
6379 * Restore the type and discard the copied-in/pre-processed voucher.
6380 */
6381 if (IO_VALID(voucher)) {
6382 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
6383 if (kmsg->ikm_voucher_type == MACH_MSG_TYPE_MOVE_SEND) {
6384 mr |= ipc_kmsg_copyout_object(space, voucher, voucher_type, NULL, NULL, &voucher_name);
6385 hdr->msgh_voucher_port = voucher_name;
6386 } else {
6387 assert(kmsg->ikm_voucher_type == MACH_MSG_TYPE_COPY_SEND);
6388 hdr->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, MACH_MSG_TYPE_COPY_SEND,
6389 MACH_MSGH_BITS_OTHER(hdr->msgh_bits));
6390 ipc_object_destroy(voucher, voucher_type);
6391 }
6392 ipc_kmsg_clear_voucher_port(kmsg);
6393 }
6394
6395 if (mbits & MACH_MSGH_BITS_COMPLEX) {
6396 mr |= ipc_kmsg_copyout_body(kmsg, space, map, 0);
6397 }
6398
6399 current_thread()->ith_knote = ITH_KNOTE_NULL;
6400
6401 return mr;
6402 }
6403
6404 /*
6405 * Routine: ipc_kmsg_copyout_dest_to_user
6406 * Purpose:
6407 * Copies out the destination port in the message.
6408 * Destroys all other rights and memory in the message.
6409 * Conditions:
6410 * Nothing locked.
6411 */
6412
6413 void
ipc_kmsg_copyout_dest_to_user(ipc_kmsg_t kmsg,ipc_space_t space)6414 ipc_kmsg_copyout_dest_to_user(
6415 ipc_kmsg_t kmsg,
6416 ipc_space_t space)
6417 {
6418 mach_msg_bits_t mbits;
6419 ipc_object_t dest;
6420 ipc_object_t reply;
6421 ipc_object_t voucher;
6422 mach_msg_type_name_t dest_type;
6423 mach_msg_type_name_t reply_type;
6424 mach_msg_type_name_t voucher_type;
6425 mach_port_name_t dest_name, reply_name, voucher_name;
6426 mach_msg_header_t *hdr;
6427
6428 ikm_validate_sig(kmsg);
6429
6430 hdr = ikm_header(kmsg);
6431 mbits = hdr->msgh_bits;
6432 dest = ip_to_object(hdr->msgh_remote_port);
6433 reply = ip_to_object(hdr->msgh_local_port);
6434 voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
6435 voucher_name = hdr->msgh_voucher_port;
6436 dest_type = MACH_MSGH_BITS_REMOTE(mbits);
6437 reply_type = MACH_MSGH_BITS_LOCAL(mbits);
6438 voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
6439
6440 assert(IO_VALID(dest));
6441
6442 ipc_importance_assert_clean(kmsg);
6443
6444 io_lock(dest);
6445 if (io_active(dest)) {
6446 ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
6447 /* dest is unlocked */
6448 } else {
6449 io_unlock(dest);
6450 io_release(dest);
6451 dest_name = MACH_PORT_DEAD;
6452 }
6453
6454 if (IO_VALID(reply)) {
6455 ipc_object_destroy(reply, reply_type);
6456 reply_name = MACH_PORT_NULL;
6457 } else {
6458 reply_name = CAST_MACH_PORT_TO_NAME(reply);
6459 }
6460
6461 if (IO_VALID(voucher)) {
6462 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
6463 ipc_object_destroy(voucher, voucher_type);
6464 ipc_kmsg_clear_voucher_port(kmsg);
6465 voucher_name = MACH_PORT_NULL;
6466 }
6467
6468 hdr->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
6469 voucher_type, mbits);
6470 hdr->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
6471 hdr->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
6472 hdr->msgh_voucher_port = voucher_name;
6473
6474 if (mbits & MACH_MSGH_BITS_COMPLEX) {
6475 mach_msg_body_t *body;
6476
6477 body = (mach_msg_body_t *) (hdr + 1);
6478 ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
6479 (mach_msg_descriptor_t *)(body + 1));
6480 }
6481 }
6482
6483 /*
6484 * Routine: ipc_kmsg_copyout_dest_to_kernel
6485 * Purpose:
6486 * Copies out the destination and reply ports in the message.
6487 * Leaves all other rights and memory in the message alone.
6488 * Conditions:
6489 * Nothing locked.
6490 *
6491 * Derived from ipc_kmsg_copyout_dest_to_user.
6492 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
6493 * We really do want to save rights and memory.
6494 */
6495
6496 void
ipc_kmsg_copyout_dest_to_kernel(ipc_kmsg_t kmsg,ipc_space_t space)6497 ipc_kmsg_copyout_dest_to_kernel(
6498 ipc_kmsg_t kmsg,
6499 ipc_space_t space)
6500 {
6501 ipc_object_t dest;
6502 mach_port_t reply;
6503 mach_msg_type_name_t dest_type;
6504 mach_msg_type_name_t reply_type;
6505 mach_port_name_t dest_name;
6506 mach_msg_header_t *hdr;
6507
6508 ikm_validate_sig(kmsg);
6509
6510 hdr = ikm_header(kmsg);
6511 dest = ip_to_object(hdr->msgh_remote_port);
6512 reply = hdr->msgh_local_port;
6513 dest_type = MACH_MSGH_BITS_REMOTE(hdr->msgh_bits);
6514 reply_type = MACH_MSGH_BITS_LOCAL(hdr->msgh_bits);
6515
6516 assert(IO_VALID(dest));
6517
6518 io_lock(dest);
6519 if (io_active(dest)) {
6520 ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
6521 /* dest is unlocked */
6522 } else {
6523 io_unlock(dest);
6524 io_release(dest);
6525 dest_name = MACH_PORT_DEAD;
6526 }
6527
6528 /*
6529 * While MIG kernel users don't receive vouchers, the
6530 * msgh_voucher_port field is intended to be round-tripped through the
6531 * kernel if there is no voucher disposition set. Here we check for a
6532 * non-zero voucher disposition, and consume the voucher send right as
6533 * there is no possible way to specify MACH_RCV_VOUCHER semantics.
6534 */
6535 mach_msg_type_name_t voucher_type;
6536 voucher_type = MACH_MSGH_BITS_VOUCHER(hdr->msgh_bits);
6537 if (voucher_type != MACH_MSGH_BITS_ZERO) {
6538 ipc_port_t voucher = ipc_kmsg_get_voucher_port(kmsg);
6539
6540 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
6541 /*
6542 * someone managed to send this kernel routine a message with
6543 * a voucher in it. Cleanup the reference in
6544 * kmsg->ikm_voucher.
6545 */
6546 if (IP_VALID(voucher)) {
6547 ipc_port_release_send(voucher);
6548 }
6549 hdr->msgh_voucher_port = 0;
6550 ipc_kmsg_clear_voucher_port(kmsg);
6551 }
6552
6553 hdr->msgh_bits =
6554 (MACH_MSGH_BITS_OTHER(hdr->msgh_bits) |
6555 MACH_MSGH_BITS(reply_type, dest_type));
6556 hdr->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
6557 hdr->msgh_remote_port = reply;
6558 }
6559
6560 /*
6561 * Caller has a reference to the kmsg and the mqueue lock held.
6562 *
6563 * As such, we can safely return a pointer to the thread group in the kmsg and
6564 * not an additional reference. It is up to the caller to decide to take an
6565 * additional reference on the thread group while still holding the mqueue lock,
6566 * if needed.
6567 */
6568 #if CONFIG_PREADOPT_TG
6569 struct thread_group *
ipc_kmsg_get_thread_group(ipc_kmsg_t kmsg)6570 ipc_kmsg_get_thread_group(ipc_kmsg_t kmsg)
6571 {
6572 struct thread_group *tg = NULL;
6573 kern_return_t __assert_only kr;
6574
6575 ipc_voucher_t voucher = convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg));
6576 kr = bank_get_preadopt_thread_group(voucher, &tg);
6577 ipc_voucher_release(voucher);
6578
6579 return tg;
6580 }
6581 #endif
6582
6583 #ifdef __arm64__
6584 /*
6585 * Just sets those parts of the trailer that aren't set up at allocation time.
6586 */
6587 static void
ipc_kmsg_munge_trailer(mach_msg_max_trailer_t * in,void * _out,boolean_t is64bit)6588 ipc_kmsg_munge_trailer(mach_msg_max_trailer_t *in, void *_out, boolean_t is64bit)
6589 {
6590 if (is64bit) {
6591 mach_msg_max_trailer64_t *out = (mach_msg_max_trailer64_t*)_out;
6592 out->msgh_seqno = in->msgh_seqno;
6593 out->msgh_context = in->msgh_context;
6594 out->msgh_trailer_size = in->msgh_trailer_size;
6595 out->msgh_ad = in->msgh_ad;
6596 } else {
6597 mach_msg_max_trailer32_t *out = (mach_msg_max_trailer32_t*)_out;
6598 out->msgh_seqno = in->msgh_seqno;
6599 out->msgh_context = (mach_port_context32_t)in->msgh_context;
6600 out->msgh_trailer_size = in->msgh_trailer_size;
6601 out->msgh_ad = in->msgh_ad;
6602 }
6603 }
6604 #endif /* __arm64__ */
6605
6606 mach_msg_trailer_size_t
ipc_kmsg_trailer_size(mach_msg_option_t option,__unused thread_t thread)6607 ipc_kmsg_trailer_size(
6608 mach_msg_option_t option,
6609 __unused thread_t thread)
6610 {
6611 if (!(option & MACH_RCV_TRAILER_MASK)) {
6612 return MACH_MSG_TRAILER_MINIMUM_SIZE;
6613 } else {
6614 return REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread), option);
6615 }
6616 }
6617
6618 /*
6619 * Routine: ipc_kmsg_init_trailer
6620 * Purpose:
6621 * Initiailizes a trailer in a message safely.
6622 */
6623 void
ipc_kmsg_init_trailer(ipc_kmsg_t kmsg,task_t sender)6624 ipc_kmsg_init_trailer(
6625 ipc_kmsg_t kmsg,
6626 task_t sender)
6627 {
6628 static const mach_msg_max_trailer_t KERNEL_TRAILER_TEMPLATE = {
6629 .msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0,
6630 .msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE,
6631 .msgh_sender = KERNEL_SECURITY_TOKEN_VALUE,
6632 .msgh_audit = KERNEL_AUDIT_TOKEN_VALUE
6633 };
6634
6635 mach_msg_max_trailer_t *trailer;
6636
6637 /*
6638 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
6639 * However, the internal size field of the trailer (msgh_trailer_size)
6640 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
6641 * the cases where no implicit data is requested.
6642 */
6643 trailer = ipc_kmsg_get_trailer(kmsg, false);
6644 if (sender == TASK_NULL) {
6645 memcpy(trailer, &KERNEL_TRAILER_TEMPLATE, sizeof(*trailer));
6646 } else {
6647 bzero(trailer, sizeof(*trailer));
6648 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
6649 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
6650 trailer->msgh_sender = *task_get_sec_token(sender);
6651 trailer->msgh_audit = *task_get_audit_token(sender);
6652 }
6653 }
6654
6655
6656 void
ipc_kmsg_add_trailer(ipc_kmsg_t kmsg,ipc_space_t space __unused,mach_msg_option_t option,__unused thread_t thread,mach_port_seqno_t seqno,boolean_t minimal_trailer,mach_vm_offset_t context)6657 ipc_kmsg_add_trailer(ipc_kmsg_t kmsg, ipc_space_t space __unused,
6658 mach_msg_option_t option, __unused thread_t thread,
6659 mach_port_seqno_t seqno, boolean_t minimal_trailer,
6660 mach_vm_offset_t context)
6661 {
6662 mach_msg_max_trailer_t *trailer;
6663
6664 #ifdef __arm64__
6665 mach_msg_max_trailer_t tmp_trailer; /* This accommodates U64, and we'll munge */
6666
6667 /*
6668 * If we are building a minimal_trailer, that means we have not attempted to
6669 * copy out message body (which converts descriptors to user sizes) because
6670 * we are coming from msg_receive_error().
6671 *
6672 * Adjust trailer calculation accordingly.
6673 */
6674 void *real_trailer_out = (void*)ipc_kmsg_get_trailer(kmsg, !minimal_trailer);
6675
6676 /*
6677 * Populate scratch with initial values set up at message allocation time.
6678 * After, we reinterpret the space in the message as the right type
6679 * of trailer for the address space in question.
6680 */
6681 bcopy(real_trailer_out, &tmp_trailer, MAX_TRAILER_SIZE);
6682 trailer = &tmp_trailer;
6683 #else /* __arm64__ */
6684 (void)thread;
6685 trailer = ipc_kmsg_get_trailer(kmsg, !minimal_trailer);
6686 #endif /* __arm64__ */
6687
6688 if (!(option & MACH_RCV_TRAILER_MASK)) {
6689 return;
6690 }
6691
6692 trailer->msgh_seqno = seqno;
6693 trailer->msgh_context = context;
6694 trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread), option);
6695
6696 if (minimal_trailer) {
6697 goto done;
6698 }
6699
6700 if (GET_RCV_ELEMENTS(option) >= MACH_RCV_TRAILER_AV) {
6701 trailer->msgh_ad = 0;
6702 }
6703
6704 /*
6705 * The ipc_kmsg_t holds a reference to the label of a label
6706 * handle, not the port. We must get a reference to the port
6707 * and a send right to copyout to the receiver.
6708 */
6709
6710 if (option & MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_LABELS)) {
6711 trailer->msgh_labels.sender = 0;
6712 }
6713
6714 done:
6715 #ifdef __arm64__
6716 ipc_kmsg_munge_trailer(trailer, real_trailer_out, thread_is_64bit_addr(thread));
6717 #endif /* __arm64__ */
6718 return;
6719 }
6720
6721 /*
6722 * Get the trailer address of kmsg.
6723 *
6724 * - body_copied_out: Whether ipc_kmsg_copyout_body() has been called.
6725 * If true, descriptors in kmsg has been converted to user size.
6726 *
6727 * /!\ WARNING /!\
6728 * Should not be used after ipc_kmsg_convert_header_to_user() is called.
6729 */
6730 mach_msg_max_trailer_t *
ipc_kmsg_get_trailer(ipc_kmsg_t kmsg,bool body_copied_out)6731 ipc_kmsg_get_trailer(
6732 ipc_kmsg_t kmsg,
6733 bool body_copied_out) /* is kmsg body copyout attempted */
6734 {
6735 mach_msg_header_t *hdr = ikm_header(kmsg);
6736
6737 if (ikm_is_linear(kmsg)) {
6738 return (mach_msg_max_trailer_t *)((vm_offset_t)hdr +
6739 mach_round_msg(hdr->msgh_size));
6740 } else {
6741 assert(kmsg->ikm_udata != NULL);
6742 return (mach_msg_max_trailer_t *)((vm_offset_t)kmsg->ikm_udata +
6743 ikm_content_size(kmsg, current_map(), 0, body_copied_out));
6744 }
6745 }
6746
6747 void
ipc_kmsg_set_voucher_port(ipc_kmsg_t kmsg,ipc_port_t voucher_port,mach_msg_type_name_t type)6748 ipc_kmsg_set_voucher_port(
6749 ipc_kmsg_t kmsg,
6750 ipc_port_t voucher_port,
6751 mach_msg_type_name_t type)
6752 {
6753 if (IP_VALID(voucher_port)) {
6754 assert(ip_kotype(voucher_port) == IKOT_VOUCHER);
6755 }
6756 kmsg->ikm_voucher_port = voucher_port;
6757 kmsg->ikm_voucher_type = type;
6758 }
6759
6760 ipc_port_t
ipc_kmsg_get_voucher_port(ipc_kmsg_t kmsg)6761 ipc_kmsg_get_voucher_port(ipc_kmsg_t kmsg)
6762 {
6763 return kmsg->ikm_voucher_port;
6764 }
6765
6766 void
ipc_kmsg_clear_voucher_port(ipc_kmsg_t kmsg)6767 ipc_kmsg_clear_voucher_port(ipc_kmsg_t kmsg)
6768 {
6769 kmsg->ikm_voucher_port = IP_NULL;
6770 kmsg->ikm_voucher_type = MACH_MSGH_BITS_ZERO;
6771 }
6772