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