2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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,
61 * Copyright (c) 2005 SPARTA, Inc.
66 * File: kern/ipc_kobject.c
70 * Functions for letting a port represent a kernel object.
73 #include <mach_debug.h>
74 #include <mach_ipc_test.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>
82 #include <mach/vm_param.h>
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>
101 #include <mach/vm32_map_server.h>
103 #include <mach/thread_act_server.h>
104 #include <mach/restartable_server.h>
106 #include <mach/exc_server.h>
107 #include <mach/mach_exc_server.h>
108 #include <mach/mach_eventlink_server.h>
110 #include <device/device_types.h>
111 #include <device/device_server.h>
113 #if CONFIG_USER_NOTIFICATION
114 #include <UserNotification/UNDReplyServer.h>
118 #include <mach/arcade_register_server.h>
122 #include <kern/audit_sessionport.h>
125 #if MACH_MACHINE_ROUTINES
126 #include <machine/machine_routines.h>
127 #endif /* MACH_MACHINE_ROUTINES */
129 #include <uk_xkern/xk_uproxy_server.h>
130 #endif /* XK_PROXY */
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>
142 #include <kern/arcade.h>
143 #endif /* CONFIG_ARCADE */
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>
154 #include <kern/hv_support.h>
157 #include <vm/vm_protos.h>
159 #include <security/mac_mach_internal.h>
161 extern char *proc_name_address(void *p
);
163 extern int proc_pid(struct proc
*p
);
166 * Routine: ipc_kobject_notify
168 * Deliver notifications to kobjects that care about them.
172 mach_msg_header_t
*request_header
,
173 mach_msg_header_t
*reply_header
);
177 mig_routine_t routine
;
182 #define MAX_MIG_ENTRIES 1031
183 #define MIG_HASH(x) (x)
185 #define KOBJ_IDX_NOT_SET (-1)
188 #define max(a, b) (((a) > (b)) ? (a) : (b))
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 */
195 static ZONE_DECLARE(ipc_kobject_label_zone
, "ipc kobject labels",
196 sizeof(struct ipc_kobject_label
), ZC_NONE
);
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
,
214 (const struct mig_subsystem
*)&vm32_map_subsystem
,
216 #if CONFIG_USER_NOTIFICATION
217 (const struct mig_subsystem
*)&UNDReply_subsystem
,
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
,
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 */
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
,
236 (const struct mig_subsystem
*)&arcade_register_subsystem
,
238 (const struct mig_subsystem
*)&mach_eventlink_subsystem
,
244 unsigned int i
, n
= sizeof(mig_e
) / sizeof(const struct mig_subsystem
*);
246 mach_msg_id_t j
, pos
, nentry
, range
;
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!");
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");
265 if (howmany
== MAX_MIG_ENTRIES
) {
266 panic("the mig dispatch table is too small");
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
;
275 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
277 mig_buckets
[pos
].kobjidx
= KOBJ_IDX_NOT_SET
;
279 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
284 printf("mig_table_max_displ = %d mach_kobj_count = %d\n",
285 mig_table_max_displ
, mach_kobj_count
);
287 STARTUP(MACH_IPC
, STARTUP_RANK_FIRST
, mig_init
);
290 * Do a hash table lookup for given msgh_id. Return 0
294 find_mig_hash_entry(int msgh_id
)
296 unsigned int i
= (unsigned int)MIG_HASH(msgh_id
);
297 int max_iter
= mig_table_max_displ
;
301 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
302 } while (msgh_id
!= ptr
->num
&& ptr
->num
&& --max_iter
);
304 if (!ptr
->routine
|| msgh_id
!= ptr
->num
) {
305 ptr
= (mig_hash_t
*)0;
312 * Routine: ipc_kobject_set_kobjidx
314 * Set the index for the kobject filter
315 * mask for a given message ID.
318 ipc_kobject_set_kobjidx(
322 mig_hash_t
*ptr
= find_mig_hash_entry(msgh_id
);
324 if (ptr
== (mig_hash_t
*)0) {
325 return KERN_INVALID_ARGUMENT
;
328 assert(index
< mach_kobj_count
);
329 ptr
->kobjidx
= index
;
335 * Routine: ipc_kobject_server
337 * Handle a message sent to the kernel.
338 * Generates a reply message.
339 * Version for Untyped IPC.
347 mach_msg_option_t __unused option
)
349 mach_msg_size_t reply_size
;
352 ipc_port_t replyp
= IPC_PORT_NULL
;
353 mach_msg_max_trailer_t
*trailer
;
355 task_t task
= TASK_NULL
;
357 boolean_t exec_token_changed
= FALSE
;
358 int request_msgh_id
= request
->ikm_header
->msgh_id
;
363 port
= request
->ikm_header
->msgh_remote_port
;
364 if (IP_VALID(port
)) {
365 ikot
= ip_kotype(port
);
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
);
377 /* Find corresponding mig_hash entry, if any */
378 ptr
= find_mig_hash_entry(request_msgh_id
);
380 /* Get the reply_size. */
381 if (ptr
== (mig_hash_t
*)0) {
382 reply_size
= sizeof(mig_reply_error_t
);
384 reply_size
= ptr
->size
;
387 /* round up for trailer size */
388 reply_size
+= MAX_TRAILER_SIZE
;
389 reply
= ipc_kmsg_alloc(reply_size
);
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
);
399 * Initialize reply message.
402 #define InP ((mach_msg_header_t *) request->ikm_header)
403 #define OutP ((mig_reply_error_t *) reply->ikm_header)
406 * MIG should really assure no data leakage -
407 * but until it does, pessimistically zero the
408 * whole reply buffer.
410 bzero((void *)OutP
, reply_size
);
412 OutP
->NDR
= NDR_record
;
413 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
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;
427 * Find the routine to call, and call it
428 * to perform the kernel function
430 ipc_kmsg_trace_send(request
, option
);
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.
437 if (ikot
== IKOT_TASK_CONTROL
) {
438 task
= convert_port_to_task_with_exec_token(port
, &exec_token
, TRUE
);
442 int idx
= ptr
->kobjidx
;
443 task_t curtask
= current_task();
444 uint8_t *filter_mask
= curtask
->mach_kobj_filter_mask
;
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
;
459 #endif /* CONFIG_MACF */
461 (*ptr
->routine
)(request
->ikm_header
, reply
->ikm_header
);
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
;
472 task_deallocate(task
);
475 kernel_task
->messages_received
++;
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
);
484 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
487 kernel_task
->messages_received
++;
490 kernel_task
->messages_sent
++;
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
;
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.
509 * We set msgh_remote_port to IP_NULL so that the kmsg
510 * destroy routines don't try to destroy the port twice.
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
;
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
;
524 panic("ipc_kobject_server: strange destination rights");
528 * Destroy voucher. The kernel MIG servers never take ownership
529 * of vouchers sent in messages. Swallow any such rights here.
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
;
538 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
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
546 ipc_kmsg_free(request
);
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.
553 request
->ikm_header
->msgh_local_port
= MACH_PORT_NULL
;
554 ipc_kmsg_destroy(request
);
557 if (kr
== MIG_NO_REPLY
) {
559 * The server function will send a reply message
560 * using the reply port right, which it has saved.
564 ipc_kmsg_free(reply
);
570 replyp
= reply
->ikm_header
->msgh_remote_port
;
573 if (!IP_VALID(replyp
)) {
575 * Can't queue the reply message if the destination
576 * (the reply port) isn't valid.
579 ipc_kmsg_destroy(reply
);
582 } else if (replyp
->ip_receiver
== ipc_space_kernel
) {
584 * Don't send replies to kobject kernel ports
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
);
594 /* Fail the MIG call if the task exec token changed during the call */
595 if (kr
== KERN_SUCCESS
&& exec_token_changed
) {
597 * Create a new reply msg with error and destroy the old reply msg.
599 ipc_kmsg_t new_reply
= ipc_kmsg_alloc(reply_size
);
601 if (new_reply
== IKM_NULL
) {
602 printf("ipc_kobject_server: dropping request\n");
603 ipc_kmsg_destroy(reply
);
607 * Initialize the new reply message.
610 #define OutP_new ((mig_reply_error_t *) new_reply->ikm_header)
611 #define OutP_old ((mig_reply_error_t *) reply->ikm_header)
613 bzero((void *)OutP_new
, reply_size
);
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
;
623 /* Set the error as KERN_INVALID_TASK */
624 OutP_new
->RetCode
= KERN_INVALID_TASK
;
631 * Destroy everything in reply except the reply port right,
632 * which is needed in the new reply message.
634 reply
->ikm_header
->msgh_remote_port
= MACH_PORT_NULL
;
635 ipc_kmsg_destroy(reply
);
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
;
652 * Routine: ipc_kobject_set
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.
658 * Nothing locked. The port must be active if setting
659 * a kobject linkage. Clearing a linkage is OK on an
665 ipc_kobject_t kobject
,
666 ipc_kobject_type_t type
)
669 ipc_kobject_set_atomically(port
, kobject
, type
);
674 ipc_kobject_set_atomically(
676 ipc_kobject_t kobject
,
677 ipc_kobject_type_t type
)
679 assert(type
== IKOT_NONE
|| ip_active(port
));
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
;
688 port
->ip_kobject
= kobject
;
690 if (type
!= IKOT_NONE
) {
691 /* Once set, this bit can never be unset */
692 port
->ip_object
.io_bits
|= IO_BITS_KOBJECT
;
697 * Routine: ipc_kobject_init_port
699 * Initialize a kobject port with the given types and options.
701 * This function never fails.
704 ipc_kobject_init_port(
706 ipc_kobject_t kobject
,
707 ipc_kobject_type_t type
,
708 ipc_kobject_alloc_options_t options
)
710 ipc_kobject_set_atomically(port
, kobject
, type
);
712 if (options
& IPC_KOBJECT_ALLOC_MAKE_SEND
) {
713 ipc_port_make_send_locked(port
);
715 if (options
& IPC_KOBJECT_ALLOC_NSREQUEST
) {
716 ipc_port_make_sonce_locked(port
);
717 port
->ip_nsrequest
= port
;
719 if (options
& IPC_KOBJECT_ALLOC_NO_GRANT
) {
720 port
->ip_no_grant
= 1;
722 if (options
& IPC_KOBJECT_ALLOC_IMMOVABLE_SEND
) {
723 port
->ip_immovable_send
= 1;
725 if (options
& IPC_KOBJECT_ALLOC_PINNED
) {
731 * Routine: ipc_kobject_alloc_port
733 * Allocate a kobject port in the kernel space of the specified type.
735 * This function never fails.
738 * No locks held (memory is allocated)
741 ipc_kobject_alloc_port(
742 ipc_kobject_t kobject
,
743 ipc_kobject_type_t type
,
744 ipc_kobject_alloc_options_t options
)
746 ipc_port_t port
= ipc_port_alloc_kernel();
748 if (port
== IP_NULL
) {
749 panic("ipc_kobject_alloc_port(): failed to allocate port");
752 ipc_kobject_init_port(port
, kobject
, type
, options
);
757 * Routine: ipc_kobject_alloc_labeled_port
759 * Allocate a kobject port and associated mandatory access label
760 * in the kernel space of the specified type.
762 * This function never fails.
765 * No locks held (memory is allocated)
769 ipc_kobject_alloc_labeled_port(
770 ipc_kobject_t kobject
,
771 ipc_kobject_type_t type
,
773 ipc_kobject_alloc_options_t options
)
776 ipc_kobject_label_t labelp
;
778 port
= ipc_port_alloc_kernel();
779 if (port
== IP_NULL
) {
780 panic("ipc_kobject_alloc_port(): failed to allocate port");
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");
787 labelp
->ikol_label
= label
;
788 port
->ip_kolabel
= labelp
;
789 port
->ip_object
.io_bits
|= IO_BITS_KOLABEL
;
791 ipc_kobject_init_port(port
, kobject
, type
, options
);
796 ipc_kobject_subst_once_notify(mach_msg_header_t
*msg
)
798 mach_no_senders_notification_t
*notification
= (void *)msg
;
799 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
801 require_ip_active(port
);
802 assert(IKOT_PORT_SUBST_ONCE
== ip_kotype(port
));
804 ip_release((ipc_port_t
)ip_get_kobject(port
));
805 ipc_port_dealloc_kernel(port
);
809 * Routine: ipc_kobject_alloc_subst_once
811 * Make a port that will be substituted by the kolabel
812 * rules once, preventing the next substitution (of its target)
816 * A port with a send right, that will substitute to its "kobject".
819 * No locks held (memory is allocated)
820 * `target` has a refcount that this function consumes
823 ipc_kobject_alloc_subst_once(
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
);
832 * Routine: ipc_kobject_make_send_lazy_alloc_port
834 * Make a send once for a kobject port.
836 * A location owning this port is passed in port_store.
837 * If no port exists, a port is made lazily.
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.
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.
848 * TRUE if a notification was armed
852 * Nothing is locked, memory can be allocated.
853 * The caller must be able to donate a kobject reference to the port.
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
)
864 ipc_port_t port
, previous
, __ptrauth_only port_addr
;
865 boolean_t rc
= FALSE
;
867 port
= os_atomic_load(port_store
, dependency
);
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
));
876 #endif // __has_feature(ptrauth_calls)
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
);
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
));
892 #endif // __has_feature(ptrauth_calls)
894 if (os_atomic_cmpxchgv(port_store
, IP_NULL
, port_addr
, &previous
, release
)) {
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;
905 ipc_port_dealloc_kernel(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
;
924 * Routine: ipc_kobject_make_send_lazy_alloc_labeled_port
926 * Make a send once for a kobject port.
928 * A location owning this port is passed in port_store.
929 * If no port exists, a port is made lazily.
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.
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.
940 * TRUE if a notification was armed
944 * Nothing is locked, memory can be allocated.
945 * The caller must be able to donate a kobject reference to the port.
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
,
954 ipc_port_t port
, previous
;
955 boolean_t rc
= FALSE
;
957 port
= os_atomic_load(port_store
, dependency
);
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
)) {
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;
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
);
979 assert(ip_is_kolabeled(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
;
997 * Routine: ipc_kobject_destroy
999 * Release any kernel object resources associated
1000 * with the port, which is being destroyed.
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.
1008 * Cleans up any kobject label that might be present.
1010 * The port is not locked, but it is dead.
1014 ipc_kobject_destroy(
1017 switch (ip_kotype(port
)) {
1019 mk_timer_port_destroy(port
);
1022 case IKOT_NAMED_ENTRY
:
1023 mach_destroy_memory_entry(port
);
1026 case IKOT_HOST_NOTIFY
:
1027 host_notify_port_destroy(port
);
1030 case IKOT_SUID_CRED
:
1031 suid_cred_destroy(port
);
1038 if (ip_is_kolabeled(port
)) {
1039 ipc_kobject_label_t labelp
= port
->ip_kolabel
;
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
);
1050 * Routine: ipc_kobject_label_substitute_task
1052 * Substitute a task control port for its immovable
1053 * equivalent when the receiver is that task.
1055 * Space is write locked and active.
1056 * Port is locked and active.
1058 * - IP_NULL port if no substitution is to be done
1059 * - a valid port if a substitution needs to happen
1062 ipc_kobject_label_substitute_task(
1066 ipc_port_t subst
= IP_NULL
;
1067 task_t task
= ipc_kobject_get(port
);
1069 if (task
!= TASK_NULL
&& task
== space
->is_task
) {
1070 if ((subst
= port
->ip_alt_port
)) {
1079 * Routine: ipc_kobject_label_substitute_thread
1081 * Substitute a thread control port for its immovable
1082 * equivalent when it belongs to the receiver task.
1084 * Space is write locked and active.
1085 * Port is locked and active.
1087 * - IP_NULL port if no substitution is to be done
1088 * - a valid port if a substitution needs to happen
1091 ipc_kobject_label_substitute_thread(
1095 ipc_port_t subst
= IP_NULL
;
1096 thread_t thread
= ipc_kobject_get(port
);
1098 if (thread
!= THREAD_NULL
&& space
->is_task
== thread
->task
) {
1099 if ((subst
= port
->ip_alt_port
) != IP_NULL
) {
1108 * Routine: ipc_kobject_label_check
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.
1116 * Space is write locked and active.
1117 * Port is locked and active.
1120 * Whether the copyout is authorized.
1122 * If a port substitution is requested, the space is unlocked,
1123 * the port is unlocked and its "right" consumed.
1125 * As of now, substituted ports only happen for send rights.
1128 ipc_kobject_label_check(
1131 mach_msg_type_name_t msgt_name
,
1132 ipc_object_copyout_flags_t
*flags
,
1133 ipc_port_t
*subst_portp
)
1135 ipc_kobject_label_t labelp
;
1138 assert(is_active(space
));
1139 assert(ip_active(port
));
1141 *subst_portp
= IP_NULL
;
1143 /* Unlabled ports/kobjects are always allowed */
1144 if (!ip_is_kolabeled(port
)) {
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");
1154 labelp
= port
->ip_kolabel
;
1155 label
= labelp
->ikol_label
;
1157 if ((*flags
& IPC_OBJECT_COPYOUT_FLAGS_NO_LABEL_CHECK
) == 0 &&
1158 (label
& IPC_LABEL_SUBST_MASK
)) {
1159 ipc_port_t subst
= IP_NULL
;
1161 if (msgt_name
!= MACH_MSG_TYPE_PORT_SEND
) {
1165 switch (label
& IPC_LABEL_SUBST_MASK
) {
1166 case IPC_LABEL_SUBST_TASK
:
1167 subst
= ipc_kobject_label_substitute_task(space
, port
);
1169 case IPC_LABEL_SUBST_THREAD
:
1170 subst
= ipc_kobject_label_substitute_thread(space
, port
);
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
);
1178 panic("unexpected label: %llx\n", label
);
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
);
1187 *subst_portp
= port
;
1192 return (label
& space
->is_label
& IPC_LABEL_SPACE_MASK
) ==
1193 (label
& IPC_LABEL_SPACE_MASK
);
1198 mach_msg_header_t
*request_header
,
1199 mach_msg_header_t
*reply_header
)
1201 mach_msg_max_trailer_t
* trailer
;
1202 ipc_port_t port
= request_header
->msgh_remote_port
;
1204 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
1206 trailer
= (mach_msg_max_trailer_t
*)
1207 ((vm_offset_t
)request_header
+ request_header
->msgh_size
);
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!
1214 if (0 != bcmp(&trailer
->msgh_audit
, &KERNEL_AUDIT_TOKEN
,
1215 sizeof(trailer
->msgh_audit
))) {
1218 if (0 != bcmp(&trailer
->msgh_sender
, &KERNEL_SECURITY_TOKEN
,
1219 sizeof(trailer
->msgh_sender
))) {
1223 switch (request_header
->msgh_id
) {
1224 case MACH_NOTIFY_NO_SENDERS
:
1225 switch (ip_kotype(port
)) {
1227 ipc_voucher_notify(request_header
);
1230 case IKOT_VOUCHER_ATTR_CONTROL
:
1231 ipc_voucher_attr_control_notify(request_header
);
1234 case IKOT_PORT_SUBST_ONCE
:
1235 ipc_kobject_subst_once_notify(request_header
);
1238 case IKOT_SEMAPHORE
:
1239 semaphore_notify(request_header
);
1242 case IKOT_EVENTLINK
:
1243 ipc_eventlink_notify(request_header
);
1246 case IKOT_TASK_CONTROL
:
1247 task_port_notify(request_header
);
1250 case IKOT_NAMED_ENTRY
:
1254 * Bring the sequence number and mscount in
1255 * line with ipc_port_destroy assertion.
1257 port
->ip_mscount
= 0;
1258 port
->ip_messages
.imq_seqno
= 0;
1259 ipc_port_destroy(port
); /* releases lock */
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
;
1272 case IKOT_AU_SESSIONPORT
:
1273 audit_session_nosenders(request_header
);
1277 fileport_notify(request_header
);
1280 case IKOT_WORK_INTERVAL
:
1281 work_interval_port_notify(request_header
);
1283 case IKOT_TASK_READ
:
1284 case IKOT_TASK_INSPECT
:
1285 task_port_with_flavor_notify(request_header
);
1287 case IKOT_THREAD_READ
:
1288 case IKOT_THREAD_INSPECT
:
1289 thread_port_with_flavor_notify(request_header
);
1291 case IKOT_SUID_CRED
:
1292 suid_cred_notify(request_header
);
1294 case IKOT_TASK_ID_TOKEN
:
1295 task_id_token_notify(request_header
);
1298 case IKOT_HYPERVISOR
:
1299 hv_port_notify(request_header
);
1306 case MACH_NOTIFY_PORT_DELETED
:
1307 case MACH_NOTIFY_PORT_DESTROYED
:
1308 case MACH_NOTIFY_SEND_ONCE
:
1309 case MACH_NOTIFY_DEAD_NAME
:
1315 switch (ip_kotype(port
)) {
1317 case IKOT_IOKIT_OBJECT
:
1318 case IKOT_IOKIT_CONNECT
:
1319 case IKOT_IOKIT_IDENT
:
1320 case IKOT_UEXT_OBJECT
:
1322 return iokit_notify(request_header
);
1325 case IKOT_TASK_RESUME
:
1327 return task_suspension_notify(request_header
);