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