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