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_DEFINE(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 }
1537
1538 /*
1539 * Routine: ipc_kmsg_queue_next
1540 * Purpose:
1541 * Return the kmsg following the given kmsg.
1542 * (Or IKM_NULL if it is the last one in the queue.)
1543 */
1544
1545 ipc_kmsg_t
ipc_kmsg_queue_next(ipc_kmsg_queue_t queue,ipc_kmsg_t kmsg)1546 ipc_kmsg_queue_next(
1547 ipc_kmsg_queue_t queue,
1548 ipc_kmsg_t kmsg)
1549 {
1550 ipc_kmsg_t next;
1551
1552 assert(queue->ikmq_base != IKM_NULL);
1553
1554 next = kmsg->ikm_next;
1555 if (queue->ikmq_base == next) {
1556 next = IKM_NULL;
1557 }
1558
1559 return next;
1560 }
1561
1562 /*
1563 * Routine: ipc_kmsg_destroy
1564 * Purpose:
1565 * Destroys a kernel message. Releases all rights,
1566 * references, and memory held by the message.
1567 * Frees the message.
1568 * Conditions:
1569 * No locks held.
1570 */
1571
1572 void
ipc_kmsg_destroy(ipc_kmsg_t kmsg)1573 ipc_kmsg_destroy(
1574 ipc_kmsg_t kmsg)
1575 {
1576 /*
1577 * Destroying a message can cause more messages to be destroyed.
1578 * Curtail recursion by putting messages on the deferred
1579 * destruction queue. If this was the first message on the
1580 * queue, this instance must process the full queue.
1581 */
1582 if (ipc_kmsg_delayed_destroy(kmsg)) {
1583 ipc_kmsg_reap_delayed();
1584 }
1585 }
1586
1587 /*
1588 * Routine: ipc_kmsg_delayed_destroy
1589 * Purpose:
1590 * Enqueues a kernel message for deferred destruction.
1591 * Returns:
1592 * Boolean indicator that the caller is responsible to reap
1593 * deferred messages.
1594 */
1595
1596 boolean_t
ipc_kmsg_delayed_destroy(ipc_kmsg_t kmsg)1597 ipc_kmsg_delayed_destroy(
1598 ipc_kmsg_t kmsg)
1599 {
1600 ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
1601 boolean_t first = ipc_kmsg_queue_empty(queue);
1602
1603 ipc_kmsg_enqueue(queue, kmsg);
1604 return first;
1605 }
1606
1607 /*
1608 * Routine: ipc_kmsg_delayed_destroy_queue
1609 * Purpose:
1610 * Enqueues a queue of kernel messages for deferred destruction.
1611 * Returns:
1612 * Boolean indicator that the caller is responsible to reap
1613 * deferred messages.
1614 */
1615
1616 boolean_t
ipc_kmsg_delayed_destroy_queue(ipc_kmsg_queue_t queue)1617 ipc_kmsg_delayed_destroy_queue(
1618 ipc_kmsg_queue_t queue)
1619 {
1620 ipc_kmsg_queue_t d_queue = &(current_thread()->ith_messages);
1621 ipc_kmsg_t d_first, d_last;
1622 ipc_kmsg_t first, last;
1623
1624 first = queue->ikmq_base;
1625 if (first == IKM_NULL) {
1626 return FALSE;
1627 }
1628 queue->ikmq_base = IKM_NULL;
1629
1630 d_first = d_queue->ikmq_base;
1631 if (d_first == IKM_NULL) {
1632 d_queue->ikmq_base = first;
1633 return TRUE;
1634 }
1635
1636 last = first->ikm_prev;
1637 d_last = d_first->ikm_prev;
1638
1639 d_last->ikm_next = first;
1640 first->ikm_prev = d_last;
1641
1642 d_first->ikm_prev = last;
1643 last->ikm_next = d_first;
1644
1645 return FALSE;
1646 }
1647
1648 /*
1649 * Routine: ipc_kmsg_reap_delayed
1650 * Purpose:
1651 * Destroys messages from the per-thread
1652 * deferred reaping queue.
1653 * Conditions:
1654 * No locks held.
1655 */
1656
1657 void
ipc_kmsg_reap_delayed(void)1658 ipc_kmsg_reap_delayed(void)
1659 {
1660 ipc_kmsg_queue_t queue = &(current_thread()->ith_messages);
1661 ipc_kmsg_t kmsg;
1662
1663 /*
1664 * must leave kmsg in queue while cleaning it to assure
1665 * no nested calls recurse into here.
1666 */
1667 while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
1668 ipc_kmsg_clean(kmsg);
1669 ipc_kmsg_rmqueue(queue, kmsg);
1670 ipc_kmsg_free(kmsg);
1671 }
1672 }
1673
1674 /*
1675 * Routine: ipc_kmsg_clean_body
1676 * Purpose:
1677 * Cleans the body of a kernel message.
1678 * Releases all rights, references, and memory.
1679 *
1680 * Conditions:
1681 * No locks held.
1682 */
1683 static void
ipc_kmsg_clean_body(__unused ipc_kmsg_t kmsg,mach_msg_type_number_t number,mach_msg_descriptor_t * saddr)1684 ipc_kmsg_clean_body(
1685 __unused ipc_kmsg_t kmsg,
1686 mach_msg_type_number_t number,
1687 mach_msg_descriptor_t *saddr)
1688 {
1689 mach_msg_type_number_t i;
1690
1691 if (number == 0) {
1692 return;
1693 }
1694
1695 for (i = 0; i < number; i++, saddr++) {
1696 switch (saddr->type.type) {
1697 case MACH_MSG_PORT_DESCRIPTOR: {
1698 mach_msg_port_descriptor_t *dsc;
1699
1700 dsc = &saddr->port;
1701
1702 /*
1703 * Destroy port rights carried in the message
1704 */
1705 if (!IP_VALID(dsc->name)) {
1706 continue;
1707 }
1708 ipc_object_destroy(ip_to_object(dsc->name), dsc->disposition);
1709 break;
1710 }
1711 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
1712 case MACH_MSG_OOL_DESCRIPTOR: {
1713 mach_msg_ool_descriptor_t *dsc;
1714
1715 dsc = (mach_msg_ool_descriptor_t *)&saddr->out_of_line;
1716
1717 /*
1718 * Destroy memory carried in the message
1719 */
1720 if (dsc->size == 0) {
1721 assert(dsc->address == (void *) 0);
1722 } else {
1723 vm_map_copy_discard((vm_map_copy_t) dsc->address);
1724 }
1725 break;
1726 }
1727 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
1728 ipc_object_t *objects;
1729 mach_msg_type_number_t j;
1730 mach_msg_ool_ports_descriptor_t *dsc;
1731
1732 dsc = (mach_msg_ool_ports_descriptor_t *)&saddr->ool_ports;
1733 objects = (ipc_object_t *) dsc->address;
1734
1735 if (dsc->count == 0) {
1736 break;
1737 }
1738
1739 assert(objects != (ipc_object_t *) 0);
1740
1741 /* destroy port rights carried in the message */
1742
1743 for (j = 0; j < dsc->count; j++) {
1744 ipc_object_t object = objects[j];
1745
1746 if (!IO_VALID(object)) {
1747 continue;
1748 }
1749
1750 ipc_object_destroy(object, dsc->disposition);
1751 }
1752
1753 /* destroy memory carried in the message */
1754
1755 assert(dsc->count != 0);
1756
1757 kfree_type(mach_port_t, dsc->count, dsc->address);
1758 break;
1759 }
1760 case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
1761 mach_msg_guarded_port_descriptor_t *dsc = (typeof(dsc)) & saddr->guarded_port;
1762
1763 /*
1764 * Destroy port rights carried in the message
1765 */
1766 if (!IP_VALID(dsc->name)) {
1767 continue;
1768 }
1769 ipc_object_destroy(ip_to_object(dsc->name), dsc->disposition);
1770 break;
1771 }
1772 default:
1773 panic("invalid descriptor type: (%p: %d)",
1774 saddr, saddr->type.type);
1775 }
1776 }
1777 }
1778
1779 /*
1780 * Routine: ipc_kmsg_clean_partial
1781 * Purpose:
1782 * Cleans a partially-acquired kernel message.
1783 * number is the index of the type descriptor
1784 * in the body of the message that contained the error.
1785 * If dolast, the memory and port rights in this last
1786 * type spec are also cleaned. In that case, number
1787 * specifies the number of port rights to clean.
1788 * Conditions:
1789 * Nothing locked.
1790 */
1791
1792 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)1793 ipc_kmsg_clean_partial(
1794 ipc_kmsg_t kmsg,
1795 mach_msg_type_number_t number,
1796 mach_msg_descriptor_t *desc,
1797 vm_offset_t paddr,
1798 vm_size_t length)
1799 {
1800 ipc_object_t object;
1801 mach_msg_bits_t mbits = kmsg->ikm_header->msgh_bits;
1802
1803 /* deal with importance chain while we still have dest and voucher references */
1804 ipc_importance_clean(kmsg);
1805
1806 object = ip_to_object(kmsg->ikm_header->msgh_remote_port);
1807 assert(IO_VALID(object));
1808 ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
1809
1810 object = ip_to_object(kmsg->ikm_header->msgh_local_port);
1811 if (IO_VALID(object)) {
1812 ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
1813 }
1814
1815 object = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
1816 if (IO_VALID(object)) {
1817 assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
1818 ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
1819 ipc_kmsg_clear_voucher_port(kmsg);
1820 }
1821
1822 if (paddr) {
1823 kmem_free(ipc_kernel_copy_map, paddr, length);
1824 }
1825
1826 ipc_kmsg_clean_body(kmsg, number, desc);
1827 }
1828
1829 /*
1830 * Routine: ipc_kmsg_clean
1831 * Purpose:
1832 * Cleans a kernel message. Releases all rights,
1833 * references, and memory held by the message.
1834 * Conditions:
1835 * No locks held.
1836 */
1837
1838 static void
ipc_kmsg_clean(ipc_kmsg_t kmsg)1839 ipc_kmsg_clean(
1840 ipc_kmsg_t kmsg)
1841 {
1842 ipc_object_t object;
1843 mach_msg_bits_t mbits;
1844
1845 /* deal with importance chain while we still have dest and voucher references */
1846 ipc_importance_clean(kmsg);
1847
1848 mbits = kmsg->ikm_header->msgh_bits;
1849 object = ip_to_object(kmsg->ikm_header->msgh_remote_port);
1850 if (IO_VALID(object)) {
1851 ipc_object_destroy_dest(object, MACH_MSGH_BITS_REMOTE(mbits));
1852 }
1853
1854 object = ip_to_object(kmsg->ikm_header->msgh_local_port);
1855 if (IO_VALID(object)) {
1856 ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
1857 }
1858
1859 object = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
1860 if (IO_VALID(object)) {
1861 assert(MACH_MSGH_BITS_VOUCHER(mbits) == MACH_MSG_TYPE_MOVE_SEND);
1862 ipc_object_destroy(object, MACH_MSG_TYPE_PORT_SEND);
1863 ipc_kmsg_clear_voucher_port(kmsg);
1864 }
1865
1866 if (mbits & MACH_MSGH_BITS_COMPLEX) {
1867 mach_msg_body_t *body;
1868
1869 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
1870 ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
1871 (mach_msg_descriptor_t *)(body + 1));
1872 }
1873 }
1874
1875 /*
1876 * Routine: ipc_kmsg_set_prealloc
1877 * Purpose:
1878 * Assign a kmsg as a preallocated message buffer to a port.
1879 * Conditions:
1880 * port locked.
1881 */
1882
1883 void
ipc_kmsg_set_prealloc(ipc_kmsg_t kmsg,ipc_port_t port)1884 ipc_kmsg_set_prealloc(
1885 ipc_kmsg_t kmsg,
1886 ipc_port_t port)
1887 {
1888 assert(kmsg->ikm_prealloc == IP_NULL);
1889
1890 kmsg->ikm_prealloc = IP_NULL;
1891
1892 assert(port_send_turnstile(port) == TURNSTILE_NULL);
1893 kmsg->ikm_turnstile = TURNSTILE_NULL;
1894 IP_SET_PREALLOC(port, kmsg);
1895 }
1896
1897 /*
1898 * Routine: ipc_kmsg_get_from_user
1899 * Purpose:
1900 * Allocates a kernel message buffer.
1901 * Copies a user message to the message buffer.
1902 * Conditions:
1903 * Nothing locked.
1904 * Returns:
1905 * MACH_MSG_SUCCESS Acquired a message buffer.
1906 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
1907 * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple.
1908 * MACH_SEND_TOO_LARGE Message too large to ever be sent.
1909 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
1910 * MACH_SEND_INVALID_DATA Couldn't copy message data.
1911 */
1912
1913 mach_msg_return_t
ipc_kmsg_get_from_user(mach_vm_address_t msg_addr,mach_msg_size_t size,ipc_kmsg_t * kmsgp)1914 ipc_kmsg_get_from_user(
1915 mach_vm_address_t msg_addr,
1916 mach_msg_size_t size,
1917 ipc_kmsg_t *kmsgp)
1918 {
1919 ipc_kmsg_t kmsg;
1920 mach_msg_user_base_t user_base;
1921 mach_msg_size_t len_copied;
1922 mach_msg_size_t descriptors;
1923
1924 if ((size < sizeof(mach_msg_user_header_t)) || (size & 3)) {
1925 return MACH_SEND_MSG_TOO_SMALL;
1926 }
1927
1928 if (size > ipc_kmsg_max_body_space) {
1929 return MACH_SEND_TOO_LARGE;
1930 }
1931
1932 if (size == sizeof(mach_msg_user_header_t)) {
1933 len_copied = sizeof(mach_msg_user_header_t);
1934 } else {
1935 len_copied = sizeof(mach_msg_user_base_t);
1936 }
1937
1938 user_base.body.msgh_descriptor_count = descriptors = 0;
1939 if (copyinmsg(msg_addr, (char *)&user_base, len_copied)) {
1940 return MACH_SEND_INVALID_DATA;
1941 }
1942
1943 /*
1944 * If the message claims to be complex, it must at least
1945 * have the length of a "base" message (header + dsc_count).
1946 */
1947 if (user_base.header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
1948 if (len_copied < sizeof(mach_msg_user_base_t)) {
1949 return MACH_SEND_MSG_TOO_SMALL;
1950 }
1951 descriptors = user_base.body.msgh_descriptor_count;
1952 if (descriptors > (size - len_copied) / MACH_MSG_DESC_MIN_SIZE) {
1953 return MACH_SEND_MSG_TOO_SMALL;
1954 }
1955 }
1956
1957 #if defined(__LP64__)
1958 size += USER_HEADER_SIZE_DELTA;
1959 #endif
1960 /* unreachable if !DEBUG */
1961 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
1962 __unreachable_ok_push
1963 for (uint32_t j = 0; j < sizeof(user_base.header); j++) {
1964 kprintf("%02x\n", ((unsigned char*)&user_base.header)[j]);
1965 }
1966 __unreachable_ok_pop
1967 }
1968
1969 kmsg = ipc_kmsg_alloc(size, descriptors, IPC_KMSG_ALLOC_USER);
1970 if (kmsg == IKM_NULL) {
1971 return MACH_SEND_NO_BUFFER;
1972 }
1973
1974 kmsg->ikm_header->msgh_size = size;
1975 kmsg->ikm_header->msgh_bits = user_base.header.msgh_bits;
1976 kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(user_base.header.msgh_remote_port);
1977 kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(user_base.header.msgh_local_port);
1978 kmsg->ikm_header->msgh_voucher_port = user_base.header.msgh_voucher_port;
1979 kmsg->ikm_header->msgh_id = user_base.header.msgh_id;
1980
1981 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_get_from_user header:\n"
1982 " size: 0x%.8x\n"
1983 " bits: 0x%.8x\n"
1984 " remote_port: %p\n"
1985 " local_port: %p\n"
1986 " voucher_port: 0x%.8x\n"
1987 " id: %.8d\n",
1988 kmsg->ikm_header->msgh_size,
1989 kmsg->ikm_header->msgh_bits,
1990 kmsg->ikm_header->msgh_remote_port,
1991 kmsg->ikm_header->msgh_local_port,
1992 kmsg->ikm_header->msgh_voucher_port,
1993 kmsg->ikm_header->msgh_id);
1994
1995 if (size >= sizeof(mach_msg_base_t)) {
1996 mach_msg_base_t *kbase = ((mach_msg_base_t *)kmsg->ikm_header);
1997
1998 kbase->body.msgh_descriptor_count =
1999 user_base.body.msgh_descriptor_count;
2000 }
2001
2002 if (size > sizeof(mach_msg_base_t)) {
2003 if (copyinmsg(msg_addr + sizeof(mach_msg_user_base_t),
2004 (char *)kmsg->ikm_header + sizeof(mach_msg_base_t),
2005 size - sizeof(mach_msg_base_t))) {
2006 ipc_kmsg_free(kmsg);
2007 return MACH_SEND_INVALID_DATA;
2008 }
2009 }
2010
2011 /* unreachable if !DEBUG */
2012 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
2013 __unreachable_ok_push
2014 kprintf("body: size: %lu\n", (size - sizeof(mach_msg_header_t)));
2015 for (uint32_t i = 0; i * 4 < (size - sizeof(mach_msg_header_t)); i++) {
2016 kprintf("%.4x\n", ((uint32_t *)(kmsg->ikm_header + 1))[i]);
2017 }
2018 __unreachable_ok_pop
2019 }
2020 DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_get_from_user()");
2021
2022 *kmsgp = kmsg;
2023 return MACH_MSG_SUCCESS;
2024 }
2025
2026 /*
2027 * Routine: ipc_kmsg_get_from_kernel
2028 * Purpose:
2029 * First checks for a preallocated message
2030 * reserved for kernel clients. If not found or size is too large -
2031 * allocates a new kernel message buffer.
2032 * Copies a kernel message to the message buffer.
2033 * Only resource errors are allowed.
2034 * Conditions:
2035 * Nothing locked.
2036 * Ports in header are ipc_port_t.
2037 * Returns:
2038 * MACH_MSG_SUCCESS Acquired a message buffer.
2039 * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer.
2040 */
2041
2042 mach_msg_return_t
ipc_kmsg_get_from_kernel(mach_msg_header_t * msg,mach_msg_size_t size,ipc_kmsg_t * kmsgp)2043 ipc_kmsg_get_from_kernel(
2044 mach_msg_header_t *msg,
2045 mach_msg_size_t size, /* can be larger than prealloc space */
2046 ipc_kmsg_t *kmsgp)
2047 {
2048 ipc_kmsg_t kmsg;
2049 ipc_port_t dest_port;
2050
2051 assert(size >= sizeof(mach_msg_header_t));
2052 assert((size & 3) == 0);
2053
2054 dest_port = msg->msgh_remote_port;
2055
2056 /*
2057 * See if the port has a pre-allocated kmsg for kernel
2058 * clients. These are set up for those kernel clients
2059 * which cannot afford to wait.
2060 */
2061 if (IP_VALID(dest_port) && IP_PREALLOC(dest_port)) {
2062 ip_mq_lock(dest_port);
2063 if (!ip_active(dest_port)) {
2064 ip_mq_unlock(dest_port);
2065 return MACH_SEND_NO_BUFFER;
2066 }
2067 assert(IP_PREALLOC(dest_port));
2068 kmsg = dest_port->ip_premsg;
2069 if (ikm_prealloc_inuse(kmsg)) {
2070 ip_mq_unlock(dest_port);
2071 return MACH_SEND_NO_BUFFER;
2072 }
2073 assert(kmsg->ikm_size == IKM_SAVED_MSG_SIZE);
2074 if (size + MAX_TRAILER_SIZE > kmsg->ikm_size) {
2075 ip_mq_unlock(dest_port);
2076 return MACH_SEND_TOO_LARGE;
2077 }
2078 ikm_prealloc_set_inuse(kmsg, dest_port);
2079 ikm_set_header(kmsg, NULL, size);
2080 ip_mq_unlock(dest_port);
2081 } else {
2082 kmsg = ipc_kmsg_alloc(size, 0, IPC_KMSG_ALLOC_KERNEL);
2083 if (kmsg == IKM_NULL) {
2084 return MACH_SEND_NO_BUFFER;
2085 }
2086 }
2087
2088 memcpy(kmsg->ikm_header, msg, size);
2089 kmsg->ikm_header->msgh_size = size;
2090
2091 *kmsgp = kmsg;
2092 return MACH_MSG_SUCCESS;
2093 }
2094
2095 /*
2096 * Routine: ipc_kmsg_send
2097 * Purpose:
2098 * Send a message. The message holds a reference
2099 * for the destination port in the msgh_remote_port field.
2100 *
2101 * If unsuccessful, the caller still has possession of
2102 * the message and must do something with it. If successful,
2103 * the message is queued, given to a receiver, destroyed,
2104 * or handled directly by the kernel via mach_msg.
2105 * Conditions:
2106 * Nothing locked.
2107 * Returns:
2108 * MACH_MSG_SUCCESS The message was accepted.
2109 * MACH_SEND_TIMED_OUT Caller still has message.
2110 * MACH_SEND_INTERRUPTED Caller still has message.
2111 * MACH_SEND_INVALID_DEST Caller still has message.
2112 */
2113 mach_msg_return_t
ipc_kmsg_send(ipc_kmsg_t kmsg,mach_msg_option_t option,mach_msg_timeout_t send_timeout)2114 ipc_kmsg_send(
2115 ipc_kmsg_t kmsg,
2116 mach_msg_option_t option,
2117 mach_msg_timeout_t send_timeout)
2118 {
2119 ipc_port_t port;
2120 thread_t th = current_thread();
2121 mach_msg_return_t error = MACH_MSG_SUCCESS;
2122 boolean_t kernel_reply = FALSE;
2123
2124 /* Check if honor qlimit flag is set on thread. */
2125 if ((th->options & TH_OPT_HONOR_QLIMIT) == TH_OPT_HONOR_QLIMIT) {
2126 /* Remove the MACH_SEND_ALWAYS flag to honor queue limit. */
2127 option &= (~MACH_SEND_ALWAYS);
2128 /* Add the timeout flag since the message queue might be full. */
2129 option |= MACH_SEND_TIMEOUT;
2130 th->options &= (~TH_OPT_HONOR_QLIMIT);
2131 }
2132
2133 #if IMPORTANCE_INHERITANCE
2134 bool did_importance = false;
2135 #if IMPORTANCE_TRACE
2136 mach_msg_id_t imp_msgh_id = -1;
2137 int sender_pid = -1;
2138 #endif /* IMPORTANCE_TRACE */
2139 #endif /* IMPORTANCE_INHERITANCE */
2140
2141 /* don't allow the creation of a circular loop */
2142 if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
2143 ipc_kmsg_destroy(kmsg);
2144 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_MSGH_BITS_CIRCULAR);
2145 return MACH_MSG_SUCCESS;
2146 }
2147
2148 ipc_voucher_send_preprocessing(kmsg);
2149
2150 port = kmsg->ikm_header->msgh_remote_port;
2151 assert(IP_VALID(port));
2152 ip_mq_lock(port);
2153
2154 /*
2155 * If the destination has been guarded with a reply context, and the
2156 * sender is consuming a send-once right, then assume this is a reply
2157 * to an RPC and we need to validate that this sender is currently in
2158 * the correct context.
2159 */
2160 if (enforce_strict_reply && port->ip_reply_context != 0 &&
2161 ((option & MACH_SEND_KERNEL) == 0) &&
2162 MACH_MSGH_BITS_REMOTE(kmsg->ikm_header->msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE) {
2163 error = ipc_kmsg_validate_reply_context_locked(option, port, th->ith_voucher, th->ith_voucher_name);
2164 if (error != MACH_MSG_SUCCESS) {
2165 ip_mq_unlock(port);
2166 return error;
2167 }
2168 }
2169
2170 #if IMPORTANCE_INHERITANCE
2171 retry:
2172 #endif /* IMPORTANCE_INHERITANCE */
2173 /*
2174 * Can't deliver to a dead port.
2175 * However, we can pretend it got sent
2176 * and was then immediately destroyed.
2177 */
2178 if (!ip_active(port)) {
2179 ip_mq_unlock(port);
2180 #if MACH_FLIPC
2181 if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2182 flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2183 }
2184 #endif
2185 if (did_importance) {
2186 /*
2187 * We're going to pretend we delivered this message
2188 * successfully, and just eat the kmsg. However, the
2189 * kmsg is actually visible via the importance_task!
2190 * We need to cleanup this linkage before we destroy
2191 * the message, and more importantly before we set the
2192 * msgh_remote_port to NULL. See: 34302571
2193 */
2194 ipc_importance_clean(kmsg);
2195 }
2196 ip_release(port); /* JMM - Future: release right, not just ref */
2197 kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
2198 ipc_kmsg_destroy(kmsg);
2199 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_INVALID_DEST);
2200 return MACH_MSG_SUCCESS;
2201 }
2202
2203 if (ip_in_space(port, ipc_space_kernel)) {
2204 require_ip_active(port);
2205 port->ip_messages.imq_seqno++;
2206 ip_mq_unlock(port);
2207
2208 counter_inc(¤t_task()->messages_sent);
2209
2210 /*
2211 * Call the server routine, and get the reply message to send.
2212 */
2213 kmsg = ipc_kobject_server(port, kmsg, option);
2214 if (kmsg == IKM_NULL) {
2215 return MACH_MSG_SUCCESS;
2216 }
2217
2218 /* sign the reply message */
2219 ikm_sign(kmsg, TASK_NULL);
2220
2221 /* restart the KMSG_INFO tracing for the reply message */
2222 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
2223 port = kmsg->ikm_header->msgh_remote_port;
2224 assert(IP_VALID(port));
2225 ip_mq_lock(port);
2226 /* fall thru with reply - same options */
2227 kernel_reply = TRUE;
2228 if (!ip_active(port)) {
2229 error = MACH_SEND_INVALID_DEST;
2230 }
2231 }
2232
2233 #if IMPORTANCE_INHERITANCE
2234 /*
2235 * Need to see if this message needs importance donation and/or
2236 * propagation. That routine can drop the port lock temporarily.
2237 * If it does we'll have to revalidate the destination.
2238 */
2239 if (!did_importance) {
2240 did_importance = true;
2241 if (ipc_importance_send(kmsg, option)) {
2242 goto retry;
2243 }
2244 }
2245 #endif /* IMPORTANCE_INHERITANCE */
2246
2247 if (error != MACH_MSG_SUCCESS) {
2248 ip_mq_unlock(port);
2249 } else {
2250 /*
2251 * We have a valid message and a valid reference on the port.
2252 * call mqueue_send() on its message queue.
2253 */
2254 ipc_special_reply_port_msg_sent(port);
2255
2256 error = ipc_mqueue_send_locked(&port->ip_messages, kmsg, option,
2257 send_timeout);
2258 /* port unlocked */
2259 }
2260
2261 #if IMPORTANCE_INHERITANCE
2262 if (did_importance) {
2263 __unused int importance_cleared = 0;
2264 switch (error) {
2265 case MACH_SEND_TIMED_OUT:
2266 case MACH_SEND_NO_BUFFER:
2267 case MACH_SEND_INTERRUPTED:
2268 case MACH_SEND_INVALID_DEST:
2269 /*
2270 * We still have the kmsg and its
2271 * reference on the port. But we
2272 * have to back out the importance
2273 * boost.
2274 *
2275 * The port could have changed hands,
2276 * be inflight to another destination,
2277 * etc... But in those cases our
2278 * back-out will find the new owner
2279 * (and all the operations that
2280 * transferred the right should have
2281 * applied their own boost adjustments
2282 * to the old owner(s)).
2283 */
2284 importance_cleared = 1;
2285 ipc_importance_clean(kmsg);
2286 break;
2287
2288 case MACH_MSG_SUCCESS:
2289 default:
2290 break;
2291 }
2292 #if IMPORTANCE_TRACE
2293 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, (IMPORTANCE_CODE(IMP_MSG, IMP_MSG_SEND)) | DBG_FUNC_END,
2294 task_pid(current_task()), sender_pid, imp_msgh_id, importance_cleared, 0);
2295 #endif /* IMPORTANCE_TRACE */
2296 }
2297 #endif /* IMPORTANCE_INHERITANCE */
2298
2299 /*
2300 * If the port has been destroyed while we wait, treat the message
2301 * as a successful delivery (like we do for an inactive port).
2302 */
2303 if (error == MACH_SEND_INVALID_DEST) {
2304 #if MACH_FLIPC
2305 if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2306 flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2307 }
2308 #endif
2309 ip_release(port); /* JMM - Future: release right, not just ref */
2310 kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
2311 ipc_kmsg_destroy(kmsg);
2312 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_INVALID_DEST);
2313 return MACH_MSG_SUCCESS;
2314 }
2315
2316 if (error != MACH_MSG_SUCCESS && kernel_reply) {
2317 /*
2318 * Kernel reply messages that fail can't be allowed to
2319 * pseudo-receive on error conditions. We need to just treat
2320 * the message as a successful delivery.
2321 */
2322 #if MACH_FLIPC
2323 if (MACH_NODE_VALID(kmsg->ikm_node) && FPORT_VALID(port->ip_messages.imq_fport)) {
2324 flipc_msg_ack(kmsg->ikm_node, &port->ip_messages, FALSE);
2325 }
2326 #endif
2327 ip_release(port); /* JMM - Future: release right, not just ref */
2328 kmsg->ikm_header->msgh_remote_port = MACH_PORT_NULL;
2329 ipc_kmsg_destroy(kmsg);
2330 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, error);
2331 return MACH_MSG_SUCCESS;
2332 }
2333 return error;
2334 }
2335
2336 /*
2337 * Routine: ipc_kmsg_put_to_user
2338 * Purpose:
2339 * Copies a message buffer to a user message.
2340 * Copies only the specified number of bytes.
2341 * Frees the message buffer.
2342 * Conditions:
2343 * Nothing locked. The message buffer must have clean
2344 * header fields.
2345 * Returns:
2346 * MACH_MSG_SUCCESS Copied data out of message buffer.
2347 * MACH_RCV_INVALID_DATA Couldn't copy to user message.
2348 */
2349
2350 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)2351 ipc_kmsg_put_to_user(
2352 ipc_kmsg_t kmsg,
2353 mach_msg_option_t option,
2354 mach_vm_address_t rcv_addr,
2355 mach_msg_size_t rcv_size,
2356 mach_msg_size_t trailer_size,
2357 mach_msg_size_t *sizep)
2358 {
2359 mach_msg_size_t size = kmsg->ikm_header->msgh_size + trailer_size;
2360 mach_msg_return_t mr;
2361
2362 DEBUG_IPC_KMSG_PRINT(kmsg, "ipc_kmsg_put_to_user()");
2363
2364
2365 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_put_to_user header:\n"
2366 " size: 0x%.8x\n"
2367 " bits: 0x%.8x\n"
2368 " remote_port: %p\n"
2369 " local_port: %p\n"
2370 " voucher_port: 0x%.8x\n"
2371 " id: %.8d\n",
2372 kmsg->ikm_header->msgh_size,
2373 kmsg->ikm_header->msgh_bits,
2374 kmsg->ikm_header->msgh_remote_port,
2375 kmsg->ikm_header->msgh_local_port,
2376 kmsg->ikm_header->msgh_voucher_port,
2377 kmsg->ikm_header->msgh_id);
2378
2379 assert(current_task() != kernel_task);
2380
2381 #if defined(__LP64__)
2382 {
2383 mach_msg_user_header_t *user_header =
2384 (mach_msg_user_header_t *)((vm_offset_t)(kmsg->ikm_header) + USER_HEADER_SIZE_DELTA);
2385
2386 mach_msg_bits_t bits = kmsg->ikm_header->msgh_bits;
2387 mach_msg_size_t msg_size = kmsg->ikm_header->msgh_size;
2388 mach_port_name_t remote_port = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_remote_port);
2389 mach_port_name_t local_port = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_local_port);
2390 mach_port_name_t voucher_port = kmsg->ikm_header->msgh_voucher_port;
2391 mach_msg_id_t id = kmsg->ikm_header->msgh_id;
2392
2393 user_header->msgh_id = id;
2394 user_header->msgh_local_port = local_port;
2395 user_header->msgh_remote_port = remote_port;
2396 user_header->msgh_voucher_port = voucher_port;
2397 user_header->msgh_size = msg_size - USER_HEADER_SIZE_DELTA;
2398 user_header->msgh_bits = bits;
2399
2400 size -= USER_HEADER_SIZE_DELTA;
2401 kmsg->ikm_header = (mach_msg_header_t *)user_header;
2402 }
2403 #endif
2404
2405 /* unreachable if !DEBUG */
2406 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
2407 __unreachable_ok_push
2408 kprintf("ipc_kmsg_put_to_user header+body: %d\n", (size));
2409 for (uint32_t i = 0; i * 4 < size; i++) {
2410 kprintf("%.4x\n", ((uint32_t *)kmsg->ikm_header)[i]);
2411 }
2412 kprintf("type: %d\n", ((mach_msg_type_descriptor_t *)(((mach_msg_base_t *)kmsg->ikm_header) + 1))->type);
2413 __unreachable_ok_pop
2414 }
2415
2416 /*
2417 * (81193887) some clients stomp their own stack due to mis-sized
2418 * combined send/receives where the receive buffer didn't account
2419 * for the trailer size.
2420 *
2421 * At the very least, avoid smashint their stack.
2422 */
2423 if (size > rcv_size) {
2424 size = rcv_size;
2425 }
2426
2427 /* Re-Compute target address if using stack-style delivery */
2428 if (option & MACH_RCV_STACK) {
2429 rcv_addr += rcv_size - size;
2430 }
2431
2432 if (copyoutmsg((const char *) kmsg->ikm_header, rcv_addr, size)) {
2433 mr = MACH_RCV_INVALID_DATA;
2434 size = 0;
2435 } else {
2436 mr = MACH_MSG_SUCCESS;
2437 }
2438
2439 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
2440 (rcv_addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS ||
2441 rcv_addr + size >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) ? (uintptr_t)0 : (uintptr_t)rcv_addr,
2442 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
2443 1 /* this is on the receive/copyout path */,
2444 0,
2445 0);
2446 ipc_kmsg_free(kmsg);
2447
2448 if (sizep) {
2449 *sizep = size;
2450 }
2451 return mr;
2452 }
2453
2454 /*
2455 * Routine: ipc_kmsg_put_to_kernel
2456 * Purpose:
2457 * Copies a message buffer to a kernel message.
2458 * Frees the message buffer.
2459 * No errors allowed.
2460 * Conditions:
2461 * Nothing locked.
2462 */
2463
2464 void
ipc_kmsg_put_to_kernel(mach_msg_header_t * msg,ipc_kmsg_t kmsg,mach_msg_size_t size)2465 ipc_kmsg_put_to_kernel(
2466 mach_msg_header_t *msg,
2467 ipc_kmsg_t kmsg,
2468 mach_msg_size_t size)
2469 {
2470 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, size);
2471
2472 ipc_kmsg_free(kmsg);
2473 }
2474
2475 static pthread_priority_compact_t
ipc_get_current_thread_priority(void)2476 ipc_get_current_thread_priority(void)
2477 {
2478 thread_t thread = current_thread();
2479 thread_qos_t qos;
2480 int relpri;
2481
2482 qos = thread_get_requested_qos(thread, &relpri);
2483 if (!qos) {
2484 qos = thread_user_promotion_qos_for_pri(thread->base_pri);
2485 relpri = 0;
2486 }
2487 return _pthread_priority_make_from_thread_qos(qos, relpri, 0);
2488 }
2489
2490 static kern_return_t
ipc_kmsg_set_qos(ipc_kmsg_t kmsg,mach_msg_option_t options,mach_msg_priority_t priority)2491 ipc_kmsg_set_qos(
2492 ipc_kmsg_t kmsg,
2493 mach_msg_option_t options,
2494 mach_msg_priority_t priority)
2495 {
2496 kern_return_t kr;
2497 ipc_port_t special_reply_port = kmsg->ikm_header->msgh_local_port;
2498 ipc_port_t dest_port = kmsg->ikm_header->msgh_remote_port;
2499
2500 if ((options & MACH_SEND_OVERRIDE) &&
2501 !mach_msg_priority_is_pthread_priority(priority)) {
2502 mach_msg_qos_t qos = mach_msg_priority_qos(priority);
2503 int relpri = mach_msg_priority_relpri(priority);
2504 mach_msg_qos_t ovr = mach_msg_priority_overide_qos(priority);
2505
2506 kmsg->ikm_ppriority = _pthread_priority_make_from_thread_qos(qos, relpri, 0);
2507 kmsg->ikm_qos_override = MAX(qos, ovr);
2508 } else {
2509 #if CONFIG_VOUCHER_DEPRECATED
2510 kr = ipc_get_pthpriority_from_kmsg_voucher(kmsg, &kmsg->ikm_ppriority);
2511 #else
2512 kr = KERN_FAILURE;
2513 #endif /* CONFIG_VOUCHER_DEPRECATED */
2514 if (kr != KERN_SUCCESS) {
2515 if (options & MACH_SEND_PROPAGATE_QOS) {
2516 kmsg->ikm_ppriority = ipc_get_current_thread_priority();
2517 } else {
2518 kmsg->ikm_ppriority = MACH_MSG_PRIORITY_UNSPECIFIED;
2519 }
2520 }
2521
2522 if (options & MACH_SEND_OVERRIDE) {
2523 mach_msg_qos_t qos = _pthread_priority_thread_qos(kmsg->ikm_ppriority);
2524 mach_msg_qos_t ovr = _pthread_priority_thread_qos(priority);
2525 kmsg->ikm_qos_override = MAX(qos, ovr);
2526 } else {
2527 kmsg->ikm_qos_override = _pthread_priority_thread_qos(kmsg->ikm_ppriority);
2528 }
2529 }
2530
2531 kr = KERN_SUCCESS;
2532
2533 if (IP_VALID(special_reply_port) &&
2534 special_reply_port->ip_specialreply &&
2535 !ip_is_kobject(dest_port) &&
2536 MACH_MSGH_BITS_LOCAL(kmsg->ikm_header->msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE) {
2537 boolean_t sync_bootstrap_checkin = !!(options & MACH_SEND_SYNC_BOOTSTRAP_CHECKIN);
2538 /*
2539 * Link the destination port to special reply port and make sure that
2540 * dest port has a send turnstile, else allocate one.
2541 */
2542 ipc_port_link_special_reply_port(special_reply_port, dest_port, sync_bootstrap_checkin);
2543 }
2544 return kr;
2545 }
2546
2547 static kern_return_t
ipc_kmsg_set_qos_kernel(ipc_kmsg_t kmsg)2548 ipc_kmsg_set_qos_kernel(
2549 ipc_kmsg_t kmsg)
2550 {
2551 ipc_port_t dest_port = kmsg->ikm_header->msgh_remote_port;
2552 kmsg->ikm_qos_override = dest_port->ip_kernel_qos_override;
2553 kmsg->ikm_ppriority = _pthread_priority_make_from_thread_qos(kmsg->ikm_qos_override, 0, 0);
2554 return KERN_SUCCESS;
2555 }
2556
2557 /*
2558 * Routine: ipc_kmsg_link_reply_context_locked
2559 * Purpose:
2560 * Link any required context from the sending voucher
2561 * to the reply port. The ipc_kmsg_copyin_from_user function will
2562 * enforce that the sender calls mach_msg in this context.
2563 * Conditions:
2564 * reply port is locked
2565 */
2566 static void
ipc_kmsg_link_reply_context_locked(ipc_port_t reply_port,ipc_port_t voucher_port)2567 ipc_kmsg_link_reply_context_locked(
2568 ipc_port_t reply_port,
2569 ipc_port_t voucher_port)
2570 {
2571 kern_return_t __assert_only kr;
2572 uint32_t persona_id = 0;
2573 ipc_voucher_t voucher;
2574
2575 ip_mq_lock_held(reply_port);
2576
2577 if (!ip_active(reply_port)) {
2578 return;
2579 }
2580
2581 voucher = convert_port_to_voucher(voucher_port);
2582
2583 kr = bank_get_bank_ledger_thread_group_and_persona(voucher, NULL, NULL, &persona_id);
2584 assert(kr == KERN_SUCCESS);
2585 ipc_voucher_release(voucher);
2586
2587 if (persona_id == 0 || persona_id == PERSONA_ID_NONE) {
2588 /* there was no persona context to record */
2589 return;
2590 }
2591
2592 /*
2593 * Set the persona_id as the context on the reply port.
2594 * This will force the thread that replies to have adopted a voucher
2595 * with a matching persona.
2596 */
2597 reply_port->ip_reply_context = persona_id;
2598
2599 return;
2600 }
2601
2602 static kern_return_t
ipc_kmsg_validate_reply_port_locked(ipc_port_t reply_port,mach_msg_option_t options)2603 ipc_kmsg_validate_reply_port_locked(ipc_port_t reply_port, mach_msg_option_t options)
2604 {
2605 ip_mq_lock_held(reply_port);
2606
2607 if (!ip_active(reply_port)) {
2608 /*
2609 * Ideally, we would enforce that the reply receive right is
2610 * active, but asynchronous XPC cancellation destroys the
2611 * receive right, so we just have to return success here.
2612 */
2613 return KERN_SUCCESS;
2614 }
2615
2616 if (options & MACH_SEND_MSG) {
2617 /*
2618 * If the rely port is active, then it should not be
2619 * in-transit, and the receive right should be in the caller's
2620 * IPC space.
2621 */
2622 if (!ip_in_space(reply_port, current_task()->itk_space)) {
2623 return KERN_INVALID_CAPABILITY;
2624 }
2625
2626 /*
2627 * A port used as a reply port in an RPC should have exactly 1
2628 * extant send-once right which we either just made or are
2629 * moving as part of the IPC.
2630 */
2631 if (reply_port->ip_sorights != 1) {
2632 return KERN_INVALID_CAPABILITY;
2633 }
2634 /*
2635 * XPC uses an extra send-right to keep the name of the reply
2636 * right around through cancellation. That makes it harder to
2637 * enforce a particular semantic kere, so for now, we say that
2638 * you can have a maximum of 1 send right (in addition to your
2639 * send once right). In the future, it would be great to lock
2640 * this down even further.
2641 */
2642 if (reply_port->ip_srights > 1) {
2643 return KERN_INVALID_CAPABILITY;
2644 }
2645
2646 /*
2647 * The sender can also specify that the receive right should
2648 * be immovable. Note that this check only applies to
2649 * send-only operations. Combined send/receive or rcv-only
2650 * operations can specify an immovable receive right by
2651 * opt-ing into guarded descriptors (MACH_RCV_GUARDED_DESC)
2652 * and using the MACH_MSG_STRICT_REPLY options flag.
2653 */
2654 if (MACH_SEND_REPLY_IS_IMMOVABLE(options)) {
2655 if (!reply_port->ip_immovable_receive) {
2656 return KERN_INVALID_CAPABILITY;
2657 }
2658 }
2659 }
2660
2661 /*
2662 * don't enforce this yet: need a better way of indicating the
2663 * receiver wants this...
2664 */
2665 #if 0
2666 if (MACH_RCV_WITH_IMMOVABLE_REPLY(options)) {
2667 if (!reply_port->ip_immovable_receive) {
2668 return KERN_INVALID_CAPABILITY;
2669 }
2670 }
2671 #endif /* 0 */
2672
2673 return KERN_SUCCESS;
2674 }
2675
2676 /*
2677 * Routine: ipc_kmsg_validate_reply_context_locked
2678 * Purpose:
2679 * Validate that the current thread is running in the context
2680 * required by the destination port.
2681 * Conditions:
2682 * dest_port is locked
2683 * Returns:
2684 * MACH_MSG_SUCCESS on success.
2685 * On error, an EXC_GUARD exception is also raised.
2686 * This function *always* resets the port reply context.
2687 */
2688 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)2689 ipc_kmsg_validate_reply_context_locked(
2690 mach_msg_option_t option,
2691 ipc_port_t dest_port,
2692 ipc_voucher_t voucher,
2693 mach_port_name_t voucher_name)
2694 {
2695 uint32_t dest_ctx = dest_port->ip_reply_context;
2696 dest_port->ip_reply_context = 0;
2697
2698 if (!ip_active(dest_port)) {
2699 return MACH_MSG_SUCCESS;
2700 }
2701
2702 if (voucher == IPC_VOUCHER_NULL || !MACH_PORT_VALID(voucher_name)) {
2703 if ((option & MACH_SEND_KERNEL) == 0) {
2704 mach_port_guard_exception(voucher_name, 0,
2705 (MPG_FLAGS_STRICT_REPLY_INVALID_VOUCHER | dest_ctx),
2706 kGUARD_EXC_STRICT_REPLY);
2707 }
2708 return MACH_SEND_INVALID_CONTEXT;
2709 }
2710
2711 kern_return_t __assert_only kr;
2712 uint32_t persona_id = 0;
2713 kr = bank_get_bank_ledger_thread_group_and_persona(voucher, NULL, NULL, &persona_id);
2714 assert(kr == KERN_SUCCESS);
2715
2716 if (dest_ctx != persona_id) {
2717 if ((option & MACH_SEND_KERNEL) == 0) {
2718 mach_port_guard_exception(voucher_name, 0,
2719 (MPG_FLAGS_STRICT_REPLY_MISMATCHED_PERSONA | ((((uint64_t)persona_id << 32) & MPG_FLAGS_STRICT_REPLY_MASK) | dest_ctx)),
2720 kGUARD_EXC_STRICT_REPLY);
2721 }
2722 return MACH_SEND_INVALID_CONTEXT;
2723 }
2724
2725 return MACH_MSG_SUCCESS;
2726 }
2727
2728 /*
2729 * Routine: ipc_kmsg_copyin_header
2730 * Purpose:
2731 * "Copy-in" port rights in the header of a message.
2732 * Operates atomically; if it doesn't succeed the
2733 * message header and the space are left untouched.
2734 * If it does succeed the remote/local port fields
2735 * contain object pointers instead of port names,
2736 * and the bits field is updated. The destination port
2737 * will be a valid port pointer.
2738 *
2739 * Conditions:
2740 * Nothing locked.
2741 * Returns:
2742 * MACH_MSG_SUCCESS Successful copyin.
2743 * MACH_SEND_INVALID_HEADER
2744 * Illegal value in the message header bits.
2745 * MACH_SEND_INVALID_DEST The space is dead.
2746 * MACH_SEND_INVALID_DEST Can't copyin destination port.
2747 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2748 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
2749 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
2750 */
2751
2752 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)2753 ipc_kmsg_copyin_header(
2754 ipc_kmsg_t kmsg,
2755 ipc_space_t space,
2756 mach_msg_priority_t priority,
2757 mach_msg_option_t *optionp)
2758 {
2759 mach_msg_header_t *msg = kmsg->ikm_header;
2760 mach_msg_bits_t mbits = msg->msgh_bits & MACH_MSGH_BITS_USER;
2761 mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(msg->msgh_remote_port);
2762 mach_port_name_t reply_name = CAST_MACH_PORT_TO_NAME(msg->msgh_local_port);
2763 mach_port_name_t voucher_name = MACH_PORT_NULL;
2764 kern_return_t kr;
2765
2766 mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
2767 mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
2768 mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
2769 ipc_object_t dest_port = IO_NULL;
2770 ipc_object_t reply_port = IO_NULL;
2771 ipc_port_t dest_soright = IP_NULL;
2772 ipc_port_t dport = IP_NULL;
2773 ipc_port_t reply_soright = IP_NULL;
2774 ipc_port_t voucher_soright = IP_NULL;
2775 ipc_port_t release_port = IP_NULL;
2776 ipc_port_t voucher_port = IP_NULL;
2777 ipc_port_t voucher_release_port = IP_NULL;
2778 ipc_entry_t dest_entry = IE_NULL;
2779 ipc_entry_t reply_entry = IE_NULL;
2780 ipc_entry_t voucher_entry = IE_NULL;
2781
2782 int assertcnt = 0;
2783 #if IMPORTANCE_INHERITANCE
2784 boolean_t needboost = FALSE;
2785 #endif /* IMPORTANCE_INHERITANCE */
2786
2787 if ((mbits != msg->msgh_bits) ||
2788 (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type)) ||
2789 ((reply_type == 0) ?
2790 (reply_name != MACH_PORT_NULL) :
2791 !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type))) {
2792 return MACH_SEND_INVALID_HEADER;
2793 }
2794
2795 if (!MACH_PORT_VALID(dest_name)) {
2796 return MACH_SEND_INVALID_DEST;
2797 }
2798
2799 is_write_lock(space);
2800 if (!is_active(space)) {
2801 is_write_unlock(space);
2802 return MACH_SEND_INVALID_DEST;
2803 }
2804 /* space locked and active */
2805
2806 /*
2807 * If there is a voucher specified, make sure the disposition is
2808 * valid and the entry actually refers to a voucher port. Don't
2809 * actually copy in until we validate destination and reply.
2810 */
2811 if (voucher_type != MACH_MSGH_BITS_ZERO) {
2812 voucher_name = msg->msgh_voucher_port;
2813
2814 if (voucher_name == MACH_PORT_DEAD ||
2815 (voucher_type != MACH_MSG_TYPE_MOVE_SEND &&
2816 voucher_type != MACH_MSG_TYPE_COPY_SEND)) {
2817 is_write_unlock(space);
2818 if ((*optionp & MACH_SEND_KERNEL) == 0) {
2819 mach_port_guard_exception(voucher_name, 0, 0, kGUARD_EXC_SEND_INVALID_VOUCHER);
2820 }
2821 return MACH_SEND_INVALID_VOUCHER;
2822 }
2823
2824 if (voucher_name != MACH_PORT_NULL) {
2825 voucher_entry = ipc_entry_lookup(space, voucher_name);
2826 if (voucher_entry == IE_NULL ||
2827 (voucher_entry->ie_bits & MACH_PORT_TYPE_SEND) == 0 ||
2828 io_kotype(voucher_entry->ie_object) != IKOT_VOUCHER) {
2829 is_write_unlock(space);
2830 if ((*optionp & MACH_SEND_KERNEL) == 0) {
2831 mach_port_guard_exception(voucher_name, 0, 0, kGUARD_EXC_SEND_INVALID_VOUCHER);
2832 }
2833 return MACH_SEND_INVALID_VOUCHER;
2834 }
2835 } else {
2836 voucher_type = MACH_MSG_TYPE_MOVE_SEND;
2837 }
2838 }
2839
2840 if (enforce_strict_reply && MACH_SEND_WITH_STRICT_REPLY(*optionp) &&
2841 (!MACH_PORT_VALID(reply_name) ||
2842 ((reply_type != MACH_MSG_TYPE_MAKE_SEND_ONCE) && (reply_type != MACH_MSG_TYPE_MOVE_SEND_ONCE))
2843 )) {
2844 /*
2845 * The caller cannot enforce a reply context with an invalid
2846 * reply port name, or a non-send_once reply disposition.
2847 */
2848 is_write_unlock(space);
2849 if ((*optionp & MACH_SEND_KERNEL) == 0) {
2850 mach_port_guard_exception(reply_name, 0,
2851 (MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_DISP | reply_type),
2852 kGUARD_EXC_STRICT_REPLY);
2853 }
2854 return MACH_SEND_INVALID_REPLY;
2855 }
2856
2857 /*
2858 * Handle combinations of validating destination and reply; along
2859 * with copying in destination, reply, and voucher in an atomic way.
2860 */
2861
2862 if (dest_name == voucher_name) {
2863 /*
2864 * If the destination name is the same as the voucher name,
2865 * the voucher_entry must already be known. Either that or
2866 * the destination name is MACH_PORT_NULL (i.e. invalid).
2867 */
2868 dest_entry = voucher_entry;
2869 if (dest_entry == IE_NULL) {
2870 goto invalid_dest;
2871 }
2872
2873 /*
2874 * Make sure a future copyin of the reply port will succeed.
2875 * Once we start copying in the dest/voucher pair, we can't
2876 * back out.
2877 */
2878 if (MACH_PORT_VALID(reply_name)) {
2879 assert(reply_type != 0); /* because reply_name not null */
2880
2881 /* It is just WRONG if dest, voucher, and reply are all the same. */
2882 if (voucher_name == reply_name) {
2883 goto invalid_reply;
2884 }
2885 reply_entry = ipc_entry_lookup(space, reply_name);
2886 if (reply_entry == IE_NULL) {
2887 goto invalid_reply;
2888 }
2889 assert(dest_entry != reply_entry); /* names are not equal */
2890 if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type)) {
2891 goto invalid_reply;
2892 }
2893 }
2894
2895 /*
2896 * Do the joint copyin of the dest disposition and
2897 * voucher disposition from the one entry/port. We
2898 * already validated that the voucher copyin would
2899 * succeed (above). So, any failure in combining
2900 * the copyins can be blamed on the destination.
2901 */
2902 kr = ipc_right_copyin_two(space, dest_name, dest_entry,
2903 dest_type, voucher_type, &dest_port, &dest_soright,
2904 &release_port);
2905 if (kr != KERN_SUCCESS) {
2906 assert(kr != KERN_INVALID_CAPABILITY);
2907 goto invalid_dest;
2908 }
2909 voucher_port = ip_object_to_port(dest_port);
2910
2911 /*
2912 * could not have been one of these dispositions,
2913 * validated the port was a true kernel voucher port above,
2914 * AND was successfully able to copyin both dest and voucher.
2915 */
2916 assert(dest_type != MACH_MSG_TYPE_MAKE_SEND);
2917 assert(dest_type != MACH_MSG_TYPE_MAKE_SEND_ONCE);
2918 assert(dest_type != MACH_MSG_TYPE_MOVE_SEND_ONCE);
2919
2920 /*
2921 * Perform the delayed reply right copyin (guaranteed success).
2922 */
2923 if (reply_entry != IE_NULL) {
2924 kr = ipc_right_copyin(space, reply_name, reply_entry,
2925 reply_type, IPC_OBJECT_COPYIN_FLAGS_DEADOK,
2926 &reply_port, &reply_soright,
2927 &release_port, &assertcnt, 0, NULL);
2928 assert(assertcnt == 0);
2929 assert(kr == KERN_SUCCESS);
2930 }
2931 } else {
2932 if (dest_name == reply_name) {
2933 /*
2934 * Destination and reply ports are the same!
2935 * This is very similar to the case where the
2936 * destination and voucher ports were the same
2937 * (except the reply port disposition is not
2938 * previously validated).
2939 */
2940 dest_entry = ipc_entry_lookup(space, dest_name);
2941 if (dest_entry == IE_NULL) {
2942 goto invalid_dest;
2943 }
2944
2945 reply_entry = dest_entry;
2946 assert(reply_type != 0); /* because name not null */
2947
2948 /*
2949 * Pre-validate that the reply right can be copied in by itself.
2950 * Fail if reply port is marked as immovable send.
2951 */
2952 if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type)) {
2953 goto invalid_reply;
2954 }
2955
2956 /*
2957 * Do the joint copyin of the dest disposition and
2958 * reply disposition from the one entry/port.
2959 */
2960 kr = ipc_right_copyin_two(space, dest_name, dest_entry,
2961 dest_type, reply_type, &dest_port, &dest_soright,
2962 &release_port);
2963 if (kr == KERN_INVALID_CAPABILITY) {
2964 goto invalid_reply;
2965 } else if (kr != KERN_SUCCESS) {
2966 goto invalid_dest;
2967 }
2968 reply_port = dest_port;
2969 } else {
2970 /*
2971 * Handle destination and reply independently, as
2972 * they are independent entries (even if the entries
2973 * refer to the same port).
2974 *
2975 * This can be the tough case to make atomic.
2976 *
2977 * The difficult problem is serializing with port death.
2978 * The bad case is when dest_port dies after its copyin,
2979 * reply_port dies before its copyin, and dest_port dies before
2980 * reply_port. Then the copyins operated as if dest_port was
2981 * alive and reply_port was dead, which shouldn't have happened
2982 * because they died in the other order.
2983 *
2984 * Note that it is easy for a user task to tell if
2985 * a copyin happened before or after a port died.
2986 * If a port dies before copyin, a dead-name notification
2987 * is generated and the dead name's urefs are incremented,
2988 * and if the copyin happens first, a port-deleted
2989 * notification is generated.
2990 *
2991 * Even so, avoiding that potentially detectable race is too
2992 * expensive - and no known code cares about it. So, we just
2993 * do the expedient thing and copy them in one after the other.
2994 */
2995
2996 dest_entry = ipc_entry_lookup(space, dest_name);
2997 if (dest_entry == IE_NULL) {
2998 goto invalid_dest;
2999 }
3000 assert(dest_entry != voucher_entry);
3001
3002 /*
3003 * Make sure reply port entry is valid before dest copyin.
3004 */
3005 if (MACH_PORT_VALID(reply_name)) {
3006 if (reply_name == voucher_name) {
3007 goto invalid_reply;
3008 }
3009 reply_entry = ipc_entry_lookup(space, reply_name);
3010 if (reply_entry == IE_NULL) {
3011 goto invalid_reply;
3012 }
3013 assert(dest_entry != reply_entry); /* names are not equal */
3014 assert(reply_type != 0); /* because reply_name not null */
3015
3016 if (!ipc_right_copyin_check_reply(space, reply_name, reply_entry, reply_type)) {
3017 goto invalid_reply;
3018 }
3019 }
3020
3021 /*
3022 * copyin the destination.
3023 */
3024 kr = ipc_right_copyin(space, dest_name, dest_entry,
3025 dest_type, (IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND |
3026 IPC_OBJECT_COPYIN_FLAGS_ALLOW_DEAD_SEND_ONCE),
3027 &dest_port, &dest_soright,
3028 &release_port, &assertcnt, 0, NULL);
3029 assert(assertcnt == 0);
3030 if (kr != KERN_SUCCESS) {
3031 goto invalid_dest;
3032 }
3033 assert(IO_VALID(dest_port));
3034 assert(!IP_VALID(release_port));
3035
3036 /*
3037 * Copyin the pre-validated reply right.
3038 * It's OK if the reply right has gone dead in the meantime.
3039 */
3040 if (MACH_PORT_VALID(reply_name)) {
3041 kr = ipc_right_copyin(space, reply_name, reply_entry,
3042 reply_type, IPC_OBJECT_COPYIN_FLAGS_DEADOK,
3043 &reply_port, &reply_soright,
3044 &release_port, &assertcnt, 0, NULL);
3045 assert(assertcnt == 0);
3046 assert(kr == KERN_SUCCESS);
3047 } else {
3048 /* convert invalid name to equivalent ipc_object type */
3049 reply_port = ip_to_object(CAST_MACH_NAME_TO_PORT(reply_name));
3050 }
3051 }
3052
3053 /*
3054 * Finally can copyin the voucher right now that dest and reply
3055 * are fully copied in (guaranteed success).
3056 */
3057 if (IE_NULL != voucher_entry) {
3058 kr = ipc_right_copyin(space, voucher_name, voucher_entry,
3059 voucher_type, IPC_OBJECT_COPYIN_FLAGS_NONE,
3060 (ipc_object_t *)&voucher_port,
3061 &voucher_soright,
3062 &voucher_release_port,
3063 &assertcnt, 0, NULL);
3064 assert(assertcnt == 0);
3065 assert(KERN_SUCCESS == kr);
3066 assert(IP_VALID(voucher_port));
3067 require_ip_active(voucher_port);
3068 }
3069 }
3070
3071 dest_type = ipc_object_copyin_type(dest_type);
3072 reply_type = ipc_object_copyin_type(reply_type);
3073
3074 dport = ip_object_to_port(dest_port);
3075 /*
3076 * If the dest port died, or is a kobject AND its receive right belongs to kernel,
3077 * allow copyin of immovable send rights in the message body (port descriptor) to
3078 * succeed since those send rights are simply "moved" or "copied" into kernel.
3079 *
3080 * See: ipc_object_copyin().
3081 */
3082
3083 ip_mq_lock(dport);
3084
3085 if (!ip_active(dport) || (ip_is_kobject(dport) &&
3086 ip_in_space(dport, ipc_space_kernel))) {
3087 assert(ip_kotype(dport) != IKOT_HOST_NOTIFY && ip_kotype(dport) != IKOT_TIMER);
3088 kmsg->ikm_flags |= IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND;
3089 }
3090
3091 /*
3092 * JMM - Without rdar://problem/6275821, this is the last place we can
3093 * re-arm the send-possible notifications. It may trigger unexpectedly
3094 * early (send may NOT have failed), but better than missing. We assure
3095 * we won't miss by forcing MACH_SEND_ALWAYS if we got past arming.
3096 */
3097 if (((*optionp & MACH_SEND_NOTIFY) != 0) &&
3098 dest_type != MACH_MSG_TYPE_PORT_SEND_ONCE &&
3099 dest_entry != IE_NULL && dest_entry->ie_request != IE_REQ_NONE) {
3100 /* dport still locked from above */
3101 if (ip_active(dport) && !ip_in_space(dport, ipc_space_kernel)) {
3102 /* dport could be in-transit, or in an ipc space */
3103 if (ip_full(dport)) {
3104 #if IMPORTANCE_INHERITANCE
3105 needboost = ipc_port_request_sparm(dport, dest_name,
3106 dest_entry->ie_request,
3107 *optionp,
3108 priority);
3109 if (needboost == FALSE) {
3110 ip_mq_unlock(dport);
3111 }
3112 #else
3113 ipc_port_request_sparm(dport, dest_name,
3114 dest_entry->ie_request,
3115 *optionp,
3116 priority);
3117 ip_mq_unlock(dport);
3118 #endif /* IMPORTANCE_INHERITANCE */
3119 } else {
3120 *optionp |= MACH_SEND_ALWAYS;
3121 ip_mq_unlock(dport);
3122 }
3123 } else {
3124 ip_mq_unlock(dport);
3125 }
3126 } else {
3127 ip_mq_unlock(dport);
3128 }
3129 /* dport is unlocked, unless needboost == TRUE */
3130
3131 is_write_unlock(space);
3132
3133 #if IMPORTANCE_INHERITANCE
3134 /*
3135 * If our request is the first boosting send-possible
3136 * notification this cycle, push the boost down the
3137 * destination port.
3138 */
3139 if (needboost == TRUE) {
3140 /* dport still locked from above */
3141 if (ipc_port_importance_delta(dport, IPID_OPTION_SENDPOSSIBLE, 1) == FALSE) {
3142 ip_mq_unlock(dport);
3143 }
3144 }
3145 #endif /* IMPORTANCE_INHERITANCE */
3146
3147 /* dport is unlocked */
3148
3149 if (dest_soright != IP_NULL) {
3150 ipc_notify_port_deleted(dest_soright, dest_name);
3151 }
3152 if (reply_soright != IP_NULL) {
3153 ipc_notify_port_deleted(reply_soright, reply_name);
3154 }
3155 if (voucher_soright != IP_NULL) {
3156 ipc_notify_port_deleted(voucher_soright, voucher_name);
3157 }
3158
3159 /*
3160 * No room to store voucher port in in-kernel msg header,
3161 * so we store it back in the kmsg itself. Store original voucher
3162 * type there as well, but set the bits to the post-copyin type.
3163 */
3164 if (IP_VALID(voucher_port)) {
3165 ipc_kmsg_set_voucher_port(kmsg, voucher_port, voucher_type);
3166 voucher_type = MACH_MSG_TYPE_MOVE_SEND;
3167 }
3168
3169 msg->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, voucher_type, mbits);
3170 msg->msgh_remote_port = ip_object_to_port(dest_port);
3171 msg->msgh_local_port = ip_object_to_port(reply_port);
3172
3173 /*
3174 * capture the qos value(s) for the kmsg qos,
3175 * and apply any override before we enqueue the kmsg.
3176 */
3177 ipc_kmsg_set_qos(kmsg, *optionp, priority);
3178
3179 if (release_port != IP_NULL) {
3180 ip_release(release_port);
3181 }
3182
3183 if (voucher_release_port != IP_NULL) {
3184 ip_release(voucher_release_port);
3185 }
3186
3187 if (enforce_strict_reply && MACH_SEND_WITH_STRICT_REPLY(*optionp) && IP_VALID(msg->msgh_local_port)) {
3188 /*
3189 * We've already validated that the reply disposition is a
3190 * [make/move] send-once. Ideally, we should enforce that the
3191 * reply port is also not dead, but XPC asynchronous
3192 * cancellation can make the reply port dead before we
3193 * actually make it to the mach_msg send.
3194 *
3195 * Here, we ensure that if we have a non-dead reply port, then
3196 * the reply port's receive right should not be in-transit,
3197 * and should live in the caller's IPC space.
3198 */
3199 ipc_port_t rport = msg->msgh_local_port;
3200 ip_mq_lock(rport);
3201 kr = ipc_kmsg_validate_reply_port_locked(rport, *optionp);
3202 ip_mq_unlock(rport);
3203 if (kr != KERN_SUCCESS) {
3204 /*
3205 * no descriptors have been copied in yet, but the
3206 * full header has been copied in: clean it up
3207 */
3208 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
3209 if ((*optionp & MACH_SEND_KERNEL) == 0) {
3210 mach_port_guard_exception(reply_name, 0,
3211 (MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_PORT | kr),
3212 kGUARD_EXC_STRICT_REPLY);
3213 }
3214 return MACH_SEND_INVALID_REPLY;
3215 }
3216 }
3217
3218 return MACH_MSG_SUCCESS;
3219
3220 invalid_reply:
3221 is_write_unlock(space);
3222
3223 if (release_port != IP_NULL) {
3224 ip_release(release_port);
3225 }
3226
3227 assert(voucher_port == IP_NULL);
3228 assert(voucher_soright == IP_NULL);
3229
3230 if ((*optionp & MACH_SEND_KERNEL) == 0) {
3231 mach_port_guard_exception(reply_name, 0, 0, kGUARD_EXC_SEND_INVALID_REPLY);
3232 }
3233 return MACH_SEND_INVALID_REPLY;
3234
3235 invalid_dest:
3236 is_write_unlock(space);
3237
3238 if (release_port != IP_NULL) {
3239 ip_release(release_port);
3240 }
3241
3242 if (reply_soright != IP_NULL) {
3243 ipc_notify_port_deleted(reply_soright, reply_name);
3244 }
3245
3246 assert(voucher_port == IP_NULL);
3247 assert(voucher_soright == IP_NULL);
3248
3249 return MACH_SEND_INVALID_DEST;
3250 }
3251
3252 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)3253 ipc_kmsg_copyin_port_descriptor(
3254 mach_msg_port_descriptor_t *dsc,
3255 mach_msg_user_port_descriptor_t *user_dsc_in,
3256 ipc_space_t space,
3257 ipc_object_t dest,
3258 ipc_kmsg_t kmsg,
3259 mach_msg_option_t *optionp,
3260 mach_msg_return_t *mr)
3261 {
3262 mach_msg_user_port_descriptor_t user_dsc = *user_dsc_in;
3263 mach_msg_type_name_t user_disp;
3264 mach_msg_type_name_t result_disp;
3265 mach_port_name_t name;
3266 ipc_object_t object;
3267
3268 user_disp = user_dsc.disposition;
3269 result_disp = ipc_object_copyin_type(user_disp);
3270
3271 name = (mach_port_name_t)user_dsc.name;
3272 if (MACH_PORT_VALID(name)) {
3273 kern_return_t kr = ipc_object_copyin(space, name, user_disp, &object, 0, NULL, kmsg->ikm_flags);
3274 if (kr != KERN_SUCCESS) {
3275 if (((*optionp & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
3276 mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
3277 }
3278 *mr = MACH_SEND_INVALID_RIGHT;
3279 return NULL;
3280 }
3281
3282 if ((result_disp == MACH_MSG_TYPE_PORT_RECEIVE) &&
3283 ipc_port_check_circularity(ip_object_to_port(object),
3284 ip_object_to_port(dest))) {
3285 kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
3286 }
3287 dsc->name = ip_object_to_port(object);
3288 } else {
3289 dsc->name = CAST_MACH_NAME_TO_PORT(name);
3290 }
3291 dsc->disposition = result_disp;
3292 dsc->type = MACH_MSG_PORT_DESCRIPTOR;
3293
3294 dsc->pad_end = 0; // debug, unnecessary
3295
3296 return (mach_msg_descriptor_t *)(user_dsc_in + 1);
3297 }
3298
3299 static mach_msg_descriptor_t *
ipc_kmsg_copyin_ool_descriptor(mach_msg_ool_descriptor_t * dsc,mach_msg_descriptor_t * user_dsc,int is_64bit,mach_vm_address_t * paddr,vm_map_copy_t * copy,vm_size_t * space_needed,vm_map_t map,__unused mach_msg_option_t * optionp,mach_msg_return_t * mr)3300 ipc_kmsg_copyin_ool_descriptor(
3301 mach_msg_ool_descriptor_t *dsc,
3302 mach_msg_descriptor_t *user_dsc,
3303 int is_64bit,
3304 mach_vm_address_t *paddr,
3305 vm_map_copy_t *copy,
3306 vm_size_t *space_needed,
3307 vm_map_t map,
3308 __unused mach_msg_option_t *optionp,
3309 mach_msg_return_t *mr)
3310 {
3311 vm_size_t length;
3312 boolean_t dealloc;
3313 mach_msg_copy_options_t copy_options;
3314 mach_vm_offset_t addr;
3315 mach_msg_descriptor_type_t dsc_type;
3316
3317 if (is_64bit) {
3318 mach_msg_ool_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3319
3320 addr = (mach_vm_offset_t) user_ool_dsc->address;
3321 length = user_ool_dsc->size;
3322 dealloc = user_ool_dsc->deallocate;
3323 copy_options = user_ool_dsc->copy;
3324 dsc_type = user_ool_dsc->type;
3325
3326 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3327 } else {
3328 mach_msg_ool_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3329
3330 addr = CAST_USER_ADDR_T(user_ool_dsc->address);
3331 dealloc = user_ool_dsc->deallocate;
3332 copy_options = user_ool_dsc->copy;
3333 dsc_type = user_ool_dsc->type;
3334 length = user_ool_dsc->size;
3335
3336 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3337 }
3338
3339 dsc->size = (mach_msg_size_t)length;
3340 dsc->deallocate = dealloc;
3341 dsc->copy = copy_options;
3342 dsc->type = dsc_type;
3343
3344 if (length == 0) {
3345 dsc->address = NULL;
3346 } else if ((length >= MSG_OOL_SIZE_SMALL) &&
3347 (copy_options == MACH_MSG_PHYSICAL_COPY) && !dealloc) {
3348 /*
3349 * If the request is a physical copy and the source
3350 * is not being deallocated, then allocate space
3351 * in the kernel's pageable ipc copy map and copy
3352 * the data in. The semantics guarantee that the
3353 * data will have been physically copied before
3354 * the send operation terminates. Thus if the data
3355 * is not being deallocated, we must be prepared
3356 * to page if the region is sufficiently large.
3357 */
3358 if (copyin(addr, (char *)*paddr, length)) {
3359 *mr = MACH_SEND_INVALID_MEMORY;
3360 return NULL;
3361 }
3362
3363 /*
3364 * The kernel ipc copy map is marked no_zero_fill.
3365 * If the transfer is not a page multiple, we need
3366 * to zero fill the balance.
3367 */
3368 if (!page_aligned(length)) {
3369 (void) memset((void *) (*paddr + length), 0,
3370 round_page(length) - length);
3371 }
3372 if (vm_map_copyin(ipc_kernel_copy_map, (vm_map_address_t)*paddr,
3373 (vm_map_size_t)length, TRUE, copy) != KERN_SUCCESS) {
3374 *mr = MACH_MSG_VM_KERNEL;
3375 return NULL;
3376 }
3377 dsc->address = (void *)*copy;
3378 *paddr += round_page(length);
3379 *space_needed -= round_page(length);
3380 } else {
3381 /*
3382 * Make a vm_map_copy_t of the of the data. If the
3383 * data is small, this will do an optimized physical
3384 * copy. Otherwise, it will do a virtual copy.
3385 *
3386 * NOTE: A virtual copy is OK if the original is being
3387 * deallocted, even if a physical copy was requested.
3388 */
3389 kern_return_t kr = vm_map_copyin(map, addr,
3390 (vm_map_size_t)length, dealloc, copy);
3391 if (kr != KERN_SUCCESS) {
3392 *mr = (kr == KERN_RESOURCE_SHORTAGE) ?
3393 MACH_MSG_VM_KERNEL :
3394 MACH_SEND_INVALID_MEMORY;
3395 return NULL;
3396 }
3397 dsc->address = (void *)*copy;
3398 }
3399
3400 return user_dsc;
3401 }
3402
3403 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)3404 ipc_kmsg_copyin_ool_ports_descriptor(
3405 mach_msg_ool_ports_descriptor_t *dsc,
3406 mach_msg_descriptor_t *user_dsc,
3407 int is_64bit,
3408 vm_map_t map,
3409 ipc_space_t space,
3410 ipc_object_t dest,
3411 ipc_kmsg_t kmsg,
3412 mach_msg_option_t *optionp,
3413 mach_msg_return_t *mr)
3414 {
3415 void *data;
3416 ipc_object_t *objects;
3417 unsigned int i;
3418 mach_vm_offset_t addr;
3419 mach_msg_type_name_t user_disp;
3420 mach_msg_type_name_t result_disp;
3421 mach_msg_type_number_t count;
3422 mach_msg_copy_options_t copy_option;
3423 boolean_t deallocate;
3424 mach_msg_descriptor_type_t type;
3425 vm_size_t ports_length, names_length;
3426
3427 if (is_64bit) {
3428 mach_msg_ool_ports_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3429
3430 addr = (mach_vm_offset_t)user_ool_dsc->address;
3431 count = user_ool_dsc->count;
3432 deallocate = user_ool_dsc->deallocate;
3433 copy_option = user_ool_dsc->copy;
3434 user_disp = user_ool_dsc->disposition;
3435 type = user_ool_dsc->type;
3436
3437 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3438 } else {
3439 mach_msg_ool_ports_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
3440
3441 addr = CAST_USER_ADDR_T(user_ool_dsc->address);
3442 count = user_ool_dsc->count;
3443 deallocate = user_ool_dsc->deallocate;
3444 copy_option = user_ool_dsc->copy;
3445 user_disp = user_ool_dsc->disposition;
3446 type = user_ool_dsc->type;
3447
3448 user_dsc = (typeof(user_dsc))(user_ool_dsc + 1);
3449 }
3450
3451 dsc->deallocate = deallocate;
3452 dsc->copy = copy_option;
3453 dsc->type = type;
3454 dsc->count = count;
3455 dsc->address = NULL; /* for now */
3456
3457 result_disp = ipc_object_copyin_type(user_disp);
3458 dsc->disposition = result_disp;
3459
3460 /* We always do a 'physical copy', but you have to specify something valid */
3461 if (copy_option != MACH_MSG_PHYSICAL_COPY &&
3462 copy_option != MACH_MSG_VIRTUAL_COPY) {
3463 *mr = MACH_SEND_INVALID_TYPE;
3464 return NULL;
3465 }
3466
3467 /* calculate length of data in bytes, rounding up */
3468
3469 if (os_mul_overflow(count, sizeof(mach_port_t), &ports_length)) {
3470 *mr = MACH_SEND_TOO_LARGE;
3471 return NULL;
3472 }
3473
3474 if (os_mul_overflow(count, sizeof(mach_port_name_t), &names_length)) {
3475 *mr = MACH_SEND_TOO_LARGE;
3476 return NULL;
3477 }
3478
3479 if (ports_length == 0) {
3480 return user_dsc;
3481 }
3482
3483 data = kalloc_type(mach_port_t, count, Z_WAITOK);
3484
3485 if (data == NULL) {
3486 *mr = MACH_SEND_NO_BUFFER;
3487 return NULL;
3488 }
3489
3490 #ifdef __LP64__
3491 mach_port_name_t *names = &((mach_port_name_t *)data)[count];
3492 #else
3493 mach_port_name_t *names = ((mach_port_name_t *)data);
3494 #endif
3495
3496 if (copyinmap(map, addr, names, names_length) != KERN_SUCCESS) {
3497 kfree_type(mach_port_t, count, data);
3498 *mr = MACH_SEND_INVALID_MEMORY;
3499 return NULL;
3500 }
3501
3502 if (deallocate) {
3503 (void) mach_vm_deallocate(map, addr, (mach_vm_size_t)names_length);
3504 }
3505
3506 objects = (ipc_object_t *) data;
3507 dsc->address = data;
3508
3509 for (i = 0; i < count; i++) {
3510 mach_port_name_t name = names[i];
3511 ipc_object_t object;
3512
3513 if (!MACH_PORT_VALID(name)) {
3514 objects[i] = ip_to_object(CAST_MACH_NAME_TO_PORT(name));
3515 continue;
3516 }
3517
3518 kern_return_t kr = ipc_object_copyin(space, name, user_disp, &object, 0, NULL, kmsg->ikm_flags);
3519
3520 if (kr != KERN_SUCCESS) {
3521 unsigned int j;
3522
3523 for (j = 0; j < i; j++) {
3524 object = objects[j];
3525 if (IPC_OBJECT_VALID(object)) {
3526 ipc_object_destroy(object, result_disp);
3527 }
3528 }
3529 kfree_type(mach_port_t, count, data);
3530 dsc->address = NULL;
3531 if (((*optionp & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
3532 mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
3533 }
3534 *mr = MACH_SEND_INVALID_RIGHT;
3535 return NULL;
3536 }
3537
3538 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
3539 ipc_port_check_circularity(ip_object_to_port(object),
3540 ip_object_to_port(dest))) {
3541 kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
3542 }
3543
3544 objects[i] = object;
3545 }
3546
3547 return user_dsc;
3548 }
3549
3550 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)3551 ipc_kmsg_copyin_guarded_port_descriptor(
3552 mach_msg_guarded_port_descriptor_t *dsc,
3553 mach_msg_descriptor_t *user_addr,
3554 int is_64bit,
3555 ipc_space_t space,
3556 ipc_object_t dest,
3557 ipc_kmsg_t kmsg,
3558 mach_msg_option_t *optionp,
3559 mach_msg_return_t *mr)
3560 {
3561 mach_msg_descriptor_t *user_dsc;
3562 mach_msg_type_name_t disp;
3563 mach_msg_type_name_t result_disp;
3564 mach_port_name_t name;
3565 mach_msg_guard_flags_t guard_flags;
3566 ipc_object_t object;
3567 mach_port_context_t context;
3568
3569 if (!is_64bit) {
3570 mach_msg_guarded_port_descriptor32_t *user_gp_dsc = (typeof(user_gp_dsc))user_addr;
3571 name = user_gp_dsc->name;
3572 guard_flags = user_gp_dsc->flags;
3573 disp = user_gp_dsc->disposition;
3574 context = user_gp_dsc->context;
3575 user_dsc = (mach_msg_descriptor_t *)(user_gp_dsc + 1);
3576 } else {
3577 mach_msg_guarded_port_descriptor64_t *user_gp_dsc = (typeof(user_gp_dsc))user_addr;
3578 name = user_gp_dsc->name;
3579 guard_flags = user_gp_dsc->flags;
3580 disp = user_gp_dsc->disposition;
3581 context = user_gp_dsc->context;
3582 user_dsc = (mach_msg_descriptor_t *)(user_gp_dsc + 1);
3583 }
3584
3585 guard_flags &= MACH_MSG_GUARD_FLAGS_MASK;
3586 result_disp = ipc_object_copyin_type(disp);
3587
3588 if (MACH_PORT_VALID(name)) {
3589 kern_return_t kr = ipc_object_copyin(space, name, disp, &object, context, &guard_flags, kmsg->ikm_flags);
3590 if (kr != KERN_SUCCESS) {
3591 if (((*optionp & MACH_SEND_KERNEL) == 0) && (kr == KERN_INVALID_RIGHT)) {
3592 mach_port_guard_exception(name, 0, 0, kGUARD_EXC_SEND_INVALID_RIGHT);
3593 }
3594 *mr = MACH_SEND_INVALID_RIGHT;
3595 return NULL;
3596 }
3597
3598 if ((result_disp == MACH_MSG_TYPE_PORT_RECEIVE) &&
3599 ipc_port_check_circularity(ip_object_to_port(object),
3600 ip_object_to_port(dest))) {
3601 kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
3602 }
3603 dsc->name = ip_object_to_port(object);
3604 } else {
3605 dsc->name = CAST_MACH_NAME_TO_PORT(name);
3606 }
3607 dsc->flags = guard_flags;
3608 dsc->disposition = result_disp;
3609 dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
3610
3611 #if __LP64__
3612 dsc->pad_end = 0; // debug, unnecessary
3613 #endif
3614
3615 return user_dsc;
3616 }
3617
3618
3619 /*
3620 * Routine: ipc_kmsg_copyin_body
3621 * Purpose:
3622 * "Copy-in" port rights and out-of-line memory
3623 * in the message body.
3624 *
3625 * In all failure cases, the message is left holding
3626 * no rights or memory. However, the message buffer
3627 * is not deallocated. If successful, the message
3628 * contains a valid destination port.
3629 * Conditions:
3630 * Nothing locked.
3631 * Returns:
3632 * MACH_MSG_SUCCESS Successful copyin.
3633 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
3634 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
3635 * MACH_SEND_INVALID_TYPE Bad type specification.
3636 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
3637 * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT
3638 * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible
3639 * MACH_SEND_NO_GRANT_DEST Dest port doesn't accept ports in body
3640 */
3641
3642 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)3643 ipc_kmsg_copyin_body(
3644 ipc_kmsg_t kmsg,
3645 ipc_space_t space,
3646 vm_map_t map,
3647 mach_msg_option_t *optionp)
3648 {
3649 ipc_object_t dest;
3650 mach_msg_body_t *body;
3651 mach_msg_descriptor_t *daddr;
3652 mach_msg_descriptor_t *user_addr, *kern_addr;
3653 mach_msg_type_number_t dsc_count;
3654 boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
3655 boolean_t complex = FALSE;
3656 boolean_t contains_port_desc = FALSE;
3657 vm_size_t space_needed = 0;
3658 mach_vm_address_t paddr = 0;
3659 vm_offset_t end;
3660 vm_map_copy_t copy = VM_MAP_COPY_NULL;
3661 mach_msg_return_t mr = MACH_MSG_SUCCESS;
3662 ipc_port_t remote_port = kmsg->ikm_header->msgh_remote_port;
3663
3664 vm_size_t descriptor_size = 0;
3665
3666 mach_msg_type_number_t total_ool_port_count = 0;
3667 mach_msg_guard_flags_t guard_flags = 0;
3668 mach_port_context_t context;
3669 mach_msg_type_name_t disp;
3670
3671 /*
3672 * Determine if the target is a kernel port.
3673 */
3674 dest = ip_to_object(remote_port);
3675 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
3676 daddr = (mach_msg_descriptor_t *) (body + 1);
3677 end = ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
3678
3679 dsc_count = body->msgh_descriptor_count;
3680 if (dsc_count == 0) {
3681 return MACH_MSG_SUCCESS;
3682 }
3683
3684 /*
3685 * Make an initial pass to determine kernal VM space requirements for
3686 * physical copies and possible contraction of the descriptors from
3687 * processes with pointers larger than the kernel's.
3688 */
3689 for (mach_msg_type_number_t i = 0; i < dsc_count; i++) {
3690 mach_msg_size_t dsize;
3691 mach_msg_size_t size;
3692 mach_msg_type_number_t ool_port_count = 0;
3693
3694 if ((vm_offset_t)daddr + MACH_MSG_DESC_MIN_SIZE > end) {
3695 mr = MACH_SEND_MSG_TOO_SMALL;
3696 goto clean_message;
3697 }
3698
3699 dsize = ipc_kmsg_user_desc_size(daddr->type.type, is_task_64bit);
3700 if ((vm_offset_t)daddr + dsize > end) {
3701 mr = MACH_SEND_MSG_TOO_SMALL;
3702 goto clean_message;
3703 }
3704
3705 switch (daddr->type.type) {
3706 case MACH_MSG_OOL_DESCRIPTOR:
3707 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
3708 size = (is_task_64bit) ?
3709 ((mach_msg_ool_descriptor64_t *)daddr)->size :
3710 daddr->out_of_line.size;
3711
3712 if (daddr->out_of_line.copy != MACH_MSG_PHYSICAL_COPY &&
3713 daddr->out_of_line.copy != MACH_MSG_VIRTUAL_COPY) {
3714 /*
3715 * Invalid copy option
3716 */
3717 mr = MACH_SEND_INVALID_TYPE;
3718 goto clean_message;
3719 }
3720
3721 if ((size >= MSG_OOL_SIZE_SMALL) &&
3722 (daddr->out_of_line.copy == MACH_MSG_PHYSICAL_COPY) &&
3723 !(daddr->out_of_line.deallocate)) {
3724 /*
3725 * Out-of-line memory descriptor, accumulate kernel
3726 * memory requirements
3727 */
3728 if (space_needed + round_page(size) <= space_needed) {
3729 /* Overflow dectected */
3730 mr = MACH_MSG_VM_KERNEL;
3731 goto clean_message;
3732 }
3733
3734 space_needed += round_page(size);
3735 if (space_needed > ipc_kmsg_max_vm_space) {
3736 /* Per message kernel memory limit exceeded */
3737 mr = MACH_MSG_VM_KERNEL;
3738 goto clean_message;
3739 }
3740 }
3741 break;
3742 case MACH_MSG_PORT_DESCRIPTOR:
3743 if (os_add_overflow(total_ool_port_count, 1, &total_ool_port_count)) {
3744 /* Overflow detected */
3745 mr = MACH_SEND_TOO_LARGE;
3746 goto clean_message;
3747 }
3748 contains_port_desc = TRUE;
3749 break;
3750 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
3751 ool_port_count = (is_task_64bit) ?
3752 ((mach_msg_ool_ports_descriptor64_t *)daddr)->count :
3753 daddr->ool_ports.count;
3754
3755 if (os_add_overflow(total_ool_port_count, ool_port_count, &total_ool_port_count)) {
3756 /* Overflow detected */
3757 mr = MACH_SEND_TOO_LARGE;
3758 goto clean_message;
3759 }
3760
3761 if (ool_port_count > (ipc_kmsg_max_vm_space / sizeof(mach_port_t))) {
3762 /* Per message kernel memory limit exceeded */
3763 mr = MACH_SEND_TOO_LARGE;
3764 goto clean_message;
3765 }
3766 contains_port_desc = TRUE;
3767 break;
3768 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
3769 guard_flags = (is_task_64bit) ?
3770 ((mach_msg_guarded_port_descriptor64_t *)daddr)->flags :
3771 ((mach_msg_guarded_port_descriptor32_t *)daddr)->flags;
3772 context = (is_task_64bit) ?
3773 ((mach_msg_guarded_port_descriptor64_t *)daddr)->context :
3774 ((mach_msg_guarded_port_descriptor32_t *)daddr)->context;
3775 disp = (is_task_64bit) ?
3776 ((mach_msg_guarded_port_descriptor64_t *)daddr)->disposition :
3777 ((mach_msg_guarded_port_descriptor32_t *)daddr)->disposition;
3778
3779 /* Only MACH_MSG_TYPE_MOVE_RECEIVE is supported for now */
3780 if (!guard_flags || ((guard_flags & ~MACH_MSG_GUARD_FLAGS_MASK) != 0) ||
3781 ((guard_flags & MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND) && (context != 0)) ||
3782 (disp != MACH_MSG_TYPE_MOVE_RECEIVE)) {
3783 /*
3784 * Invalid guard flags, context or disposition
3785 */
3786 mr = MACH_SEND_INVALID_TYPE;
3787 goto clean_message;
3788 }
3789 if (os_add_overflow(total_ool_port_count, 1, &total_ool_port_count)) {
3790 /* Overflow detected */
3791 mr = MACH_SEND_TOO_LARGE;
3792 goto clean_message;
3793 }
3794 contains_port_desc = TRUE;
3795 break;
3796 default:
3797 mr = MACH_SEND_INVALID_TYPE;
3798 goto clean_message;
3799 }
3800
3801 descriptor_size += dsize;
3802 daddr = (typeof(daddr))((vm_offset_t)daddr + dsize);
3803 }
3804
3805 /* Sending more than 16383 rights in one message seems crazy */
3806 if (total_ool_port_count >= (MACH_PORT_UREFS_MAX / 4)) {
3807 mr = MACH_SEND_TOO_LARGE;
3808 goto clean_message;
3809 }
3810
3811 /*
3812 * Check if dest is a no-grant port; Since this bit is set only on
3813 * port construction and cannot be unset later, we can peek at the
3814 * bit without paying the cost of locking the port.
3815 */
3816 if (contains_port_desc && remote_port->ip_no_grant) {
3817 mr = MACH_SEND_NO_GRANT_DEST;
3818 goto clean_message;
3819 }
3820
3821 /*
3822 * Allocate space in the pageable kernel ipc copy map for all the
3823 * ool data that is to be physically copied. Map is marked wait for
3824 * space.
3825 */
3826 if (space_needed) {
3827 if (mach_vm_allocate_kernel(ipc_kernel_copy_map, &paddr, space_needed,
3828 VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IPC) != KERN_SUCCESS) {
3829 mr = MACH_MSG_VM_KERNEL;
3830 goto clean_message;
3831 }
3832 }
3833
3834 /* user_addr = just after base as it was copied in */
3835 user_addr = (mach_msg_descriptor_t *)((vm_offset_t)kmsg->ikm_header + sizeof(mach_msg_base_t));
3836
3837 /* Shift the mach_msg_base_t down to make room for dsc_count*16bytes of descriptors on 64 bit kernels
3838 */
3839 if (descriptor_size != KERNEL_DESC_SIZE * dsc_count) {
3840 vm_offset_t dsc_adjust = KERNEL_DESC_SIZE * dsc_count - descriptor_size;
3841
3842 memmove((char *)(((vm_offset_t)kmsg->ikm_header) - dsc_adjust), kmsg->ikm_header, sizeof(mach_msg_base_t));
3843 kmsg->ikm_header = (mach_msg_header_t *)((vm_offset_t)kmsg->ikm_header - dsc_adjust);
3844
3845 /* Update the message size for the larger in-kernel representation */
3846 kmsg->ikm_header->msgh_size += (mach_msg_size_t)dsc_adjust;
3847 }
3848
3849
3850 /* kern_addr = just after base after it has been (conditionally) moved */
3851 kern_addr = (mach_msg_descriptor_t *)((vm_offset_t)kmsg->ikm_header + sizeof(mach_msg_base_t));
3852
3853 /* handle the OOL regions and port descriptors. */
3854 for (mach_msg_type_number_t copied_in_dscs = 0;
3855 copied_in_dscs < dsc_count; copied_in_dscs++) {
3856 switch (user_addr->type.type) {
3857 case MACH_MSG_PORT_DESCRIPTOR:
3858 user_addr = ipc_kmsg_copyin_port_descriptor((mach_msg_port_descriptor_t *)kern_addr,
3859 (mach_msg_user_port_descriptor_t *)user_addr, space, dest, kmsg, optionp, &mr);
3860 kern_addr++;
3861 complex = TRUE;
3862 break;
3863 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
3864 case MACH_MSG_OOL_DESCRIPTOR:
3865 user_addr = ipc_kmsg_copyin_ool_descriptor((mach_msg_ool_descriptor_t *)kern_addr,
3866 user_addr, is_task_64bit, &paddr, ©, &space_needed, map, optionp, &mr);
3867 kern_addr++;
3868 complex = TRUE;
3869 break;
3870 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
3871 user_addr = ipc_kmsg_copyin_ool_ports_descriptor((mach_msg_ool_ports_descriptor_t *)kern_addr,
3872 user_addr, is_task_64bit, map, space, dest, kmsg, optionp, &mr);
3873 kern_addr++;
3874 complex = TRUE;
3875 break;
3876 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
3877 user_addr = ipc_kmsg_copyin_guarded_port_descriptor((mach_msg_guarded_port_descriptor_t *)kern_addr,
3878 user_addr, is_task_64bit, space, dest, kmsg, optionp, &mr);
3879 kern_addr++;
3880 complex = TRUE;
3881 break;
3882 default:
3883 panic("invalid descriptor type %d", user_addr->type.type);
3884 }
3885
3886 if (MACH_MSG_SUCCESS != mr) {
3887 /* clean from start of message descriptors to copied_in_dscs */
3888 ipc_kmsg_clean_partial(kmsg, copied_in_dscs,
3889 (mach_msg_descriptor_t *)((mach_msg_base_t *)kmsg->ikm_header + 1),
3890 paddr, space_needed);
3891 goto out;
3892 }
3893 } /* End of loop */
3894
3895 if (!complex) {
3896 kmsg->ikm_header->msgh_bits &= ~MACH_MSGH_BITS_COMPLEX;
3897 }
3898 out:
3899 return mr;
3900
3901 clean_message:
3902 /* no descriptors have been copied in yet */
3903 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
3904 return mr;
3905 }
3906
3907 #define MACH_BOOTSTRAP_PORT_MSG_ID_MASK ((1ul << 24) - 1)
3908
3909 /*
3910 * Routine: ipc_kmsg_copyin_from_user
3911 * Purpose:
3912 * "Copy-in" port rights and out-of-line memory
3913 * in the message.
3914 *
3915 * In all failure cases, the message is left holding
3916 * no rights or memory. However, the message buffer
3917 * is not deallocated. If successful, the message
3918 * contains a valid destination port.
3919 * Conditions:
3920 * Nothing locked.
3921 * Returns:
3922 * MACH_MSG_SUCCESS Successful copyin.
3923 * MACH_SEND_INVALID_HEADER Illegal value in the message header bits.
3924 * MACH_SEND_INVALID_DEST Can't copyin destination port.
3925 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
3926 * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory.
3927 * MACH_SEND_INVALID_RIGHT Can't copyin port right in body.
3928 * MACH_SEND_INVALID_TYPE Bad type specification.
3929 * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data.
3930 */
3931
3932 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)3933 ipc_kmsg_copyin_from_user(
3934 ipc_kmsg_t kmsg,
3935 ipc_space_t space,
3936 vm_map_t map,
3937 mach_msg_priority_t priority,
3938 mach_msg_option_t *optionp,
3939 bool filter_nonfatal)
3940 {
3941 mach_msg_return_t mr;
3942 mach_port_name_t dest_name = CAST_MACH_PORT_TO_NAME(kmsg->ikm_header->msgh_remote_port);
3943
3944 kmsg->ikm_header->msgh_bits &= MACH_MSGH_BITS_USER;
3945
3946 mr = ipc_kmsg_copyin_header(kmsg, space, priority, optionp);
3947
3948 if (mr != MACH_MSG_SUCCESS) {
3949 return mr;
3950 }
3951
3952 /* Get the message filter policy if the task and port support filtering */
3953 mach_msg_filter_id fid = 0;
3954 mach_port_t remote_port = kmsg->ikm_header->msgh_remote_port;
3955 mach_msg_id_t msg_id = kmsg->ikm_header->msgh_id;
3956 void * sblabel = NULL;
3957
3958 if (mach_msg_filter_at_least(MACH_MSG_FILTER_CALLBACKS_VERSION_1) &&
3959 task_get_filter_msg_flag(current_task()) &&
3960 ip_enforce_msg_filtering(remote_port)) {
3961 ip_mq_lock(remote_port);
3962 if (ip_active(remote_port)) {
3963 if (remote_port->ip_service_port) {
3964 ipc_service_port_label_t label = remote_port->ip_splabel;
3965 sblabel = label->ispl_sblabel;
3966 if (label && ipc_service_port_label_is_bootstrap_port(label)) {
3967 /*
3968 * Mask the top byte for messages sent to launchd's bootstrap port.
3969 * Filter any messages with domain 0 (as they correspond to MIG
3970 * based messages)
3971 */
3972 unsigned msg_protocol = msg_id & ~MACH_BOOTSTRAP_PORT_MSG_ID_MASK;
3973 if (!msg_protocol) {
3974 ip_mq_unlock(remote_port);
3975 goto filtered_msg;
3976 }
3977 msg_id = msg_id & MACH_BOOTSTRAP_PORT_MSG_ID_MASK;
3978 }
3979 } else {
3980 assert(!ip_is_kolabeled(remote_port));
3981 /* Connection ports can also have send-side message filters */
3982 sblabel = remote_port->ip_splabel;
3983 }
3984 if (sblabel) {
3985 mach_msg_filter_retain_sblabel_callback(sblabel);
3986 }
3987 }
3988 ip_mq_unlock(remote_port);
3989
3990 if (sblabel && !mach_msg_fetch_filter_policy(sblabel, msg_id, &fid)) {
3991 goto filtered_msg;
3992 }
3993 }
3994
3995 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_SEND) | DBG_FUNC_NONE,
3996 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
3997 (uintptr_t)kmsg->ikm_header->msgh_bits,
3998 (uintptr_t)kmsg->ikm_header->msgh_id,
3999 VM_KERNEL_ADDRPERM((uintptr_t)unsafe_convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg))),
4000 0);
4001
4002 DEBUG_KPRINT_SYSCALL_IPC("ipc_kmsg_copyin_from_user header:\n%.8x\n%.8x\n%p\n%p\n%p\n%.8x\n",
4003 kmsg->ikm_header->msgh_size,
4004 kmsg->ikm_header->msgh_bits,
4005 kmsg->ikm_header->msgh_remote_port,
4006 kmsg->ikm_header->msgh_local_port,
4007 ipc_kmsg_get_voucher_port(kmsg),
4008 kmsg->ikm_header->msgh_id);
4009
4010 if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
4011 mr = ipc_kmsg_copyin_body( kmsg, space, map, optionp);
4012
4013 if (DEBUG_KPRINT_SYSCALL_PREDICATE(DEBUG_KPRINT_SYSCALL_IPC_MASK)) {
4014 __unreachable_ok_push /* unreachable if !DEBUG */
4015 kprintf("body:\n");
4016 for (uint32_t i = 0; i * 4 < (kmsg->ikm_header->msgh_size - sizeof(mach_msg_header_t)); i++) {
4017 kprintf("%.4x\n", ((uint32_t *)(kmsg->ikm_header + 1))[i]);
4018 }
4019 __unreachable_ok_pop
4020 }
4021 }
4022
4023 /* Sign the message contents */
4024 if (mr == MACH_MSG_SUCCESS) {
4025 ikm_sign(kmsg, current_task());
4026 }
4027
4028 return mr;
4029
4030 filtered_msg:
4031 if (!filter_nonfatal) {
4032 mach_port_guard_exception(dest_name, 0, 0, kGUARD_EXC_MSG_FILTERED);
4033 }
4034 /* no descriptors have been copied in yet */
4035 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
4036 return MACH_SEND_MSG_FILTERED;
4037 }
4038
4039 /*
4040 * Routine: ipc_kmsg_copyin_from_kernel
4041 * Purpose:
4042 * "Copy-in" port rights and out-of-line memory
4043 * in a message sent from the kernel.
4044 *
4045 * Because the message comes from the kernel,
4046 * the implementation assumes there are no errors
4047 * or peculiarities in the message.
4048 * Conditions:
4049 * Nothing locked.
4050 */
4051
4052 mach_msg_return_t
ipc_kmsg_copyin_from_kernel(ipc_kmsg_t kmsg)4053 ipc_kmsg_copyin_from_kernel(
4054 ipc_kmsg_t kmsg)
4055 {
4056 mach_msg_bits_t bits = kmsg->ikm_header->msgh_bits;
4057 mach_msg_type_name_t rname = MACH_MSGH_BITS_REMOTE(bits);
4058 mach_msg_type_name_t lname = MACH_MSGH_BITS_LOCAL(bits);
4059 mach_msg_type_name_t vname = MACH_MSGH_BITS_VOUCHER(bits);
4060 ipc_object_t remote = ip_to_object(kmsg->ikm_header->msgh_remote_port);
4061 ipc_object_t local = ip_to_object(kmsg->ikm_header->msgh_local_port);
4062 ipc_object_t voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
4063 ipc_port_t dest = kmsg->ikm_header->msgh_remote_port;
4064
4065 /* translate the destination and reply ports */
4066 if (!IO_VALID(remote)) {
4067 return MACH_SEND_INVALID_DEST;
4068 }
4069
4070 ipc_object_copyin_from_kernel(remote, rname);
4071 if (IO_VALID(local)) {
4072 ipc_object_copyin_from_kernel(local, lname);
4073 }
4074
4075 if (IO_VALID(voucher)) {
4076 ipc_object_copyin_from_kernel(voucher, vname);
4077 }
4078
4079 /*
4080 * The common case is a complex message with no reply port,
4081 * because that is what the memory_object interface uses.
4082 */
4083
4084 if (bits == (MACH_MSGH_BITS_COMPLEX |
4085 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0))) {
4086 bits = (MACH_MSGH_BITS_COMPLEX |
4087 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0));
4088
4089 kmsg->ikm_header->msgh_bits = bits;
4090 } else {
4091 bits = (MACH_MSGH_BITS_OTHER(bits) |
4092 MACH_MSGH_BITS_SET_PORTS(ipc_object_copyin_type(rname),
4093 ipc_object_copyin_type(lname), ipc_object_copyin_type(vname)));
4094
4095 kmsg->ikm_header->msgh_bits = bits;
4096 }
4097
4098 ipc_kmsg_set_qos_kernel(kmsg);
4099
4100 if (bits & MACH_MSGH_BITS_COMPLEX) {
4101 /*
4102 * Check if the remote port accepts ports in the body.
4103 */
4104 if (dest->ip_no_grant) {
4105 mach_msg_descriptor_t *saddr;
4106 mach_msg_body_t *body;
4107 mach_msg_type_number_t i, count;
4108
4109 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
4110 saddr = (mach_msg_descriptor_t *) (body + 1);
4111 count = body->msgh_descriptor_count;
4112
4113 for (i = 0; i < count; i++, saddr++) {
4114 switch (saddr->type.type) {
4115 case MACH_MSG_PORT_DESCRIPTOR:
4116 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
4117 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
4118 /* no descriptors have been copied in yet */
4119 ipc_kmsg_clean_partial(kmsg, 0, NULL, 0, 0);
4120 return MACH_SEND_NO_GRANT_DEST;
4121 }
4122 }
4123 }
4124
4125 mach_msg_descriptor_t *saddr;
4126 mach_msg_body_t *body;
4127 mach_msg_type_number_t i, count;
4128
4129 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
4130 saddr = (mach_msg_descriptor_t *) (body + 1);
4131 count = body->msgh_descriptor_count;
4132
4133 for (i = 0; i < count; i++, saddr++) {
4134 switch (saddr->type.type) {
4135 case MACH_MSG_PORT_DESCRIPTOR: {
4136 mach_msg_type_name_t name;
4137 ipc_object_t object;
4138 mach_msg_port_descriptor_t *dsc;
4139
4140 dsc = &saddr->port;
4141
4142 /* this is really the type SEND, SEND_ONCE, etc. */
4143 name = dsc->disposition;
4144 object = ip_to_object(dsc->name);
4145 dsc->disposition = ipc_object_copyin_type(name);
4146
4147 if (!IO_VALID(object)) {
4148 break;
4149 }
4150
4151 ipc_object_copyin_from_kernel(object, name);
4152
4153 /* CDY avoid circularity when the destination is also */
4154 /* the kernel. This check should be changed into an */
4155 /* assert when the new kobject model is in place since*/
4156 /* ports will not be used in kernel to kernel chats */
4157
4158 /* do not lock remote port, use raw pointer comparison */
4159 if (!ip_in_space_noauth(ip_object_to_port(remote), ipc_space_kernel)) {
4160 /* remote port could be dead, in-transit or in an ipc space */
4161 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4162 ipc_port_check_circularity(ip_object_to_port(object),
4163 ip_object_to_port(remote))) {
4164 kmsg->ikm_header->msgh_bits |=
4165 MACH_MSGH_BITS_CIRCULAR;
4166 }
4167 }
4168 break;
4169 }
4170 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
4171 case MACH_MSG_OOL_DESCRIPTOR: {
4172 /*
4173 * The sender should supply ready-made memory, i.e.
4174 * a vm_map_copy_t, so we don't need to do anything.
4175 */
4176 break;
4177 }
4178 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
4179 ipc_object_t *objects;
4180 unsigned int j;
4181 mach_msg_type_name_t name;
4182 mach_msg_ool_ports_descriptor_t *dsc;
4183
4184 dsc = (mach_msg_ool_ports_descriptor_t *)&saddr->ool_ports;
4185
4186 /* this is really the type SEND, SEND_ONCE, etc. */
4187 name = dsc->disposition;
4188 dsc->disposition = ipc_object_copyin_type(name);
4189
4190 objects = (ipc_object_t *) dsc->address;
4191
4192 for (j = 0; j < dsc->count; j++) {
4193 ipc_object_t object = objects[j];
4194
4195 if (!IO_VALID(object)) {
4196 continue;
4197 }
4198
4199 ipc_object_copyin_from_kernel(object, name);
4200
4201 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4202 ipc_port_check_circularity(ip_object_to_port(object),
4203 ip_object_to_port(remote))) {
4204 kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4205 }
4206 }
4207 break;
4208 }
4209 case MACH_MSG_GUARDED_PORT_DESCRIPTOR: {
4210 mach_msg_guarded_port_descriptor_t *dsc = (typeof(dsc)) & saddr->guarded_port;
4211 mach_msg_type_name_t disp = dsc->disposition;
4212 ipc_object_t object = ip_to_object(dsc->name);
4213 dsc->disposition = ipc_object_copyin_type(disp);
4214 assert(dsc->flags == 0);
4215
4216 if (!IO_VALID(object)) {
4217 break;
4218 }
4219
4220 ipc_object_copyin_from_kernel(object, disp);
4221 /*
4222 * avoid circularity when the destination is also
4223 * the kernel. This check should be changed into an
4224 * assert when the new kobject model is in place since
4225 * ports will not be used in kernel to kernel chats
4226 */
4227
4228 /* do not lock remote port, use raw pointer comparison */
4229 if (!ip_in_space_noauth(ip_object_to_port(remote), ipc_space_kernel)) {
4230 /* remote port could be dead, in-transit or in an ipc space */
4231 if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
4232 ipc_port_check_circularity(ip_object_to_port(object),
4233 ip_object_to_port(remote))) {
4234 kmsg->ikm_header->msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
4235 }
4236 }
4237 break;
4238 }
4239 default: {
4240 #if MACH_ASSERT
4241 panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
4242 #endif /* MACH_ASSERT */
4243 }
4244 }
4245 }
4246 }
4247
4248 /* Add the signature to the message */
4249 ikm_sign(kmsg, TASK_NULL);
4250
4251 return MACH_MSG_SUCCESS;
4252 }
4253
4254 /*
4255 * Routine: ipc_kmsg_copyout_header
4256 * Purpose:
4257 * "Copy-out" port rights in the header of a message.
4258 * Operates atomically; if it doesn't succeed the
4259 * message header and the space are left untouched.
4260 * If it does succeed the remote/local port fields
4261 * contain port names instead of object pointers,
4262 * and the bits field is updated.
4263 * Conditions:
4264 * Nothing locked.
4265 * Returns:
4266 * MACH_MSG_SUCCESS Copied out port rights.
4267 * MACH_RCV_INVALID_NOTIFY
4268 * Notify is non-null and doesn't name a receive right.
4269 * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.)
4270 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
4271 * The space is dead.
4272 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE
4273 * No room in space for another name.
4274 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
4275 * Couldn't allocate memory for the reply port.
4276 * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL
4277 * Couldn't allocate memory for the dead-name request.
4278 */
4279
4280 static mach_msg_return_t
ipc_kmsg_copyout_header(ipc_kmsg_t kmsg,ipc_space_t space,mach_msg_option_t option)4281 ipc_kmsg_copyout_header(
4282 ipc_kmsg_t kmsg,
4283 ipc_space_t space,
4284 mach_msg_option_t option)
4285 {
4286 mach_msg_header_t *msg = kmsg->ikm_header;
4287 mach_msg_bits_t mbits = msg->msgh_bits;
4288 ipc_port_t dest = msg->msgh_remote_port;
4289
4290 assert(IP_VALID(dest));
4291
4292 /*
4293 * While we still hold a reference on the received-from port,
4294 * process all send-possible notfications we received along with
4295 * the message.
4296 */
4297 ipc_port_spnotify(dest);
4298
4299 {
4300 mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
4301 mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
4302 mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
4303 ipc_port_t reply = msg->msgh_local_port;
4304 ipc_port_t release_reply_port = IP_NULL;
4305 mach_port_name_t dest_name, reply_name;
4306
4307 ipc_port_t voucher = ipc_kmsg_get_voucher_port(kmsg);
4308 uintptr_t voucher_addr = 0;
4309 ipc_port_t release_voucher_port = IP_NULL;
4310 mach_port_name_t voucher_name;
4311
4312 uint32_t entries_held = 0;
4313 boolean_t need_write_lock = FALSE;
4314 ipc_object_copyout_flags_t reply_copyout_options = IPC_OBJECT_COPYOUT_FLAGS_NONE;
4315 kern_return_t kr;
4316
4317 /*
4318 * Reserve any potentially needed entries in the target space.
4319 * We'll free any unused before unlocking the space.
4320 */
4321 if (IP_VALID(reply)) {
4322 entries_held++;
4323 need_write_lock = TRUE;
4324 }
4325 if (IP_VALID(voucher)) {
4326 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
4327
4328 if ((option & MACH_RCV_VOUCHER) != 0) {
4329 entries_held++;
4330 }
4331 need_write_lock = TRUE;
4332 voucher_addr = unsafe_convert_port_to_voucher(voucher);
4333 }
4334
4335 if (need_write_lock) {
4336 handle_reply_again:
4337 is_write_lock(space);
4338
4339 while (entries_held) {
4340 if (!is_active(space)) {
4341 is_write_unlock(space);
4342 return MACH_RCV_HEADER_ERROR |
4343 MACH_MSG_IPC_SPACE;
4344 }
4345
4346 kr = ipc_entries_hold(space, entries_held);
4347 if (KERN_SUCCESS == kr) {
4348 break;
4349 }
4350
4351 kr = ipc_entry_grow_table(space, ITS_SIZE_NONE);
4352 if (KERN_SUCCESS != kr) {
4353 return MACH_RCV_HEADER_ERROR |
4354 MACH_MSG_IPC_SPACE;
4355 }
4356 /* space was unlocked and relocked - retry */
4357 }
4358
4359 /* Handle reply port. */
4360 if (IP_VALID(reply)) {
4361 ipc_port_t reply_subst = IP_NULL;
4362 ipc_entry_t entry;
4363
4364 ip_mq_lock(reply);
4365
4366 /* Is the reply port still active and allowed to be copied out? */
4367 if (!ip_active(reply) ||
4368 !ip_label_check(space, reply, reply_type,
4369 &reply_copyout_options, &reply_subst)) {
4370 /* clear the context value */
4371 reply->ip_reply_context = 0;
4372 ip_mq_unlock(reply);
4373
4374 assert(reply_subst == IP_NULL);
4375 release_reply_port = reply;
4376 reply = IP_DEAD;
4377 reply_name = MACH_PORT_DEAD;
4378 goto done_with_reply;
4379 }
4380
4381 /* is the kolabel requesting a substitution */
4382 if (reply_subst != IP_NULL) {
4383 /*
4384 * port is unlocked, its right consumed
4385 * space is unlocked
4386 */
4387 assert(reply_type == MACH_MSG_TYPE_PORT_SEND);
4388 msg->msgh_local_port = reply = reply_subst;
4389 goto handle_reply_again;
4390 }
4391
4392
4393 /* Is there already an entry we can use? */
4394 if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
4395 ipc_right_reverse(space, ip_to_object(reply), &reply_name, &entry)) {
4396 assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
4397 } else {
4398 /* claim a held entry for the reply port */
4399 assert(entries_held > 0);
4400 entries_held--;
4401 ipc_entry_claim(space, ip_to_object(reply),
4402 &reply_name, &entry);
4403 }
4404
4405 /* space and reply port are locked and active */
4406 ip_reference(reply); /* hold onto the reply port */
4407
4408 /*
4409 * If the receiver would like to enforce strict reply
4410 * semantics, and the message looks like it expects a reply,
4411 * and contains a voucher, then link the context in the
4412 * voucher with the reply port so that the next message sent
4413 * to the reply port must come from a thread that has a
4414 * matching context (voucher).
4415 */
4416 if (enforce_strict_reply && MACH_RCV_WITH_STRICT_REPLY(option) && IP_VALID(voucher)) {
4417 if (ipc_kmsg_validate_reply_port_locked(reply, option) != KERN_SUCCESS) {
4418 /* if the receiver isn't happy with the reply port: fail the receive. */
4419 assert(!ip_is_pinned(reply));
4420 ipc_entry_dealloc(space, ip_to_object(reply),
4421 reply_name, entry);
4422 ip_mq_unlock(reply);
4423 is_write_unlock(space);
4424 ip_release(reply);
4425 return MACH_RCV_INVALID_REPLY;
4426 }
4427 ipc_kmsg_link_reply_context_locked(reply, voucher);
4428 } else {
4429 /*
4430 * if the receive did not choose to participate
4431 * in the strict reply/RPC, then don't enforce
4432 * anything (as this could lead to booby-trapped
4433 * messages that kill the server).
4434 */
4435 reply->ip_reply_context = 0;
4436 }
4437
4438 kr = ipc_right_copyout(space, reply_name, entry,
4439 reply_type, IPC_OBJECT_COPYOUT_FLAGS_NONE, NULL, NULL,
4440 ip_to_object(reply));
4441 assert(kr == KERN_SUCCESS);
4442 /* reply port is unlocked */
4443 } else {
4444 reply_name = CAST_MACH_PORT_TO_NAME(reply);
4445 }
4446
4447 done_with_reply:
4448
4449 /* Handle voucher port. */
4450 if (voucher_type != MACH_MSGH_BITS_ZERO) {
4451 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
4452
4453 if (!IP_VALID(voucher)) {
4454 if ((option & MACH_RCV_VOUCHER) == 0) {
4455 voucher_type = MACH_MSGH_BITS_ZERO;
4456 }
4457 voucher_name = MACH_PORT_NULL;
4458 goto done_with_voucher;
4459 }
4460
4461 #if CONFIG_PREADOPT_TG
4462 struct knote *kn = current_thread()->ith_knote;
4463 if (kn == ITH_KNOTE_NULL || kn == ITH_KNOTE_PSEUDO) {
4464 /*
4465 * We are not in this path of voucher copyout because of
4466 * kevent - we cannot expect a voucher preadopt happening on
4467 * this thread for this message later on
4468 */
4469 KDBG_DEBUG(MACHDBG_CODE(DBG_MACH_THREAD_GROUP, MACH_THREAD_GROUP_PREADOPT_NA),
4470 thread_tid(current_thread()), 0, 0, 0);
4471 }
4472 #endif
4473
4474 /* clear voucher from its hiding place back in the kmsg */
4475 ipc_kmsg_clear_voucher_port(kmsg);
4476
4477 if ((option & MACH_RCV_VOUCHER) != 0) {
4478 ipc_entry_t entry;
4479
4480 ip_mq_lock(voucher);
4481
4482 if (ipc_right_reverse(space, ip_to_object(voucher),
4483 &voucher_name, &entry)) {
4484 assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
4485 } else {
4486 assert(entries_held > 0);
4487 entries_held--;
4488 ipc_entry_claim(space, ip_to_object(voucher), &voucher_name, &entry);
4489 }
4490 /* space is locked and active */
4491
4492 assert(ip_kotype(voucher) == IKOT_VOUCHER);
4493 kr = ipc_right_copyout(space, voucher_name, entry,
4494 MACH_MSG_TYPE_MOVE_SEND, IPC_OBJECT_COPYOUT_FLAGS_NONE,
4495 NULL, NULL, ip_to_object(voucher));
4496 /* voucher port is unlocked */
4497 } else {
4498 voucher_type = MACH_MSGH_BITS_ZERO;
4499 release_voucher_port = voucher;
4500 voucher_name = MACH_PORT_NULL;
4501 }
4502 } else {
4503 voucher_name = msg->msgh_voucher_port;
4504 }
4505
4506 done_with_voucher:
4507
4508 ip_mq_lock(dest);
4509 is_write_unlock(space);
4510 } else {
4511 /*
4512 * No reply or voucher port! This is an easy case.
4513 *
4514 * We only need to check that the space is still
4515 * active once we locked the destination:
4516 *
4517 * - if the space holds a receive right for `dest`,
4518 * then holding the port lock means we can't fail
4519 * to notice if the space went dead because
4520 * the is_write_unlock() will pair with
4521 * os_atomic_barrier_before_lock_acquire() + ip_mq_lock().
4522 *
4523 * - if this space doesn't hold a receive right
4524 * for `dest`, then `dest->ip_receiver` points
4525 * elsewhere, and ipc_object_copyout_dest() will
4526 * handle this situation, and failing to notice
4527 * that the space was dead is accetable.
4528 */
4529
4530 os_atomic_barrier_before_lock_acquire();
4531 ip_mq_lock(dest);
4532 if (!is_active(space)) {
4533 ip_mq_unlock(dest);
4534 return MACH_RCV_HEADER_ERROR | MACH_MSG_IPC_SPACE;
4535 }
4536
4537 reply_name = CAST_MACH_PORT_TO_NAME(reply);
4538
4539 if (voucher_type != MACH_MSGH_BITS_ZERO) {
4540 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
4541 if ((option & MACH_RCV_VOUCHER) == 0) {
4542 voucher_type = MACH_MSGH_BITS_ZERO;
4543 }
4544 voucher_name = MACH_PORT_NULL;
4545 } else {
4546 voucher_name = msg->msgh_voucher_port;
4547 }
4548 }
4549
4550 /*
4551 * At this point, the space is unlocked and the destination
4552 * port is locked.
4553 * reply_name is taken care of; we still need dest_name.
4554 * We still hold a ref for reply (if it is valid).
4555 *
4556 * If the space holds receive rights for the destination,
4557 * we return its name for the right. Otherwise the task
4558 * managed to destroy or give away the receive right between
4559 * receiving the message and this copyout. If the destination
4560 * is dead, return MACH_PORT_DEAD, and if the receive right
4561 * exists somewhere else (another space, in transit)
4562 * return MACH_PORT_NULL.
4563 *
4564 * Making this copyout operation atomic with the previous
4565 * copyout of the reply port is a bit tricky. If there was
4566 * no real reply port (it wasn't IP_VALID) then this isn't
4567 * an issue. If the reply port was dead at copyout time,
4568 * then we are OK, because if dest is dead we serialize
4569 * after the death of both ports and if dest is alive
4570 * we serialize after reply died but before dest's (later) death.
4571 * So assume reply was alive when we copied it out. If dest
4572 * is alive, then we are OK because we serialize before
4573 * the ports' deaths. So assume dest is dead when we look at it.
4574 * If reply dies/died after dest, then we are OK because
4575 * we serialize after dest died but before reply dies.
4576 * So the hard case is when reply is alive at copyout,
4577 * dest is dead at copyout, and reply died before dest died.
4578 * In this case pretend that dest is still alive, so
4579 * we serialize while both ports are alive.
4580 *
4581 * Because the space lock is held across the copyout of reply
4582 * and locking dest, the receive right for dest can't move
4583 * in or out of the space while the copyouts happen, so
4584 * that isn't an atomicity problem. In the last hard case
4585 * above, this implies that when dest is dead that the
4586 * space couldn't have had receive rights for dest at
4587 * the time reply was copied-out, so when we pretend
4588 * that dest is still alive, we can return MACH_PORT_NULL.
4589 *
4590 * If dest == reply, then we have to make it look like
4591 * either both copyouts happened before the port died,
4592 * or both happened after the port died. This special
4593 * case works naturally if the timestamp comparison
4594 * is done correctly.
4595 */
4596
4597 if (ip_active(dest)) {
4598 ipc_object_copyout_dest(space, ip_to_object(dest),
4599 dest_type, &dest_name);
4600 /* dest is unlocked */
4601 } else {
4602 ipc_port_timestamp_t timestamp;
4603
4604 timestamp = ip_get_death_time(dest);
4605 ip_mq_unlock(dest);
4606 ip_release(dest);
4607
4608 if (IP_VALID(reply)) {
4609 ip_mq_lock(reply);
4610 if (ip_active(reply) ||
4611 IP_TIMESTAMP_ORDER(timestamp,
4612 ip_get_death_time(reply))) {
4613 dest_name = MACH_PORT_DEAD;
4614 } else {
4615 dest_name = MACH_PORT_NULL;
4616 }
4617 ip_mq_unlock(reply);
4618 } else {
4619 dest_name = MACH_PORT_DEAD;
4620 }
4621 }
4622
4623 if (IP_VALID(reply)) {
4624 ip_release(reply);
4625 }
4626
4627 if (IP_VALID(release_reply_port)) {
4628 if (reply_type == MACH_MSG_TYPE_PORT_SEND_ONCE) {
4629 ipc_port_release_sonce(release_reply_port);
4630 } else {
4631 ipc_port_release_send(release_reply_port);
4632 }
4633 }
4634
4635 if ((option & MACH_RCV_VOUCHER) != 0) {
4636 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV) | DBG_FUNC_NONE,
4637 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
4638 (uintptr_t)kmsg->ikm_header->msgh_bits,
4639 (uintptr_t)kmsg->ikm_header->msgh_id,
4640 VM_KERNEL_ADDRPERM(voucher_addr), 0);
4641 } else {
4642 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV_VOUCHER_REFUSED) | DBG_FUNC_NONE,
4643 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
4644 (uintptr_t)kmsg->ikm_header->msgh_bits,
4645 (uintptr_t)kmsg->ikm_header->msgh_id,
4646 VM_KERNEL_ADDRPERM(voucher_addr), 0);
4647 }
4648
4649 if (IP_VALID(release_voucher_port)) {
4650 ipc_port_release_send(release_voucher_port);
4651 }
4652
4653 msg->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
4654 voucher_type, mbits);
4655 msg->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
4656 msg->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
4657 msg->msgh_voucher_port = voucher_name;
4658 }
4659
4660 return MACH_MSG_SUCCESS;
4661 }
4662
4663 /*
4664 * Routine: ipc_kmsg_copyout_object
4665 * Purpose:
4666 * Copy-out a port right. Always returns a name,
4667 * even for unsuccessful return codes. Always
4668 * consumes the supplied object.
4669 * Conditions:
4670 * Nothing locked.
4671 * Returns:
4672 * MACH_MSG_SUCCESS The space acquired the right
4673 * (name is valid) or the object is dead (MACH_PORT_DEAD).
4674 * MACH_MSG_IPC_SPACE No room in space for the right,
4675 * or the space is dead. (Name is MACH_PORT_NULL.)
4676 * MACH_MSG_IPC_KERNEL Kernel resource shortage.
4677 * (Name is MACH_PORT_NULL.)
4678 */
4679 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)4680 ipc_kmsg_copyout_object(
4681 ipc_space_t space,
4682 ipc_object_t object,
4683 mach_msg_type_name_t msgt_name,
4684 mach_port_context_t *context,
4685 mach_msg_guard_flags_t *guard_flags,
4686 mach_port_name_t *namep)
4687 {
4688 kern_return_t kr;
4689
4690 if (!IO_VALID(object)) {
4691 *namep = CAST_MACH_PORT_TO_NAME(object);
4692 return MACH_MSG_SUCCESS;
4693 }
4694
4695 kr = ipc_object_copyout(space, object, msgt_name, IPC_OBJECT_COPYOUT_FLAGS_NONE,
4696 context, guard_flags, namep);
4697 if (kr != KERN_SUCCESS) {
4698 if (kr == KERN_INVALID_CAPABILITY) {
4699 *namep = MACH_PORT_DEAD;
4700 } else {
4701 *namep = MACH_PORT_NULL;
4702
4703 if (kr == KERN_RESOURCE_SHORTAGE) {
4704 return MACH_MSG_IPC_KERNEL;
4705 } else {
4706 return MACH_MSG_IPC_SPACE;
4707 }
4708 }
4709 }
4710
4711 return MACH_MSG_SUCCESS;
4712 }
4713
4714 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)4715 ipc_kmsg_copyout_port_descriptor(
4716 mach_msg_descriptor_t *dsc,
4717 mach_msg_descriptor_t *dest_dsc,
4718 ipc_space_t space,
4719 kern_return_t *mr)
4720 {
4721 mach_msg_user_port_descriptor_t *user_dsc;
4722 mach_port_t port;
4723 mach_port_name_t name;
4724 mach_msg_type_name_t disp;
4725
4726 /* Copyout port right carried in the message */
4727 port = dsc->port.name;
4728 disp = dsc->port.disposition;
4729 *mr |= ipc_kmsg_copyout_object(space,
4730 ip_to_object(port), disp, NULL, NULL, &name);
4731
4732 // point to the start of this port descriptor
4733 user_dsc = ((mach_msg_user_port_descriptor_t *)dest_dsc - 1);
4734 bzero((void *)user_dsc, sizeof(*user_dsc));
4735 user_dsc->name = CAST_MACH_PORT_TO_NAME(name);
4736 user_dsc->disposition = disp;
4737 user_dsc->type = MACH_MSG_PORT_DESCRIPTOR;
4738
4739 return (mach_msg_descriptor_t *)user_dsc;
4740 }
4741
4742 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)4743 ipc_kmsg_copyout_ool_descriptor(
4744 mach_msg_ool_descriptor_t *dsc,
4745 mach_msg_descriptor_t *user_dsc,
4746 int is_64bit,
4747 vm_map_t map,
4748 mach_msg_return_t *mr)
4749 {
4750 vm_map_copy_t copy;
4751 vm_map_address_t rcv_addr;
4752 mach_msg_copy_options_t copy_options;
4753 vm_map_size_t size;
4754 mach_msg_descriptor_type_t dsc_type;
4755 boolean_t misaligned = FALSE;
4756
4757 copy = (vm_map_copy_t)dsc->address;
4758 size = (vm_map_size_t)dsc->size;
4759 copy_options = dsc->copy;
4760 assert(copy_options != MACH_MSG_KALLOC_COPY_T);
4761 dsc_type = dsc->type;
4762
4763 if (copy != VM_MAP_COPY_NULL) {
4764 kern_return_t kr;
4765
4766 rcv_addr = 0;
4767 if (vm_map_copy_validate_size(map, copy, &size) == FALSE) {
4768 panic("Inconsistent OOL/copyout size on %p: expected %d, got %lld @%p",
4769 dsc, dsc->size, (unsigned long long)copy->size, copy);
4770 }
4771
4772 if ((copy->type == VM_MAP_COPY_ENTRY_LIST) &&
4773 (trunc_page(copy->offset) != copy->offset ||
4774 round_page(dsc->size) != dsc->size)) {
4775 misaligned = TRUE;
4776 }
4777
4778 if (misaligned) {
4779 mach_vm_offset_t rounded_addr;
4780 vm_map_size_t rounded_size;
4781 vm_map_offset_t effective_page_mask, effective_page_size;
4782
4783 effective_page_mask = VM_MAP_PAGE_MASK(map);
4784 effective_page_size = effective_page_mask + 1;
4785
4786 rounded_size = vm_map_round_page(copy->offset + size, effective_page_mask) - vm_map_trunc_page(copy->offset, effective_page_mask);
4787
4788 kr = mach_vm_allocate_kernel(map, &rounded_addr,
4789 rounded_size, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_IPC);
4790
4791 if (kr == KERN_SUCCESS) {
4792 /*
4793 * vm_map_copy_overwrite does a full copy
4794 * if size is too small to optimize.
4795 * So we tried skipping the offset adjustment
4796 * if we fail the 'size' test.
4797 *
4798 * if (size >= VM_MAP_COPY_OVERWRITE_OPTIMIZATION_THRESHOLD_PAGES * effective_page_size) {
4799 *
4800 * This resulted in leaked memory especially on the
4801 * older watches (16k user - 4k kernel) because we
4802 * would do a physical copy into the start of this
4803 * rounded range but could leak part of it
4804 * on deallocation if the 'size' being deallocated
4805 * does not cover the full range. So instead we do
4806 * the misalignment adjustment always so that on
4807 * deallocation we will remove the full range.
4808 */
4809 if ((rounded_addr & effective_page_mask) !=
4810 (copy->offset & effective_page_mask)) {
4811 /*
4812 * Need similar mis-alignment of source and destination...
4813 */
4814 rounded_addr += (copy->offset & effective_page_mask);
4815
4816 assert((rounded_addr & effective_page_mask) == (copy->offset & effective_page_mask));
4817 }
4818 rcv_addr = rounded_addr;
4819
4820 kr = vm_map_copy_overwrite(map, rcv_addr, copy, size, FALSE);
4821 }
4822 } else {
4823 kr = vm_map_copyout_size(map, &rcv_addr, copy, size);
4824 }
4825 if (kr != KERN_SUCCESS) {
4826 if (kr == KERN_RESOURCE_SHORTAGE) {
4827 *mr |= MACH_MSG_VM_KERNEL;
4828 } else {
4829 *mr |= MACH_MSG_VM_SPACE;
4830 }
4831 vm_map_copy_discard(copy);
4832 rcv_addr = 0;
4833 size = 0;
4834 }
4835 } else {
4836 rcv_addr = 0;
4837 size = 0;
4838 }
4839
4840 /*
4841 * Now update the descriptor as the user would see it.
4842 * This may require expanding the descriptor to the user
4843 * visible size. There is already space allocated for
4844 * this in what naddr points to.
4845 */
4846 if (is_64bit) {
4847 mach_msg_ool_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4848 user_ool_dsc--;
4849 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4850
4851 user_ool_dsc->address = rcv_addr;
4852 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4853 TRUE : FALSE;
4854 user_ool_dsc->copy = copy_options;
4855 user_ool_dsc->type = dsc_type;
4856 user_ool_dsc->size = (mach_msg_size_t)size;
4857
4858 user_dsc = (typeof(user_dsc))user_ool_dsc;
4859 } else {
4860 mach_msg_ool_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4861 user_ool_dsc--;
4862 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4863
4864 user_ool_dsc->address = CAST_DOWN_EXPLICIT(uint32_t, rcv_addr);
4865 user_ool_dsc->size = (mach_msg_size_t)size;
4866 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4867 TRUE : FALSE;
4868 user_ool_dsc->copy = copy_options;
4869 user_ool_dsc->type = dsc_type;
4870
4871 user_dsc = (typeof(user_dsc))user_ool_dsc;
4872 }
4873 return user_dsc;
4874 }
4875
4876 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)4877 ipc_kmsg_copyout_ool_ports_descriptor(mach_msg_ool_ports_descriptor_t *dsc,
4878 mach_msg_descriptor_t *user_dsc,
4879 int is_64bit,
4880 vm_map_t map,
4881 ipc_space_t space,
4882 ipc_kmsg_t kmsg,
4883 mach_msg_return_t *mr)
4884 {
4885 mach_vm_offset_t rcv_addr = 0;
4886 mach_msg_type_name_t disp;
4887 mach_msg_type_number_t count, i;
4888 vm_size_t ports_length, names_length;
4889 mach_msg_copy_options_t copy_options = MACH_MSG_VIRTUAL_COPY;
4890
4891 count = dsc->count;
4892 disp = dsc->disposition;
4893 ports_length = count * sizeof(mach_port_t);
4894 names_length = count * sizeof(mach_port_name_t);
4895
4896 if (ports_length != 0 && dsc->address != 0) {
4897 if (copy_options == MACH_MSG_VIRTUAL_COPY) {
4898 /*
4899 * Dynamically allocate the region
4900 */
4901 vm_tag_t tag;
4902 if (vm_kernel_map_is_kernel(map)) {
4903 tag = VM_KERN_MEMORY_IPC;
4904 } else {
4905 tag = VM_MEMORY_MACH_MSG;
4906 }
4907
4908 kern_return_t kr;
4909 if ((kr = mach_vm_allocate_kernel(map, &rcv_addr,
4910 (mach_vm_size_t)names_length,
4911 VM_FLAGS_ANYWHERE, tag)) != KERN_SUCCESS) {
4912 ipc_kmsg_clean_body(kmsg, 1, (mach_msg_descriptor_t *)dsc);
4913 rcv_addr = 0;
4914
4915 if (kr == KERN_RESOURCE_SHORTAGE) {
4916 *mr |= MACH_MSG_VM_KERNEL;
4917 } else {
4918 *mr |= MACH_MSG_VM_SPACE;
4919 }
4920 }
4921 }
4922
4923 /*
4924 * Handle the port rights and copy out the names
4925 * for those rights out to user-space.
4926 */
4927 if (rcv_addr != 0) {
4928 ipc_object_t *objects = (ipc_object_t *) dsc->address;
4929 mach_port_name_t *names = (mach_port_name_t *) dsc->address;
4930
4931 /* copyout port rights carried in the message */
4932
4933 for (i = 0; i < count; i++) {
4934 ipc_object_t object = objects[i];
4935
4936 *mr |= ipc_kmsg_copyout_object(space, object,
4937 disp, NULL, NULL, &names[i]);
4938 }
4939
4940 /* copyout to memory allocated above */
4941 void *data = dsc->address;
4942 if (copyoutmap(map, data, rcv_addr, names_length) != KERN_SUCCESS) {
4943 *mr |= MACH_MSG_VM_SPACE;
4944 }
4945 kfree_type(mach_port_t, count, data);
4946 }
4947 } else {
4948 rcv_addr = 0;
4949 }
4950
4951 /*
4952 * Now update the descriptor based on the information
4953 * calculated above.
4954 */
4955 if (is_64bit) {
4956 mach_msg_ool_ports_descriptor64_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4957 user_ool_dsc--;
4958 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4959
4960 user_ool_dsc->address = rcv_addr;
4961 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4962 TRUE : FALSE;
4963 user_ool_dsc->copy = copy_options;
4964 user_ool_dsc->disposition = disp;
4965 user_ool_dsc->type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
4966 user_ool_dsc->count = count;
4967
4968 user_dsc = (typeof(user_dsc))user_ool_dsc;
4969 } else {
4970 mach_msg_ool_ports_descriptor32_t *user_ool_dsc = (typeof(user_ool_dsc))user_dsc;
4971 user_ool_dsc--;
4972 bzero((void *)user_ool_dsc, sizeof(*user_ool_dsc));
4973
4974 user_ool_dsc->address = CAST_DOWN_EXPLICIT(uint32_t, rcv_addr);
4975 user_ool_dsc->count = count;
4976 user_ool_dsc->deallocate = (copy_options == MACH_MSG_VIRTUAL_COPY) ?
4977 TRUE : FALSE;
4978 user_ool_dsc->copy = copy_options;
4979 user_ool_dsc->disposition = disp;
4980 user_ool_dsc->type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
4981
4982 user_dsc = (typeof(user_dsc))user_ool_dsc;
4983 }
4984 return user_dsc;
4985 }
4986
4987 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)4988 ipc_kmsg_copyout_guarded_port_descriptor(
4989 mach_msg_guarded_port_descriptor_t *dsc,
4990 mach_msg_descriptor_t *dest_dsc,
4991 int is_64bit,
4992 __unused ipc_kmsg_t kmsg,
4993 ipc_space_t space,
4994 mach_msg_option_t option,
4995 kern_return_t *mr)
4996 {
4997 mach_port_t port;
4998 mach_port_name_t name = MACH_PORT_NULL;
4999 mach_msg_type_name_t disp;
5000 mach_msg_guard_flags_t guard_flags;
5001 mach_port_context_t context;
5002
5003 /* Copyout port right carried in the message */
5004 port = dsc->name;
5005 disp = dsc->disposition;
5006 guard_flags = dsc->flags;
5007 context = 0;
5008
5009 /* Currently kernel_task doesnt support receiving guarded port descriptors */
5010 struct knote *kn = current_thread()->ith_knote;
5011 if ((kn != ITH_KNOTE_PSEUDO) && ((option & MACH_RCV_GUARDED_DESC) == 0)) {
5012 #if DEVELOPMENT || DEBUG
5013 /*
5014 * Simulated crash needed for debugging, notifies the receiver to opt into receiving
5015 * guarded descriptors.
5016 */
5017 mach_port_guard_exception(current_thread()->ith_receiver_name,
5018 0, 0, kGUARD_EXC_RCV_GUARDED_DESC);
5019 #endif
5020 KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_DESTROY_GUARDED_DESC), current_thread()->ith_receiver_name,
5021 VM_KERNEL_ADDRPERM(port), disp, guard_flags);
5022 ipc_object_destroy(ip_to_object(port), disp);
5023 mach_msg_user_port_descriptor_t *user_dsc = (typeof(user_dsc))dest_dsc;
5024 user_dsc--; // point to the start of this port descriptor
5025 bzero((void *)user_dsc, sizeof(*user_dsc));
5026 user_dsc->name = name;
5027 user_dsc->disposition = disp;
5028 user_dsc->type = MACH_MSG_PORT_DESCRIPTOR;
5029 dest_dsc = (typeof(dest_dsc))user_dsc;
5030 } else {
5031 *mr |= ipc_kmsg_copyout_object(space,
5032 ip_to_object(port), disp, &context, &guard_flags, &name);
5033
5034 if (!is_64bit) {
5035 mach_msg_guarded_port_descriptor32_t *user_dsc = (typeof(user_dsc))dest_dsc;
5036 user_dsc--; // point to the start of this port descriptor
5037 bzero((void *)user_dsc, sizeof(*user_dsc));
5038 user_dsc->name = name;
5039 user_dsc->flags = guard_flags;
5040 user_dsc->disposition = disp;
5041 user_dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
5042 user_dsc->context = CAST_DOWN_EXPLICIT(uint32_t, context);
5043 dest_dsc = (typeof(dest_dsc))user_dsc;
5044 } else {
5045 mach_msg_guarded_port_descriptor64_t *user_dsc = (typeof(user_dsc))dest_dsc;
5046 user_dsc--; // point to the start of this port descriptor
5047 bzero((void *)user_dsc, sizeof(*user_dsc));
5048 user_dsc->name = name;
5049 user_dsc->flags = guard_flags;
5050 user_dsc->disposition = disp;
5051 user_dsc->type = MACH_MSG_GUARDED_PORT_DESCRIPTOR;
5052 user_dsc->context = context;
5053 dest_dsc = (typeof(dest_dsc))user_dsc;
5054 }
5055 }
5056
5057 return (mach_msg_descriptor_t *)dest_dsc;
5058 }
5059
5060
5061 /*
5062 * Routine: ipc_kmsg_copyout_body
5063 * Purpose:
5064 * "Copy-out" port rights and out-of-line memory
5065 * in the body of a message.
5066 *
5067 * The error codes are a combination of special bits.
5068 * The copyout proceeds despite errors.
5069 * Conditions:
5070 * Nothing locked.
5071 * Returns:
5072 * MACH_MSG_SUCCESS Successful copyout.
5073 * MACH_MSG_IPC_SPACE No room for port right in name space.
5074 * MACH_MSG_VM_SPACE No room for memory in address space.
5075 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
5076 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
5077 * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT
5078 */
5079
5080 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)5081 ipc_kmsg_copyout_body(
5082 ipc_kmsg_t kmsg,
5083 ipc_space_t space,
5084 vm_map_t map,
5085 mach_msg_option_t option,
5086 mach_msg_body_t *slist)
5087 {
5088 mach_msg_body_t *body;
5089 mach_msg_descriptor_t *kern_dsc, *user_dsc;
5090 mach_msg_descriptor_t *saddr;
5091 mach_msg_type_number_t dsc_count, sdsc_count;
5092 mach_msg_return_t mr = MACH_MSG_SUCCESS;
5093 boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
5094
5095 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
5096 dsc_count = body->msgh_descriptor_count;
5097 kern_dsc = (mach_msg_descriptor_t *) (body + 1);
5098 /* Point user_dsc just after the end of all the descriptors */
5099 user_dsc = &kern_dsc[dsc_count];
5100
5101 assert(current_task() != kernel_task);
5102
5103 /* Do scatter list setup */
5104 if (slist != MACH_MSG_BODY_NULL) {
5105 panic("Scatter lists disabled");
5106 saddr = (mach_msg_descriptor_t *) (slist + 1);
5107 sdsc_count = slist->msgh_descriptor_count;
5108 } else {
5109 saddr = MACH_MSG_DESCRIPTOR_NULL;
5110 sdsc_count = 0;
5111 }
5112
5113 /* Now process the descriptors - in reverse order */
5114 for (mach_msg_type_number_t i = dsc_count; i-- > 0;) {
5115 switch (kern_dsc[i].type.type) {
5116 case MACH_MSG_PORT_DESCRIPTOR:
5117 user_dsc = ipc_kmsg_copyout_port_descriptor(&kern_dsc[i],
5118 user_dsc, space, &mr);
5119 break;
5120 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
5121 case MACH_MSG_OOL_DESCRIPTOR:
5122 user_dsc = ipc_kmsg_copyout_ool_descriptor(
5123 (mach_msg_ool_descriptor_t *)&kern_dsc[i],
5124 user_dsc, is_task_64bit, map, &mr);
5125 break;
5126 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
5127 user_dsc = ipc_kmsg_copyout_ool_ports_descriptor(
5128 (mach_msg_ool_ports_descriptor_t *)&kern_dsc[i],
5129 user_dsc, is_task_64bit, map, space, kmsg, &mr);
5130 break;
5131 case MACH_MSG_GUARDED_PORT_DESCRIPTOR:
5132 user_dsc = ipc_kmsg_copyout_guarded_port_descriptor(
5133 (mach_msg_guarded_port_descriptor_t *)&kern_dsc[i],
5134 user_dsc, is_task_64bit, kmsg, space, option, &mr);
5135 break;
5136 default:
5137 panic("untyped IPC copyout body: invalid message descriptor");
5138 }
5139 }
5140
5141 if (user_dsc != kern_dsc) {
5142 vm_offset_t dsc_adjust = (vm_offset_t)user_dsc - (vm_offset_t)kern_dsc;
5143 memmove((char *)((vm_offset_t)kmsg->ikm_header + dsc_adjust), kmsg->ikm_header, sizeof(mach_msg_base_t));
5144 kmsg->ikm_header = (mach_msg_header_t *)((vm_offset_t)kmsg->ikm_header + dsc_adjust);
5145 /* Update the message size for the smaller user representation */
5146 kmsg->ikm_header->msgh_size -= (mach_msg_size_t)dsc_adjust;
5147 }
5148
5149 return mr;
5150 }
5151
5152 /*
5153 * Routine: ipc_kmsg_copyout_size
5154 * Purpose:
5155 * Compute the size of the message as copied out to the given
5156 * map. If the destination map's pointers are a different size
5157 * than the kernel's, we have to allow for expansion/
5158 * contraction of the descriptors as appropriate.
5159 * Conditions:
5160 * Nothing locked.
5161 * Returns:
5162 * size of the message as it would be received.
5163 */
5164
5165 mach_msg_size_t
ipc_kmsg_copyout_size(ipc_kmsg_t kmsg,vm_map_t map)5166 ipc_kmsg_copyout_size(
5167 ipc_kmsg_t kmsg,
5168 vm_map_t map)
5169 {
5170 mach_msg_size_t send_size;
5171
5172 send_size = kmsg->ikm_header->msgh_size;
5173
5174 boolean_t is_task_64bit = (map->max_offset > VM_MAX_ADDRESS);
5175
5176 #if defined(__LP64__)
5177 send_size -= USER_HEADER_SIZE_DELTA;
5178 #endif
5179
5180 if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
5181 mach_msg_body_t *body;
5182 mach_msg_descriptor_t *saddr, *eaddr;
5183
5184 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
5185 saddr = (mach_msg_descriptor_t *) (body + 1);
5186 eaddr = saddr + body->msgh_descriptor_count;
5187
5188 send_size -= KERNEL_DESC_SIZE * body->msgh_descriptor_count;
5189 for (; saddr < eaddr; saddr++) {
5190 send_size += ipc_kmsg_user_desc_size(saddr->type.type,
5191 is_task_64bit);
5192 }
5193 }
5194 return send_size;
5195 }
5196
5197 /*
5198 * Routine: ipc_kmsg_copyout
5199 * Purpose:
5200 * "Copy-out" port rights and out-of-line memory
5201 * in the message.
5202 * Conditions:
5203 * Nothing locked.
5204 * Returns:
5205 * MACH_MSG_SUCCESS Copied out all rights and memory.
5206 * MACH_RCV_HEADER_ERROR + special bits
5207 * Rights and memory in the message are intact.
5208 * MACH_RCV_BODY_ERROR + special bits
5209 * The message header was successfully copied out.
5210 * As much of the body was handled as possible.
5211 */
5212
5213 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)5214 ipc_kmsg_copyout(
5215 ipc_kmsg_t kmsg,
5216 ipc_space_t space,
5217 vm_map_t map,
5218 mach_msg_body_t *slist,
5219 mach_msg_option_t option)
5220 {
5221 mach_msg_return_t mr;
5222
5223 ikm_validate_sig(kmsg);
5224
5225 mr = ipc_kmsg_copyout_header(kmsg, space, option);
5226 if (mr != MACH_MSG_SUCCESS) {
5227 return mr;
5228 }
5229
5230 if (kmsg->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
5231 mr = ipc_kmsg_copyout_body(kmsg, space, map, option, slist);
5232
5233 if (mr != MACH_MSG_SUCCESS) {
5234 mr |= MACH_RCV_BODY_ERROR;
5235 }
5236 }
5237
5238 return mr;
5239 }
5240
5241 /*
5242 * Routine: ipc_kmsg_copyout_pseudo
5243 * Purpose:
5244 * Does a pseudo-copyout of the message.
5245 * This is like a regular copyout, except
5246 * that the ports in the header are handled
5247 * as if they are in the body. They aren't reversed.
5248 *
5249 * The error codes are a combination of special bits.
5250 * The copyout proceeds despite errors.
5251 * Conditions:
5252 * Nothing locked.
5253 * Returns:
5254 * MACH_MSG_SUCCESS Successful copyout.
5255 * MACH_MSG_IPC_SPACE No room for port right in name space.
5256 * MACH_MSG_VM_SPACE No room for memory in address space.
5257 * MACH_MSG_IPC_KERNEL Resource shortage handling port right.
5258 * MACH_MSG_VM_KERNEL Resource shortage handling memory.
5259 */
5260
5261 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)5262 ipc_kmsg_copyout_pseudo(
5263 ipc_kmsg_t kmsg,
5264 ipc_space_t space,
5265 vm_map_t map,
5266 mach_msg_body_t *slist)
5267 {
5268 mach_msg_bits_t mbits = kmsg->ikm_header->msgh_bits;
5269 ipc_object_t dest = ip_to_object(kmsg->ikm_header->msgh_remote_port);
5270 ipc_object_t reply = ip_to_object(kmsg->ikm_header->msgh_local_port);
5271 ipc_object_t voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
5272 mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
5273 mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
5274 mach_msg_type_name_t voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
5275 mach_port_name_t voucher_name = kmsg->ikm_header->msgh_voucher_port;
5276 mach_port_name_t dest_name, reply_name;
5277 mach_msg_return_t mr;
5278
5279 /* Set ith_knote to ITH_KNOTE_PSEUDO */
5280 current_thread()->ith_knote = ITH_KNOTE_PSEUDO;
5281
5282 ikm_validate_sig(kmsg);
5283
5284 assert(IO_VALID(dest));
5285
5286 #if 0
5287 /*
5288 * If we did this here, it looks like we wouldn't need the undo logic
5289 * at the end of ipc_kmsg_send() in the error cases. Not sure which
5290 * would be more elegant to keep.
5291 */
5292 ipc_importance_clean(kmsg);
5293 #else
5294 /* just assert it is already clean */
5295 ipc_importance_assert_clean(kmsg);
5296 #endif
5297
5298 mr = (ipc_kmsg_copyout_object(space, dest, dest_type, NULL, NULL, &dest_name) |
5299 ipc_kmsg_copyout_object(space, reply, reply_type, NULL, NULL, &reply_name));
5300
5301 kmsg->ikm_header->msgh_bits = mbits & MACH_MSGH_BITS_USER;
5302 kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(dest_name);
5303 kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(reply_name);
5304
5305 /* restore the voucher:
5306 * If it was copied in via move-send, have to put back a voucher send right.
5307 *
5308 * If it was copied in via copy-send, the header still contains the old voucher name.
5309 * Restore the type and discard the copied-in/pre-processed voucher.
5310 */
5311 if (IO_VALID(voucher)) {
5312 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5313 if (kmsg->ikm_voucher_type == MACH_MSG_TYPE_MOVE_SEND) {
5314 mr |= ipc_kmsg_copyout_object(space, voucher, voucher_type, NULL, NULL, &voucher_name);
5315 kmsg->ikm_header->msgh_voucher_port = voucher_name;
5316 } else {
5317 assert(kmsg->ikm_voucher_type == MACH_MSG_TYPE_COPY_SEND);
5318 kmsg->ikm_header->msgh_bits = MACH_MSGH_BITS_SET(dest_type, reply_type, MACH_MSG_TYPE_COPY_SEND,
5319 MACH_MSGH_BITS_OTHER(kmsg->ikm_header->msgh_bits));
5320 ipc_object_destroy(voucher, voucher_type);
5321 }
5322 ipc_kmsg_clear_voucher_port(kmsg);
5323 }
5324
5325 if (mbits & MACH_MSGH_BITS_COMPLEX) {
5326 mr |= ipc_kmsg_copyout_body(kmsg, space, map, 0, slist);
5327 }
5328
5329 return mr;
5330 }
5331
5332 /*
5333 * Routine: ipc_kmsg_copyout_dest_to_user
5334 * Purpose:
5335 * Copies out the destination port in the message.
5336 * Destroys all other rights and memory in the message.
5337 * Conditions:
5338 * Nothing locked.
5339 */
5340
5341 void
ipc_kmsg_copyout_dest_to_user(ipc_kmsg_t kmsg,ipc_space_t space)5342 ipc_kmsg_copyout_dest_to_user(
5343 ipc_kmsg_t kmsg,
5344 ipc_space_t space)
5345 {
5346 mach_msg_bits_t mbits;
5347 ipc_object_t dest;
5348 ipc_object_t reply;
5349 ipc_object_t voucher;
5350 mach_msg_type_name_t dest_type;
5351 mach_msg_type_name_t reply_type;
5352 mach_msg_type_name_t voucher_type;
5353 mach_port_name_t dest_name, reply_name, voucher_name;
5354
5355 ikm_validate_sig(kmsg);
5356
5357 mbits = kmsg->ikm_header->msgh_bits;
5358 dest = ip_to_object(kmsg->ikm_header->msgh_remote_port);
5359 reply = ip_to_object(kmsg->ikm_header->msgh_local_port);
5360 voucher = ip_to_object(ipc_kmsg_get_voucher_port(kmsg));
5361 voucher_name = kmsg->ikm_header->msgh_voucher_port;
5362 dest_type = MACH_MSGH_BITS_REMOTE(mbits);
5363 reply_type = MACH_MSGH_BITS_LOCAL(mbits);
5364 voucher_type = MACH_MSGH_BITS_VOUCHER(mbits);
5365
5366 assert(IO_VALID(dest));
5367
5368 ipc_importance_assert_clean(kmsg);
5369
5370 io_lock(dest);
5371 if (io_active(dest)) {
5372 ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
5373 /* dest is unlocked */
5374 } else {
5375 io_unlock(dest);
5376 io_release(dest);
5377 dest_name = MACH_PORT_DEAD;
5378 }
5379
5380 if (IO_VALID(reply)) {
5381 ipc_object_destroy(reply, reply_type);
5382 reply_name = MACH_PORT_NULL;
5383 } else {
5384 reply_name = CAST_MACH_PORT_TO_NAME(reply);
5385 }
5386
5387 if (IO_VALID(voucher)) {
5388 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5389 ipc_object_destroy(voucher, voucher_type);
5390 ipc_kmsg_clear_voucher_port(kmsg);
5391 voucher_name = MACH_PORT_NULL;
5392 }
5393
5394 kmsg->ikm_header->msgh_bits = MACH_MSGH_BITS_SET(reply_type, dest_type,
5395 voucher_type, mbits);
5396 kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
5397 kmsg->ikm_header->msgh_remote_port = CAST_MACH_NAME_TO_PORT(reply_name);
5398 kmsg->ikm_header->msgh_voucher_port = voucher_name;
5399
5400 if (mbits & MACH_MSGH_BITS_COMPLEX) {
5401 mach_msg_body_t *body;
5402
5403 body = (mach_msg_body_t *) (kmsg->ikm_header + 1);
5404 ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count,
5405 (mach_msg_descriptor_t *)(body + 1));
5406 }
5407 }
5408
5409 /*
5410 * Routine: ipc_kmsg_copyout_dest_to_kernel
5411 * Purpose:
5412 * Copies out the destination and reply ports in the message.
5413 * Leaves all other rights and memory in the message alone.
5414 * Conditions:
5415 * Nothing locked.
5416 *
5417 * Derived from ipc_kmsg_copyout_dest_to_user.
5418 * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest).
5419 * We really do want to save rights and memory.
5420 */
5421
5422 void
ipc_kmsg_copyout_dest_to_kernel(ipc_kmsg_t kmsg,ipc_space_t space)5423 ipc_kmsg_copyout_dest_to_kernel(
5424 ipc_kmsg_t kmsg,
5425 ipc_space_t space)
5426 {
5427 ipc_object_t dest;
5428 mach_port_t reply;
5429 mach_msg_type_name_t dest_type;
5430 mach_msg_type_name_t reply_type;
5431 mach_port_name_t dest_name;
5432
5433 ikm_validate_sig(kmsg);
5434
5435 dest = ip_to_object(kmsg->ikm_header->msgh_remote_port);
5436 reply = kmsg->ikm_header->msgh_local_port;
5437 dest_type = MACH_MSGH_BITS_REMOTE(kmsg->ikm_header->msgh_bits);
5438 reply_type = MACH_MSGH_BITS_LOCAL(kmsg->ikm_header->msgh_bits);
5439
5440 assert(IO_VALID(dest));
5441
5442 io_lock(dest);
5443 if (io_active(dest)) {
5444 ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
5445 /* dest is unlocked */
5446 } else {
5447 io_unlock(dest);
5448 io_release(dest);
5449 dest_name = MACH_PORT_DEAD;
5450 }
5451
5452 /*
5453 * While MIG kernel users don't receive vouchers, the
5454 * msgh_voucher_port field is intended to be round-tripped through the
5455 * kernel if there is no voucher disposition set. Here we check for a
5456 * non-zero voucher disposition, and consume the voucher send right as
5457 * there is no possible way to specify MACH_RCV_VOUCHER semantics.
5458 */
5459 mach_msg_type_name_t voucher_type;
5460 voucher_type = MACH_MSGH_BITS_VOUCHER(kmsg->ikm_header->msgh_bits);
5461 if (voucher_type != MACH_MSGH_BITS_ZERO) {
5462 ipc_port_t voucher = ipc_kmsg_get_voucher_port(kmsg);
5463
5464 assert(voucher_type == MACH_MSG_TYPE_MOVE_SEND);
5465 /*
5466 * someone managed to send this kernel routine a message with
5467 * a voucher in it. Cleanup the reference in
5468 * kmsg->ikm_voucher.
5469 */
5470 if (IP_VALID(voucher)) {
5471 ipc_port_release_send(voucher);
5472 }
5473 kmsg->ikm_header->msgh_voucher_port = 0;
5474 ipc_kmsg_clear_voucher_port(kmsg);
5475 }
5476
5477 kmsg->ikm_header->msgh_bits =
5478 (MACH_MSGH_BITS_OTHER(kmsg->ikm_header->msgh_bits) |
5479 MACH_MSGH_BITS(reply_type, dest_type));
5480 kmsg->ikm_header->msgh_local_port = CAST_MACH_NAME_TO_PORT(dest_name);
5481 kmsg->ikm_header->msgh_remote_port = reply;
5482 }
5483
5484 /*
5485 * Caller has a reference to the kmsg and the mqueue lock held.
5486 *
5487 * As such, we can safely return a pointer to the thread group in the kmsg and
5488 * not an additional reference. It is up to the caller to decide to take an
5489 * additional reference on the thread group while still holding the mqueue lock,
5490 * if needed.
5491 */
5492 #if CONFIG_PREADOPT_TG
5493 struct thread_group *
ipc_kmsg_get_thread_group(ipc_kmsg_t kmsg)5494 ipc_kmsg_get_thread_group(ipc_kmsg_t kmsg)
5495 {
5496 struct thread_group *tg = NULL;
5497 kern_return_t __assert_only kr;
5498
5499 ipc_voucher_t voucher = convert_port_to_voucher(ipc_kmsg_get_voucher_port(kmsg));
5500 kr = bank_get_preadopt_thread_group(voucher, &tg);
5501 ipc_voucher_release(voucher);
5502
5503 return tg;
5504 }
5505 #endif
5506
5507 #ifdef __arm64__
5508 /*
5509 * Just sets those parts of the trailer that aren't set up at allocation time.
5510 */
5511 static void
ipc_kmsg_munge_trailer(mach_msg_max_trailer_t * in,void * _out,boolean_t is64bit)5512 ipc_kmsg_munge_trailer(mach_msg_max_trailer_t *in, void *_out, boolean_t is64bit)
5513 {
5514 if (is64bit) {
5515 mach_msg_max_trailer64_t *out = (mach_msg_max_trailer64_t*)_out;
5516 out->msgh_seqno = in->msgh_seqno;
5517 out->msgh_context = in->msgh_context;
5518 out->msgh_trailer_size = in->msgh_trailer_size;
5519 out->msgh_ad = in->msgh_ad;
5520 } else {
5521 mach_msg_max_trailer32_t *out = (mach_msg_max_trailer32_t*)_out;
5522 out->msgh_seqno = in->msgh_seqno;
5523 out->msgh_context = (mach_port_context32_t)in->msgh_context;
5524 out->msgh_trailer_size = in->msgh_trailer_size;
5525 out->msgh_ad = in->msgh_ad;
5526 }
5527 }
5528 #endif /* __arm64__ */
5529
5530 mach_msg_trailer_size_t
ipc_kmsg_trailer_size(mach_msg_option_t option,__unused thread_t thread)5531 ipc_kmsg_trailer_size(
5532 mach_msg_option_t option,
5533 __unused thread_t thread)
5534 {
5535 if (!(option & MACH_RCV_TRAILER_MASK)) {
5536 return MACH_MSG_TRAILER_MINIMUM_SIZE;
5537 } else {
5538 return REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread), option);
5539 }
5540 }
5541
5542 /*
5543 * Routine: ipc_kmsg_init_trailer
5544 * Purpose:
5545 * Initiailizes a trailer in a message safely.
5546 */
5547 void
ipc_kmsg_init_trailer(ipc_kmsg_t kmsg,mach_msg_size_t size,task_t sender)5548 ipc_kmsg_init_trailer(
5549 ipc_kmsg_t kmsg,
5550 mach_msg_size_t size,
5551 task_t sender)
5552 {
5553 static const mach_msg_max_trailer_t KERNEL_TRAILER_TEMPLATE = {
5554 .msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0,
5555 .msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE,
5556 .msgh_sender = KERNEL_SECURITY_TOKEN_VALUE,
5557 .msgh_audit = KERNEL_AUDIT_TOKEN_VALUE
5558 };
5559
5560 mach_msg_max_trailer_t *trailer;
5561
5562 /*
5563 * I reserve for the trailer the largest space (MAX_TRAILER_SIZE)
5564 * However, the internal size field of the trailer (msgh_trailer_size)
5565 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
5566 * the cases where no implicit data is requested.
5567 */
5568 trailer = (mach_msg_max_trailer_t *)((vm_offset_t)kmsg->ikm_header + size);
5569 if (sender == TASK_NULL) {
5570 memcpy(trailer, &KERNEL_TRAILER_TEMPLATE, sizeof(*trailer));
5571 } else {
5572 bzero(trailer, sizeof(*trailer));
5573 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
5574 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
5575 trailer->msgh_sender = *task_get_sec_token(sender);
5576 trailer->msgh_audit = *task_get_audit_token(sender);
5577 }
5578 }
5579
5580
5581 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)5582 ipc_kmsg_add_trailer(ipc_kmsg_t kmsg, ipc_space_t space __unused,
5583 mach_msg_option_t option, __unused thread_t thread,
5584 mach_port_seqno_t seqno, boolean_t minimal_trailer,
5585 mach_vm_offset_t context)
5586 {
5587 mach_msg_max_trailer_t *trailer;
5588
5589 #ifdef __arm64__
5590 mach_msg_max_trailer_t tmp_trailer; /* This accommodates U64, and we'll munge */
5591 void *real_trailer_out = (void*)(mach_msg_max_trailer_t *)
5592 ((vm_offset_t)kmsg->ikm_header +
5593 mach_round_msg(kmsg->ikm_header->msgh_size));
5594
5595 /*
5596 * Populate scratch with initial values set up at message allocation time.
5597 * After, we reinterpret the space in the message as the right type
5598 * of trailer for the address space in question.
5599 */
5600 bcopy(real_trailer_out, &tmp_trailer, MAX_TRAILER_SIZE);
5601 trailer = &tmp_trailer;
5602 #else /* __arm64__ */
5603 (void)thread;
5604 trailer = (mach_msg_max_trailer_t *)
5605 ((vm_offset_t)kmsg->ikm_header +
5606 mach_round_msg(kmsg->ikm_header->msgh_size));
5607 #endif /* __arm64__ */
5608
5609 if (!(option & MACH_RCV_TRAILER_MASK)) {
5610 return;
5611 }
5612
5613 trailer->msgh_seqno = seqno;
5614 trailer->msgh_context = context;
5615 trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread), option);
5616
5617 if (minimal_trailer) {
5618 goto done;
5619 }
5620
5621 if (GET_RCV_ELEMENTS(option) >= MACH_RCV_TRAILER_AV) {
5622 trailer->msgh_ad = 0;
5623 }
5624
5625 /*
5626 * The ipc_kmsg_t holds a reference to the label of a label
5627 * handle, not the port. We must get a reference to the port
5628 * and a send right to copyout to the receiver.
5629 */
5630
5631 if (option & MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_LABELS)) {
5632 trailer->msgh_labels.sender = 0;
5633 }
5634
5635 done:
5636 #ifdef __arm64__
5637 ipc_kmsg_munge_trailer(trailer, real_trailer_out, thread_is_64bit_addr(thread));
5638 #endif /* __arm64__ */
5639 return;
5640 }
5641
5642 void
ipc_kmsg_set_voucher_port(ipc_kmsg_t kmsg,ipc_port_t voucher_port,mach_msg_type_name_t type)5643 ipc_kmsg_set_voucher_port(
5644 ipc_kmsg_t kmsg,
5645 ipc_port_t voucher_port,
5646 mach_msg_type_name_t type)
5647 {
5648 if (IP_VALID(voucher_port)) {
5649 assert(ip_kotype(voucher_port) == IKOT_VOUCHER);
5650 }
5651 kmsg->ikm_voucher_port = voucher_port;
5652 kmsg->ikm_voucher_type = type;
5653 }
5654
5655 ipc_port_t
ipc_kmsg_get_voucher_port(ipc_kmsg_t kmsg)5656 ipc_kmsg_get_voucher_port(ipc_kmsg_t kmsg)
5657 {
5658 return kmsg->ikm_voucher_port;
5659 }
5660
5661 void
ipc_kmsg_clear_voucher_port(ipc_kmsg_t kmsg)5662 ipc_kmsg_clear_voucher_port(ipc_kmsg_t kmsg)
5663 {
5664 kmsg->ikm_voucher_port = IP_NULL;
5665 kmsg->ikm_voucher_type = MACH_MSGH_BITS_ZERO;
5666 }
5667
5668 mach_msg_header_t *
ipc_kmsg_msg_header(ipc_kmsg_t kmsg)5669 ipc_kmsg_msg_header(ipc_kmsg_t kmsg)
5670 {
5671 if (NULL == kmsg) {
5672 return NULL;
5673 }
5674 return kmsg->ikm_header;
5675 }
5676