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