]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_kobject.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_kobject.c
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 Software.Distribution@CS.CMU.EDU
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: kern/ipc_kobject.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Functions for letting a port represent a kernel object.
71 */
72
73 #include <mach_debug.h>
74 #include <mach_ipc_test.h>
75 #include <mach/mig.h>
76 #include <mach/port.h>
77 #include <mach/kern_return.h>
78 #include <mach/message.h>
79 #include <mach/mig_errors.h>
80 #include <mach/notify.h>
81 #include <mach/ndr.h>
82 #include <mach/vm_param.h>
83
84 #include <mach/mach_vm_server.h>
85 #include <mach/mach_port_server.h>
86 #include <mach/mach_host_server.h>
87 #include <mach/host_priv_server.h>
88 #include <mach/host_security_server.h>
89 #include <mach/clock_server.h>
90 #include <mach/clock_priv_server.h>
91 #include <mach/lock_set_server.h>
92 #include <mach/memory_entry_server.h>
93 #include <mach/memory_object_control_server.h>
94 #include <mach/memory_object_default_server.h>
95 #include <mach/processor_server.h>
96 #include <mach/processor_set_server.h>
97 #include <mach/task_server.h>
98 #include <mach/mach_voucher_server.h>
99 #include <mach/mach_voucher_attr_control_server.h>
100 #ifdef VM32_SUPPORT
101 #include <mach/vm32_map_server.h>
102 #endif
103 #include <mach/thread_act_server.h>
104 #include <mach/restartable_server.h>
105
106 #include <mach/exc_server.h>
107 #include <mach/mach_exc_server.h>
108 #include <mach/mach_eventlink_server.h>
109
110 #include <device/device_types.h>
111 #include <device/device_server.h>
112
113 #if CONFIG_USER_NOTIFICATION
114 #include <UserNotification/UNDReplyServer.h>
115 #endif
116
117 #if CONFIG_ARCADE
118 #include <mach/arcade_register_server.h>
119 #endif
120
121 #if CONFIG_AUDIT
122 #include <kern/audit_sessionport.h>
123 #endif
124
125 #if MACH_MACHINE_ROUTINES
126 #include <machine/machine_routines.h>
127 #endif /* MACH_MACHINE_ROUTINES */
128 #if XK_PROXY
129 #include <uk_xkern/xk_uproxy_server.h>
130 #endif /* XK_PROXY */
131
132 #include <kern/counter.h>
133 #include <kern/ipc_tt.h>
134 #include <kern/ipc_mig.h>
135 #include <kern/ipc_misc.h>
136 #include <kern/ipc_kobject.h>
137 #include <kern/host_notify.h>
138 #include <kern/mk_timer.h>
139 #include <kern/misc_protos.h>
140
141 #if CONFIG_ARCADE
142 #include <kern/arcade.h>
143 #endif /* CONFIG_ARCADE */
144
145 #include <ipc/ipc_kmsg.h>
146 #include <ipc/ipc_port.h>
147 #include <ipc/ipc_voucher.h>
148 #include <kern/sync_sema.h>
149 #include <kern/work_interval.h>
150 #include <kern/suid_cred.h>
151 #include <kern/task_ident.h>
152
153 #if HYPERVISOR
154 #include <kern/hv_support.h>
155 #endif
156
157 #include <vm/vm_protos.h>
158
159 #include <security/mac_mach_internal.h>
160
161 extern char *proc_name_address(void *p);
162 struct proc;
163 extern int proc_pid(struct proc *p);
164
165 /*
166 * Routine: ipc_kobject_notify
167 * Purpose:
168 * Deliver notifications to kobjects that care about them.
169 */
170 boolean_t
171 ipc_kobject_notify(
172 mach_msg_header_t *request_header,
173 mach_msg_header_t *reply_header);
174
175 typedef struct {
176 mach_msg_id_t num;
177 mig_routine_t routine;
178 int size;
179 int kobjidx;
180 } mig_hash_t;
181
182 #define MAX_MIG_ENTRIES 1031
183 #define MIG_HASH(x) (x)
184
185 #define KOBJ_IDX_NOT_SET (-1)
186
187 #ifndef max
188 #define max(a, b) (((a) > (b)) ? (a) : (b))
189 #endif /* max */
190
191 static SECURITY_READ_ONLY_LATE(mig_hash_t) mig_buckets[MAX_MIG_ENTRIES];
192 static SECURITY_READ_ONLY_LATE(int) mig_table_max_displ;
193 SECURITY_READ_ONLY_LATE(int) mach_kobj_count; /* count of total number of kobjects */
194
195 static ZONE_DECLARE(ipc_kobject_label_zone, "ipc kobject labels",
196 sizeof(struct ipc_kobject_label), ZC_NONE);
197
198 __startup_data
199 static const struct mig_subsystem *mig_e[] = {
200 (const struct mig_subsystem *)&mach_vm_subsystem,
201 (const struct mig_subsystem *)&mach_port_subsystem,
202 (const struct mig_subsystem *)&mach_host_subsystem,
203 (const struct mig_subsystem *)&host_priv_subsystem,
204 (const struct mig_subsystem *)&host_security_subsystem,
205 (const struct mig_subsystem *)&clock_subsystem,
206 (const struct mig_subsystem *)&clock_priv_subsystem,
207 (const struct mig_subsystem *)&processor_subsystem,
208 (const struct mig_subsystem *)&processor_set_subsystem,
209 (const struct mig_subsystem *)&is_iokit_subsystem,
210 (const struct mig_subsystem *)&lock_set_subsystem,
211 (const struct mig_subsystem *)&task_subsystem,
212 (const struct mig_subsystem *)&thread_act_subsystem,
213 #ifdef VM32_SUPPORT
214 (const struct mig_subsystem *)&vm32_map_subsystem,
215 #endif
216 #if CONFIG_USER_NOTIFICATION
217 (const struct mig_subsystem *)&UNDReply_subsystem,
218 #endif
219 (const struct mig_subsystem *)&mach_voucher_subsystem,
220 (const struct mig_subsystem *)&mach_voucher_attr_control_subsystem,
221 (const struct mig_subsystem *)&memory_entry_subsystem,
222 (const struct mig_subsystem *)&task_restartable_subsystem,
223
224 #if XK_PROXY
225 (const struct mig_subsystem *)&do_uproxy_xk_uproxy_subsystem,
226 #endif /* XK_PROXY */
227 #if MACH_MACHINE_ROUTINES
228 (const struct mig_subsystem *)&MACHINE_SUBSYSTEM,
229 #endif /* MACH_MACHINE_ROUTINES */
230 #if MCMSG && iPSC860
231 (const struct mig_subsystem *)&mcmsg_info_subsystem,
232 #endif /* MCMSG && iPSC860 */
233 (const struct mig_subsystem *)&catch_exc_subsystem,
234 (const struct mig_subsystem *)&catch_mach_exc_subsystem,
235 #if CONFIG_ARCADE
236 (const struct mig_subsystem *)&arcade_register_subsystem,
237 #endif
238 (const struct mig_subsystem *)&mach_eventlink_subsystem,
239 };
240
241 static void
242 mig_init(void)
243 {
244 unsigned int i, n = sizeof(mig_e) / sizeof(const struct mig_subsystem *);
245 int howmany;
246 mach_msg_id_t j, pos, nentry, range;
247
248 for (i = 0; i < n; i++) {
249 range = mig_e[i]->end - mig_e[i]->start;
250 if (!mig_e[i]->start || range < 0) {
251 panic("the msgh_ids in mig_e[] aren't valid!");
252 }
253
254 for (j = 0; j < range; j++) {
255 if (mig_e[i]->routine[j].stub_routine) {
256 /* Only put real entries in the table */
257 nentry = j + mig_e[i]->start;
258 for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1;
259 mig_buckets[pos].num;
260 pos++, pos = pos % MAX_MIG_ENTRIES, howmany++) {
261 if (mig_buckets[pos].num == nentry) {
262 printf("message id = %d\n", nentry);
263 panic("multiple entries with the same msgh_id");
264 }
265 if (howmany == MAX_MIG_ENTRIES) {
266 panic("the mig dispatch table is too small");
267 }
268 }
269
270 mig_buckets[pos].num = nentry;
271 mig_buckets[pos].routine = mig_e[i]->routine[j].stub_routine;
272 if (mig_e[i]->routine[j].max_reply_msg) {
273 mig_buckets[pos].size = mig_e[i]->routine[j].max_reply_msg;
274 } else {
275 mig_buckets[pos].size = mig_e[i]->maxsize;
276 }
277 mig_buckets[pos].kobjidx = KOBJ_IDX_NOT_SET;
278
279 mig_table_max_displ = max(howmany, mig_table_max_displ);
280 mach_kobj_count++;
281 }
282 }
283 }
284 printf("mig_table_max_displ = %d mach_kobj_count = %d\n",
285 mig_table_max_displ, mach_kobj_count);
286 }
287 STARTUP(MACH_IPC, STARTUP_RANK_FIRST, mig_init);
288
289 /*
290 * Do a hash table lookup for given msgh_id. Return 0
291 * if not found.
292 */
293 static mig_hash_t *
294 find_mig_hash_entry(int msgh_id)
295 {
296 unsigned int i = (unsigned int)MIG_HASH(msgh_id);
297 int max_iter = mig_table_max_displ;
298 mig_hash_t *ptr;
299
300 do {
301 ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES];
302 } while (msgh_id != ptr->num && ptr->num && --max_iter);
303
304 if (!ptr->routine || msgh_id != ptr->num) {
305 ptr = (mig_hash_t *)0;
306 }
307
308 return ptr;
309 }
310
311 /*
312 * Routine: ipc_kobject_set_kobjidx
313 * Purpose:
314 * Set the index for the kobject filter
315 * mask for a given message ID.
316 */
317 kern_return_t
318 ipc_kobject_set_kobjidx(
319 int msgh_id,
320 int index)
321 {
322 mig_hash_t *ptr = find_mig_hash_entry(msgh_id);
323
324 if (ptr == (mig_hash_t *)0) {
325 return KERN_INVALID_ARGUMENT;
326 }
327
328 assert(index < mach_kobj_count);
329 ptr->kobjidx = index;
330
331 return KERN_SUCCESS;
332 }
333
334 /*
335 * Routine: ipc_kobject_server
336 * Purpose:
337 * Handle a message sent to the kernel.
338 * Generates a reply message.
339 * Version for Untyped IPC.
340 * Conditions:
341 * Nothing locked.
342 */
343
344 ipc_kmsg_t
345 ipc_kobject_server(
346 ipc_kmsg_t request,
347 mach_msg_option_t __unused option)
348 {
349 mach_msg_size_t reply_size;
350 ipc_kmsg_t reply;
351 kern_return_t kr;
352 ipc_port_t replyp = IPC_PORT_NULL;
353 mach_msg_max_trailer_t *trailer;
354 mig_hash_t *ptr;
355 task_t task = TASK_NULL;
356 uint32_t exec_token;
357 boolean_t exec_token_changed = FALSE;
358 int request_msgh_id = request->ikm_header->msgh_id;
359 natural_t ikot;
360 ipc_port_t port;
361
362 reply = NULL;
363 port = request->ikm_header->msgh_remote_port;
364 if (IP_VALID(port)) {
365 ikot = ip_kotype(port);
366 } else {
367 ikot = IKOT_UNKNOWN;
368 }
369 if (ikot == IKOT_UEXT_OBJECT) {
370 kr = uext_server(request, &reply);
371 if ((MIG_NO_REPLY == kr) || (KERN_SUCCESS == kr)) {
372 ipc_kmsg_trace_send(request, option);
373 goto msgdone;
374 }
375 }
376
377 /* Find corresponding mig_hash entry, if any */
378 ptr = find_mig_hash_entry(request_msgh_id);
379
380 /* Get the reply_size. */
381 if (ptr == (mig_hash_t *)0) {
382 reply_size = sizeof(mig_reply_error_t);
383 } else {
384 reply_size = ptr->size;
385 }
386
387 /* round up for trailer size */
388 reply_size += MAX_TRAILER_SIZE;
389 reply = ipc_kmsg_alloc(reply_size);
390
391 if (reply == IKM_NULL) {
392 printf("ipc_kobject_server: dropping request\n");
393 ipc_kmsg_trace_send(request, option);
394 ipc_kmsg_destroy(request);
395 return IKM_NULL;
396 }
397
398 /*
399 * Initialize reply message.
400 */
401 {
402 #define InP ((mach_msg_header_t *) request->ikm_header)
403 #define OutP ((mig_reply_error_t *) reply->ikm_header)
404
405 /*
406 * MIG should really assure no data leakage -
407 * but until it does, pessimistically zero the
408 * whole reply buffer.
409 */
410 bzero((void *)OutP, reply_size);
411
412 OutP->NDR = NDR_record;
413 OutP->Head.msgh_size = sizeof(mig_reply_error_t);
414
415 OutP->Head.msgh_bits =
416 MACH_MSGH_BITS_SET(MACH_MSGH_BITS_LOCAL(InP->msgh_bits), 0, 0, 0);
417 OutP->Head.msgh_remote_port = InP->msgh_local_port;
418 OutP->Head.msgh_local_port = MACH_PORT_NULL;
419 OutP->Head.msgh_voucher_port = MACH_PORT_NULL;
420 OutP->Head.msgh_id = InP->msgh_id + 100;
421
422 #undef InP
423 #undef OutP
424 }
425
426 /*
427 * Find the routine to call, and call it
428 * to perform the kernel function
429 */
430 ipc_kmsg_trace_send(request, option);
431 {
432 if (ptr) {
433 /*
434 * Check if the port is a task port, if its a task port then
435 * snapshot the task exec token before the mig routine call.
436 */
437 if (ikot == IKOT_TASK_CONTROL) {
438 task = convert_port_to_task_with_exec_token(port, &exec_token, TRUE);
439 }
440
441 #if CONFIG_MACF
442 int idx = ptr->kobjidx;
443 task_t curtask = current_task();
444 uint8_t *filter_mask = curtask->mach_kobj_filter_mask;
445
446 /* Check kobject mig filter mask, if exists. */
447 if (__improbable(filter_mask != NULL && idx != KOBJ_IDX_NOT_SET &&
448 !bitstr_test(filter_mask, idx))) {
449 /* Not in filter mask, evaluate policy. */
450 if (mac_task_kobj_msg_evaluate != NULL) {
451 kr = mac_task_kobj_msg_evaluate(get_bsdtask_info(curtask),
452 request_msgh_id, idx);
453 if (kr != KERN_SUCCESS) {
454 ((mig_reply_error_t *) reply->ikm_header)->RetCode = kr;
455 goto skip_kobjcall;
456 }
457 }
458 }
459 #endif /* CONFIG_MACF */
460
461 (*ptr->routine)(request->ikm_header, reply->ikm_header);
462
463 #if CONFIG_MACF
464 skip_kobjcall:
465 #endif
466
467 /* Check if the exec token changed during the mig routine */
468 if (task != TASK_NULL) {
469 if (exec_token != task->exec_token) {
470 exec_token_changed = TRUE;
471 }
472 task_deallocate(task);
473 }
474
475 kernel_task->messages_received++;
476 } else {
477 if (!ipc_kobject_notify(request->ikm_header, reply->ikm_header)) {
478 #if DEVELOPMENT || DEBUG
479 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
480 request->ikm_header->msgh_id);
481 #endif /* DEVELOPMENT || DEBUG */
482 _MIG_MSGID_INVALID(request->ikm_header->msgh_id);
483
484 ((mig_reply_error_t *) reply->ikm_header)->RetCode
485 = MIG_BAD_ID;
486 } else {
487 kernel_task->messages_received++;
488 }
489 }
490 kernel_task->messages_sent++;
491 }
492
493 if (!(reply->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
494 ((mig_reply_error_t *) reply->ikm_header)->RetCode != KERN_SUCCESS) {
495 kr = ((mig_reply_error_t *) reply->ikm_header)->RetCode;
496 } else {
497 kr = KERN_SUCCESS;
498 }
499
500 msgdone:
501 /*
502 * Destroy destination. The following code differs from
503 * ipc_object_destroy in that we release the send-once
504 * right instead of generating a send-once notification
505 * (which would bring us here again, creating a loop).
506 * It also differs in that we only expect send or
507 * send-once rights, never receive rights.
508 *
509 * We set msgh_remote_port to IP_NULL so that the kmsg
510 * destroy routines don't try to destroy the port twice.
511 */
512 switch (MACH_MSGH_BITS_REMOTE(request->ikm_header->msgh_bits)) {
513 case MACH_MSG_TYPE_PORT_SEND:
514 ipc_port_release_send(request->ikm_header->msgh_remote_port);
515 request->ikm_header->msgh_remote_port = IP_NULL;
516 break;
517
518 case MACH_MSG_TYPE_PORT_SEND_ONCE:
519 ipc_port_release_sonce(request->ikm_header->msgh_remote_port);
520 request->ikm_header->msgh_remote_port = IP_NULL;
521 break;
522
523 default:
524 panic("ipc_kobject_server: strange destination rights");
525 }
526
527 /*
528 * Destroy voucher. The kernel MIG servers never take ownership
529 * of vouchers sent in messages. Swallow any such rights here.
530 */
531 if (IP_VALID(request->ikm_voucher)) {
532 assert(MACH_MSG_TYPE_PORT_SEND ==
533 MACH_MSGH_BITS_VOUCHER(request->ikm_header->msgh_bits));
534 ipc_port_release_send(request->ikm_voucher);
535 request->ikm_voucher = IP_NULL;
536 }
537
538 if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) {
539 /*
540 * The server function is responsible for the contents
541 * of the message. The reply port right is moved
542 * to the reply message, and we have deallocated
543 * the destination port right, so we just need
544 * to free the kmsg.
545 */
546 ipc_kmsg_free(request);
547 } else {
548 /*
549 * The message contents of the request are intact.
550 * Destroy everthing except the reply port right,
551 * which is needed in the reply message.
552 */
553 request->ikm_header->msgh_local_port = MACH_PORT_NULL;
554 ipc_kmsg_destroy(request);
555 }
556
557 if (kr == MIG_NO_REPLY) {
558 /*
559 * The server function will send a reply message
560 * using the reply port right, which it has saved.
561 */
562
563 if (reply) {
564 ipc_kmsg_free(reply);
565 }
566 return IKM_NULL;
567 }
568
569 if (reply) {
570 replyp = reply->ikm_header->msgh_remote_port;
571 }
572
573 if (!IP_VALID(replyp)) {
574 /*
575 * Can't queue the reply message if the destination
576 * (the reply port) isn't valid.
577 */
578
579 ipc_kmsg_destroy(reply);
580
581 return IKM_NULL;
582 } else if (replyp->ip_receiver == ipc_space_kernel) {
583 /*
584 * Don't send replies to kobject kernel ports
585 */
586 #if DEVELOPMENT || DEBUG
587 printf("%s: refusing to send reply to kobject %d port (id:%d)\n",
588 __func__, ip_kotype(replyp), request_msgh_id);
589 #endif /* DEVELOPMENT || DEBUG */
590 ipc_kmsg_destroy(reply);
591 return IKM_NULL;
592 }
593
594 /* Fail the MIG call if the task exec token changed during the call */
595 if (kr == KERN_SUCCESS && exec_token_changed) {
596 /*
597 * Create a new reply msg with error and destroy the old reply msg.
598 */
599 ipc_kmsg_t new_reply = ipc_kmsg_alloc(reply_size);
600
601 if (new_reply == IKM_NULL) {
602 printf("ipc_kobject_server: dropping request\n");
603 ipc_kmsg_destroy(reply);
604 return IKM_NULL;
605 }
606 /*
607 * Initialize the new reply message.
608 */
609 {
610 #define OutP_new ((mig_reply_error_t *) new_reply->ikm_header)
611 #define OutP_old ((mig_reply_error_t *) reply->ikm_header)
612
613 bzero((void *)OutP_new, reply_size);
614
615 OutP_new->NDR = OutP_old->NDR;
616 OutP_new->Head.msgh_size = sizeof(mig_reply_error_t);
617 OutP_new->Head.msgh_bits = OutP_old->Head.msgh_bits & ~MACH_MSGH_BITS_COMPLEX;
618 OutP_new->Head.msgh_remote_port = OutP_old->Head.msgh_remote_port;
619 OutP_new->Head.msgh_local_port = MACH_PORT_NULL;
620 OutP_new->Head.msgh_voucher_port = MACH_PORT_NULL;
621 OutP_new->Head.msgh_id = OutP_old->Head.msgh_id;
622
623 /* Set the error as KERN_INVALID_TASK */
624 OutP_new->RetCode = KERN_INVALID_TASK;
625
626 #undef OutP_new
627 #undef OutP_old
628 }
629
630 /*
631 * Destroy everything in reply except the reply port right,
632 * which is needed in the new reply message.
633 */
634 reply->ikm_header->msgh_remote_port = MACH_PORT_NULL;
635 ipc_kmsg_destroy(reply);
636
637 reply = new_reply;
638 }
639
640 trailer = (mach_msg_max_trailer_t *)
641 ((vm_offset_t)reply->ikm_header + (int)reply->ikm_header->msgh_size);
642 bzero(trailer, sizeof(*trailer));
643 trailer->msgh_sender = KERNEL_SECURITY_TOKEN;
644 trailer->msgh_audit = KERNEL_AUDIT_TOKEN;
645 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
646 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
647
648 return reply;
649 }
650
651 /*
652 * Routine: ipc_kobject_set
653 * Purpose:
654 * Make a port represent a kernel object of the given type.
655 * The caller is responsible for handling refs for the
656 * kernel object, if necessary.
657 * Conditions:
658 * Nothing locked. The port must be active if setting
659 * a kobject linkage. Clearing a linkage is OK on an
660 * inactive port.
661 */
662 void
663 ipc_kobject_set(
664 ipc_port_t port,
665 ipc_kobject_t kobject,
666 ipc_kobject_type_t type)
667 {
668 ip_lock(port);
669 ipc_kobject_set_atomically(port, kobject, type);
670 ip_unlock(port);
671 }
672
673 void
674 ipc_kobject_set_atomically(
675 ipc_port_t port,
676 ipc_kobject_t kobject,
677 ipc_kobject_type_t type)
678 {
679 assert(type == IKOT_NONE || ip_active(port));
680 #if MACH_ASSERT
681 port->ip_spares[2] = (port->ip_object.io_bits & IO_BITS_KOTYPE);
682 #endif /* MACH_ASSERT */
683 port->ip_object.io_bits = (port->ip_object.io_bits & ~IO_BITS_KOTYPE) | type;
684 if (ip_is_kolabeled(port)) {
685 ipc_kobject_label_t labelp = port->ip_kolabel;
686 labelp->ikol_kobject = kobject;
687 } else {
688 port->ip_kobject = kobject;
689 }
690 if (type != IKOT_NONE) {
691 /* Once set, this bit can never be unset */
692 port->ip_object.io_bits |= IO_BITS_KOBJECT;
693 }
694 }
695
696 /*
697 * Routine: ipc_kobject_init_port
698 * Purpose:
699 * Initialize a kobject port with the given types and options.
700 *
701 * This function never fails.
702 */
703 static inline void
704 ipc_kobject_init_port(
705 ipc_port_t port,
706 ipc_kobject_t kobject,
707 ipc_kobject_type_t type,
708 ipc_kobject_alloc_options_t options)
709 {
710 ipc_kobject_set_atomically(port, kobject, type);
711
712 if (options & IPC_KOBJECT_ALLOC_MAKE_SEND) {
713 ipc_port_make_send_locked(port);
714 }
715 if (options & IPC_KOBJECT_ALLOC_NSREQUEST) {
716 ipc_port_make_sonce_locked(port);
717 port->ip_nsrequest = port;
718 }
719 if (options & IPC_KOBJECT_ALLOC_NO_GRANT) {
720 port->ip_no_grant = 1;
721 }
722 if (options & IPC_KOBJECT_ALLOC_IMMOVABLE_SEND) {
723 port->ip_immovable_send = 1;
724 }
725 if (options & IPC_KOBJECT_ALLOC_PINNED) {
726 port->ip_pinned = 1;
727 }
728 }
729
730 /*
731 * Routine: ipc_kobject_alloc_port
732 * Purpose:
733 * Allocate a kobject port in the kernel space of the specified type.
734 *
735 * This function never fails.
736 *
737 * Conditions:
738 * No locks held (memory is allocated)
739 */
740 ipc_port_t
741 ipc_kobject_alloc_port(
742 ipc_kobject_t kobject,
743 ipc_kobject_type_t type,
744 ipc_kobject_alloc_options_t options)
745 {
746 ipc_port_t port = ipc_port_alloc_kernel();
747
748 if (port == IP_NULL) {
749 panic("ipc_kobject_alloc_port(): failed to allocate port");
750 }
751
752 ipc_kobject_init_port(port, kobject, type, options);
753 return port;
754 }
755
756 /*
757 * Routine: ipc_kobject_alloc_labeled_port
758 * Purpose:
759 * Allocate a kobject port and associated mandatory access label
760 * in the kernel space of the specified type.
761 *
762 * This function never fails.
763 *
764 * Conditions:
765 * No locks held (memory is allocated)
766 */
767
768 ipc_port_t
769 ipc_kobject_alloc_labeled_port(
770 ipc_kobject_t kobject,
771 ipc_kobject_type_t type,
772 ipc_label_t label,
773 ipc_kobject_alloc_options_t options)
774 {
775 ipc_port_t port;
776 ipc_kobject_label_t labelp;
777
778 port = ipc_port_alloc_kernel();
779 if (port == IP_NULL) {
780 panic("ipc_kobject_alloc_port(): failed to allocate port");
781 }
782
783 labelp = (ipc_kobject_label_t)zalloc(ipc_kobject_label_zone);
784 if (labelp == NULL) {
785 panic("ipc_kobject_alloc_labeled_port(): failed to allocate label");
786 }
787 labelp->ikol_label = label;
788 port->ip_kolabel = labelp;
789 port->ip_object.io_bits |= IO_BITS_KOLABEL;
790
791 ipc_kobject_init_port(port, kobject, type, options);
792 return port;
793 }
794
795 static void
796 ipc_kobject_subst_once_notify(mach_msg_header_t *msg)
797 {
798 mach_no_senders_notification_t *notification = (void *)msg;
799 ipc_port_t port = notification->not_header.msgh_remote_port;
800
801 require_ip_active(port);
802 assert(IKOT_PORT_SUBST_ONCE == ip_kotype(port));
803
804 ip_release((ipc_port_t)ip_get_kobject(port));
805 ipc_port_dealloc_kernel(port);
806 }
807
808 /*
809 * Routine: ipc_kobject_alloc_subst_once
810 * Purpose:
811 * Make a port that will be substituted by the kolabel
812 * rules once, preventing the next substitution (of its target)
813 * to happen if any.
814 *
815 * Returns:
816 * A port with a send right, that will substitute to its "kobject".
817 *
818 * Conditions:
819 * No locks held (memory is allocated)
820 * `target` has a refcount that this function consumes
821 */
822 ipc_port_t
823 ipc_kobject_alloc_subst_once(
824 ipc_port_t target)
825 {
826 return ipc_kobject_alloc_labeled_port(target,
827 IKOT_PORT_SUBST_ONCE, IPC_LABEL_SUBST_ONCE,
828 IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST);
829 }
830
831 /*
832 * Routine: ipc_kobject_make_send_lazy_alloc_port
833 * Purpose:
834 * Make a send once for a kobject port.
835 *
836 * A location owning this port is passed in port_store.
837 * If no port exists, a port is made lazily.
838 *
839 * A send right is made for the port, and if this is the first one
840 * (possibly not for the first time), then the no-more-senders
841 * notification is rearmed.
842 *
843 * When a notification is armed, the kobject must donate
844 * one of its references to the port. It is expected
845 * the no-more-senders notification will consume this reference.
846 *
847 * Returns:
848 * TRUE if a notification was armed
849 * FALSE else
850 *
851 * Conditions:
852 * Nothing is locked, memory can be allocated.
853 * The caller must be able to donate a kobject reference to the port.
854 */
855 boolean_t
856 ipc_kobject_make_send_lazy_alloc_port(
857 ipc_port_t *port_store,
858 ipc_kobject_t kobject,
859 ipc_kobject_type_t type,
860 ipc_kobject_alloc_options_t alloc_opts,
861 boolean_t __ptrauth_only should_ptrauth,
862 uint64_t __ptrauth_only ptrauth_discriminator)
863 {
864 ipc_port_t port, previous, __ptrauth_only port_addr;
865 boolean_t rc = FALSE;
866
867 port = os_atomic_load(port_store, dependency);
868
869 #if __has_feature(ptrauth_calls)
870 /* If we're on a ptrauth system and this port is signed, authenticate and strip the pointer */
871 if (should_ptrauth && IP_VALID(port)) {
872 port = ptrauth_auth_data(port,
873 ptrauth_key_process_independent_data,
874 ptrauth_blend_discriminator(port_store, ptrauth_discriminator));
875 }
876 #endif // __has_feature(ptrauth_calls)
877
878 if (!IP_VALID(port)) {
879 port = ipc_kobject_alloc_port(kobject, type,
880 IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST | alloc_opts);
881
882 #if __has_feature(ptrauth_calls)
883 if (should_ptrauth) {
884 port_addr = ptrauth_sign_unauthenticated(port,
885 ptrauth_key_process_independent_data,
886 ptrauth_blend_discriminator(port_store, ptrauth_discriminator));
887 } else {
888 port_addr = port;
889 }
890 #else
891 port_addr = port;
892 #endif // __has_feature(ptrauth_calls)
893
894 if (os_atomic_cmpxchgv(port_store, IP_NULL, port_addr, &previous, release)) {
895 return TRUE;
896 }
897
898 // undo what ipc_kobject_alloc_port() did above
899 port->ip_nsrequest = IP_NULL;
900 port->ip_mscount = 0;
901 port->ip_sorights = 0;
902 port->ip_srights = 0;
903 ip_release(port);
904 ip_release(port);
905 ipc_port_dealloc_kernel(port);
906
907 port = previous;
908 }
909
910 ip_lock(port);
911 ipc_port_make_send_locked(port);
912 if (port->ip_srights == 1) {
913 ipc_port_make_sonce_locked(port);
914 assert(port->ip_nsrequest == IP_NULL);
915 port->ip_nsrequest = port;
916 rc = TRUE;
917 }
918 ip_unlock(port);
919
920 return rc;
921 }
922
923 /*
924 * Routine: ipc_kobject_make_send_lazy_alloc_labeled_port
925 * Purpose:
926 * Make a send once for a kobject port.
927 *
928 * A location owning this port is passed in port_store.
929 * If no port exists, a port is made lazily.
930 *
931 * A send right is made for the port, and if this is the first one
932 * (possibly not for the first time), then the no-more-senders
933 * notification is rearmed.
934 *
935 * When a notification is armed, the kobject must donate
936 * one of its references to the port. It is expected
937 * the no-more-senders notification will consume this reference.
938 *
939 * Returns:
940 * TRUE if a notification was armed
941 * FALSE else
942 *
943 * Conditions:
944 * Nothing is locked, memory can be allocated.
945 * The caller must be able to donate a kobject reference to the port.
946 */
947 boolean_t
948 ipc_kobject_make_send_lazy_alloc_labeled_port(
949 ipc_port_t *port_store,
950 ipc_kobject_t kobject,
951 ipc_kobject_type_t type,
952 ipc_label_t label)
953 {
954 ipc_port_t port, previous;
955 boolean_t rc = FALSE;
956
957 port = os_atomic_load(port_store, dependency);
958
959 if (!IP_VALID(port)) {
960 port = ipc_kobject_alloc_labeled_port(kobject, type, label,
961 IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST);
962 if (os_atomic_cmpxchgv(port_store, IP_NULL, port, &previous, release)) {
963 return TRUE;
964 }
965
966 // undo what ipc_kobject_alloc_port() did above
967 port->ip_nsrequest = IP_NULL;
968 port->ip_mscount = 0;
969 port->ip_sorights = 0;
970 port->ip_srights = 0;
971 ip_release(port);
972 ip_release(port);
973 zfree(ipc_kobject_label_zone, port->ip_kolabel);
974 port->ip_object.io_bits &= ~IO_BITS_KOLABEL;
975 port->ip_kolabel = NULL;
976 ipc_port_dealloc_kernel(port);
977
978 port = previous;
979 assert(ip_is_kolabeled(port));
980 }
981
982 ip_lock(port);
983 ipc_port_make_send_locked(port);
984 if (port->ip_srights == 1) {
985 ipc_port_make_sonce_locked(port);
986 assert(port->ip_nsrequest == IP_NULL);
987 port->ip_nsrequest = port;
988 rc = TRUE;
989 }
990 ip_unlock(port);
991
992 return rc;
993 }
994
995
996 /*
997 * Routine: ipc_kobject_destroy
998 * Purpose:
999 * Release any kernel object resources associated
1000 * with the port, which is being destroyed.
1001 *
1002 * This path to free object resources should only be
1003 * needed when resources are associated with a user's port.
1004 * In the normal case, when the kernel is the receiver,
1005 * the code calling ipc_port_dealloc_kernel should clean
1006 * up the object resources.
1007 *
1008 * Cleans up any kobject label that might be present.
1009 * Conditions:
1010 * The port is not locked, but it is dead.
1011 */
1012
1013 void
1014 ipc_kobject_destroy(
1015 ipc_port_t port)
1016 {
1017 switch (ip_kotype(port)) {
1018 case IKOT_TIMER:
1019 mk_timer_port_destroy(port);
1020 break;
1021
1022 case IKOT_NAMED_ENTRY:
1023 mach_destroy_memory_entry(port);
1024 break;
1025
1026 case IKOT_HOST_NOTIFY:
1027 host_notify_port_destroy(port);
1028 break;
1029
1030 case IKOT_SUID_CRED:
1031 suid_cred_destroy(port);
1032 break;
1033
1034 default:
1035 break;
1036 }
1037
1038 if (ip_is_kolabeled(port)) {
1039 ipc_kobject_label_t labelp = port->ip_kolabel;
1040
1041 assert(labelp != NULL);
1042 assert(ip_is_kobject(port));
1043 port->ip_kolabel = NULL;
1044 port->ip_object.io_bits &= ~IO_BITS_KOLABEL;
1045 zfree(ipc_kobject_label_zone, labelp);
1046 }
1047 }
1048
1049 /*
1050 * Routine: ipc_kobject_label_substitute_task
1051 * Purpose:
1052 * Substitute a task control port for its immovable
1053 * equivalent when the receiver is that task.
1054 * Conditions:
1055 * Space is write locked and active.
1056 * Port is locked and active.
1057 * Returns:
1058 * - IP_NULL port if no substitution is to be done
1059 * - a valid port if a substitution needs to happen
1060 */
1061 static ipc_port_t
1062 ipc_kobject_label_substitute_task(
1063 ipc_space_t space,
1064 ipc_port_t port)
1065 {
1066 ipc_port_t subst = IP_NULL;
1067 task_t task = ipc_kobject_get(port);
1068
1069 if (task != TASK_NULL && task == space->is_task) {
1070 if ((subst = port->ip_alt_port)) {
1071 return subst;
1072 }
1073 }
1074
1075 return IP_NULL;
1076 }
1077
1078 /*
1079 * Routine: ipc_kobject_label_substitute_thread
1080 * Purpose:
1081 * Substitute a thread control port for its immovable
1082 * equivalent when it belongs to the receiver task.
1083 * Conditions:
1084 * Space is write locked and active.
1085 * Port is locked and active.
1086 * Returns:
1087 * - IP_NULL port if no substitution is to be done
1088 * - a valid port if a substitution needs to happen
1089 */
1090 static ipc_port_t
1091 ipc_kobject_label_substitute_thread(
1092 ipc_space_t space,
1093 ipc_port_t port)
1094 {
1095 ipc_port_t subst = IP_NULL;
1096 thread_t thread = ipc_kobject_get(port);
1097
1098 if (thread != THREAD_NULL && space->is_task == thread->task) {
1099 if ((subst = port->ip_alt_port) != IP_NULL) {
1100 return subst;
1101 }
1102 }
1103
1104 return IP_NULL;
1105 }
1106
1107 /*
1108 * Routine: ipc_kobject_label_check
1109 * Purpose:
1110 * Check to see if the space is allowed to possess
1111 * a right for the given port. In order to qualify,
1112 * the space label must contain all the privileges
1113 * listed in the port/kobject label.
1114 *
1115 * Conditions:
1116 * Space is write locked and active.
1117 * Port is locked and active.
1118 *
1119 * Returns:
1120 * Whether the copyout is authorized.
1121 *
1122 * If a port substitution is requested, the space is unlocked,
1123 * the port is unlocked and its "right" consumed.
1124 *
1125 * As of now, substituted ports only happen for send rights.
1126 */
1127 bool
1128 ipc_kobject_label_check(
1129 ipc_space_t space,
1130 ipc_port_t port,
1131 mach_msg_type_name_t msgt_name,
1132 ipc_object_copyout_flags_t *flags,
1133 ipc_port_t *subst_portp)
1134 {
1135 ipc_kobject_label_t labelp;
1136 ipc_label_t label;
1137
1138 assert(is_active(space));
1139 assert(ip_active(port));
1140
1141 *subst_portp = IP_NULL;
1142
1143 /* Unlabled ports/kobjects are always allowed */
1144 if (!ip_is_kolabeled(port)) {
1145 return true;
1146 }
1147
1148 /* Never OK to copyout the receive right for a labeled kobject */
1149 if (msgt_name == MACH_MSG_TYPE_PORT_RECEIVE) {
1150 panic("ipc_kobject_label_check: attempted receive right "
1151 "copyout for labeled kobject");
1152 }
1153
1154 labelp = port->ip_kolabel;
1155 label = labelp->ikol_label;
1156
1157 if ((*flags & IPC_OBJECT_COPYOUT_FLAGS_NO_LABEL_CHECK) == 0 &&
1158 (label & IPC_LABEL_SUBST_MASK)) {
1159 ipc_port_t subst = IP_NULL;
1160
1161 if (msgt_name != MACH_MSG_TYPE_PORT_SEND) {
1162 return false;
1163 }
1164
1165 switch (label & IPC_LABEL_SUBST_MASK) {
1166 case IPC_LABEL_SUBST_TASK:
1167 subst = ipc_kobject_label_substitute_task(space, port);
1168 break;
1169 case IPC_LABEL_SUBST_THREAD:
1170 subst = ipc_kobject_label_substitute_thread(space, port);
1171 break;
1172 case IPC_LABEL_SUBST_ONCE:
1173 /* the next check will _not_ substitute */
1174 *flags |= IPC_OBJECT_COPYOUT_FLAGS_NO_LABEL_CHECK;
1175 subst = ip_get_kobject(port);
1176 break;
1177 default:
1178 panic("unexpected label: %llx\n", label);
1179 }
1180
1181 if (subst != IP_NULL) {
1182 ip_reference(subst);
1183 is_write_unlock(space);
1184 ipc_port_release_send_and_unlock(port);
1185 port = ipc_port_make_send(subst);
1186 ip_release(subst);
1187 *subst_portp = port;
1188 return true;
1189 }
1190 }
1191
1192 return (label & space->is_label & IPC_LABEL_SPACE_MASK) ==
1193 (label & IPC_LABEL_SPACE_MASK);
1194 }
1195
1196 boolean_t
1197 ipc_kobject_notify(
1198 mach_msg_header_t *request_header,
1199 mach_msg_header_t *reply_header)
1200 {
1201 mach_msg_max_trailer_t * trailer;
1202 ipc_port_t port = request_header->msgh_remote_port;
1203
1204 ((mig_reply_error_t *) reply_header)->RetCode = MIG_NO_REPLY;
1205
1206 trailer = (mach_msg_max_trailer_t *)
1207 ((vm_offset_t)request_header + request_header->msgh_size);
1208
1209 /*
1210 * The kobject notification is privileged and can change the
1211 * refcount on kernel-internal objects - make sure
1212 * that the message wasn't faked!
1213 */
1214 if (0 != bcmp(&trailer->msgh_audit, &KERNEL_AUDIT_TOKEN,
1215 sizeof(trailer->msgh_audit))) {
1216 return FALSE;
1217 }
1218 if (0 != bcmp(&trailer->msgh_sender, &KERNEL_SECURITY_TOKEN,
1219 sizeof(trailer->msgh_sender))) {
1220 return FALSE;
1221 }
1222
1223 switch (request_header->msgh_id) {
1224 case MACH_NOTIFY_NO_SENDERS:
1225 switch (ip_kotype(port)) {
1226 case IKOT_VOUCHER:
1227 ipc_voucher_notify(request_header);
1228 return TRUE;
1229
1230 case IKOT_VOUCHER_ATTR_CONTROL:
1231 ipc_voucher_attr_control_notify(request_header);
1232 return TRUE;
1233
1234 case IKOT_PORT_SUBST_ONCE:
1235 ipc_kobject_subst_once_notify(request_header);
1236 return TRUE;
1237
1238 case IKOT_SEMAPHORE:
1239 semaphore_notify(request_header);
1240 return TRUE;
1241
1242 case IKOT_EVENTLINK:
1243 ipc_eventlink_notify(request_header);
1244 return TRUE;
1245
1246 case IKOT_TASK_CONTROL:
1247 task_port_notify(request_header);
1248 return TRUE;
1249
1250 case IKOT_NAMED_ENTRY:
1251 ip_lock(port);
1252
1253 /*
1254 * Bring the sequence number and mscount in
1255 * line with ipc_port_destroy assertion.
1256 */
1257 port->ip_mscount = 0;
1258 port->ip_messages.imq_seqno = 0;
1259 ipc_port_destroy(port); /* releases lock */
1260 return TRUE;
1261
1262 case IKOT_UPL:
1263 upl_no_senders(
1264 request_header->msgh_remote_port,
1265 (mach_port_mscount_t)
1266 ((mach_no_senders_notification_t *)
1267 request_header)->not_count);
1268 reply_header->msgh_remote_port = MACH_PORT_NULL;
1269 return TRUE;
1270
1271 #if CONFIG_AUDIT
1272 case IKOT_AU_SESSIONPORT:
1273 audit_session_nosenders(request_header);
1274 return TRUE;
1275 #endif
1276 case IKOT_FILEPORT:
1277 fileport_notify(request_header);
1278 return TRUE;
1279
1280 case IKOT_WORK_INTERVAL:
1281 work_interval_port_notify(request_header);
1282 return TRUE;
1283 case IKOT_TASK_READ:
1284 case IKOT_TASK_INSPECT:
1285 task_port_with_flavor_notify(request_header);
1286 return TRUE;
1287 case IKOT_THREAD_READ:
1288 case IKOT_THREAD_INSPECT:
1289 thread_port_with_flavor_notify(request_header);
1290 return TRUE;
1291 case IKOT_SUID_CRED:
1292 suid_cred_notify(request_header);
1293 return TRUE;
1294 case IKOT_TASK_ID_TOKEN:
1295 task_id_token_notify(request_header);
1296 return TRUE;
1297 #if HYPERVISOR
1298 case IKOT_HYPERVISOR:
1299 hv_port_notify(request_header);
1300 return TRUE;
1301 #endif
1302 }
1303
1304 break;
1305
1306 case MACH_NOTIFY_PORT_DELETED:
1307 case MACH_NOTIFY_PORT_DESTROYED:
1308 case MACH_NOTIFY_SEND_ONCE:
1309 case MACH_NOTIFY_DEAD_NAME:
1310 break;
1311
1312 default:
1313 return FALSE;
1314 }
1315 switch (ip_kotype(port)) {
1316 #ifdef IOKIT
1317 case IKOT_IOKIT_OBJECT:
1318 case IKOT_IOKIT_CONNECT:
1319 case IKOT_IOKIT_IDENT:
1320 case IKOT_UEXT_OBJECT:
1321 {
1322 return iokit_notify(request_header);
1323 }
1324 #endif
1325 case IKOT_TASK_RESUME:
1326 {
1327 return task_suspension_notify(request_header);
1328 }
1329
1330 default:
1331 return FALSE;
1332 }
1333 }